10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55864Sesaxe * Common Development and Distribution License (the "License").
65864Sesaxe * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
225864Sesaxe * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * The Cyclic Subsystem
280Sstevel@tonic-gate * --------------------
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * Prehistory
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * Historically, most computer architectures have specified interval-based
330Sstevel@tonic-gate * timer parts (e.g. SPARCstation's counter/timer; Intel's i8254). While
340Sstevel@tonic-gate * these parts deal in relative (i.e. not absolute) time values, they are
350Sstevel@tonic-gate * typically used by the operating system to implement the abstraction of
360Sstevel@tonic-gate * absolute time. As a result, these parts cannot typically be reprogrammed
370Sstevel@tonic-gate * without introducing error in the system's notion of time.
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * Starting in about 1994, chip architectures began specifying high resolution
400Sstevel@tonic-gate * timestamp registers. As of this writing (1999), all major chip families
410Sstevel@tonic-gate * (UltraSPARC, PentiumPro, MIPS, PowerPC, Alpha) have high resolution
420Sstevel@tonic-gate * timestamp registers, and two (UltraSPARC and MIPS) have added the capacity
430Sstevel@tonic-gate * to interrupt based on timestamp values. These timestamp-compare registers
440Sstevel@tonic-gate * present a time-based interrupt source which can be reprogrammed arbitrarily
450Sstevel@tonic-gate * often without introducing error. Given the low cost of implementing such a
460Sstevel@tonic-gate * timestamp-compare register (and the tangible benefit of eliminating
470Sstevel@tonic-gate * discrete timer parts), it is reasonable to expect that future chip
480Sstevel@tonic-gate * architectures will adopt this feature.
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * The cyclic subsystem has been designed to take advantage of chip
510Sstevel@tonic-gate * architectures with the capacity to interrupt based on absolute, high
520Sstevel@tonic-gate * resolution values of time.
530Sstevel@tonic-gate *
540Sstevel@tonic-gate * Subsystem Overview
550Sstevel@tonic-gate *
560Sstevel@tonic-gate * The cyclic subsystem is a low-level kernel subsystem designed to provide
570Sstevel@tonic-gate * arbitrarily high resolution, per-CPU interval timers (to avoid colliding
580Sstevel@tonic-gate * with existing terms, we dub such an interval timer a "cyclic"). Cyclics
590Sstevel@tonic-gate * can be specified to fire at high, lock or low interrupt level, and may be
600Sstevel@tonic-gate * optionally bound to a CPU or a CPU partition. A cyclic's CPU or CPU
610Sstevel@tonic-gate * partition binding may be changed dynamically; the cyclic will be "juggled"
620Sstevel@tonic-gate * to a CPU which satisfies the new binding. Alternatively, a cyclic may
630Sstevel@tonic-gate * be specified to be "omnipresent", denoting firing on all online CPUs.
640Sstevel@tonic-gate *
650Sstevel@tonic-gate * Cyclic Subsystem Interface Overview
660Sstevel@tonic-gate * -----------------------------------
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * The cyclic subsystem has interfaces with the kernel at-large, with other
690Sstevel@tonic-gate * kernel subsystems (e.g. the processor management subsystem, the checkpoint
700Sstevel@tonic-gate * resume subsystem) and with the platform (the cyclic backend). Each
710Sstevel@tonic-gate * of these interfaces is given a brief synopsis here, and is described
720Sstevel@tonic-gate * in full above the interface's implementation.
730Sstevel@tonic-gate *
740Sstevel@tonic-gate * The following diagram displays the cyclic subsystem's interfaces to
750Sstevel@tonic-gate * other kernel components. The arrows denote a "calls" relationship, with
760Sstevel@tonic-gate * the large arrow indicating the cyclic subsystem's consumer interface.
770Sstevel@tonic-gate * Each arrow is labeled with the section in which the corresponding
780Sstevel@tonic-gate * interface is described.
790Sstevel@tonic-gate *
800Sstevel@tonic-gate * Kernel at-large consumers
810Sstevel@tonic-gate * -----------++------------
820Sstevel@tonic-gate * ||
830Sstevel@tonic-gate * ||
840Sstevel@tonic-gate * _||_
850Sstevel@tonic-gate * \ /
860Sstevel@tonic-gate * \/
870Sstevel@tonic-gate * +---------------------+
880Sstevel@tonic-gate * | |
890Sstevel@tonic-gate * | Cyclic subsystem |<----------- Other kernel subsystems
900Sstevel@tonic-gate * | |
910Sstevel@tonic-gate * +---------------------+
920Sstevel@tonic-gate * ^ |
930Sstevel@tonic-gate * | |
940Sstevel@tonic-gate * | |
950Sstevel@tonic-gate * | v
960Sstevel@tonic-gate * +---------------------+
970Sstevel@tonic-gate * | |
980Sstevel@tonic-gate * | Cyclic backend |
990Sstevel@tonic-gate * | (platform specific) |
1000Sstevel@tonic-gate * | |
1010Sstevel@tonic-gate * +---------------------+
1020Sstevel@tonic-gate *
1030Sstevel@tonic-gate *
1040Sstevel@tonic-gate * Kernel At-Large Interfaces
1050Sstevel@tonic-gate *
1060Sstevel@tonic-gate * cyclic_add() <-- Creates a cyclic
1070Sstevel@tonic-gate * cyclic_add_omni() <-- Creates an omnipresent cyclic
1080Sstevel@tonic-gate * cyclic_remove() <-- Removes a cyclic
1090Sstevel@tonic-gate * cyclic_bind() <-- Change a cyclic's CPU or partition binding
110*8048SMadhavan.Venkataraman@Sun.COM * cyclic_reprogram() <-- Reprogram a cyclic's expiration
1110Sstevel@tonic-gate *
1120Sstevel@tonic-gate * Inter-subsystem Interfaces
1130Sstevel@tonic-gate *
1140Sstevel@tonic-gate * cyclic_juggle() <-- Juggles cyclics away from a CPU
1150Sstevel@tonic-gate * cyclic_offline() <-- Offlines cyclic operation on a CPU
1160Sstevel@tonic-gate * cyclic_online() <-- Reenables operation on an offlined CPU
1170Sstevel@tonic-gate * cyclic_move_in() <-- Notifies subsystem of change in CPU partition
1180Sstevel@tonic-gate * cyclic_move_out() <-- Notifies subsystem of change in CPU partition
1190Sstevel@tonic-gate * cyclic_suspend() <-- Suspends the cyclic subsystem on all CPUs
1200Sstevel@tonic-gate * cyclic_resume() <-- Resumes the cyclic subsystem on all CPUs
1210Sstevel@tonic-gate *
1220Sstevel@tonic-gate * Backend Interfaces
1230Sstevel@tonic-gate *
1240Sstevel@tonic-gate * cyclic_init() <-- Initializes the cyclic subsystem
1250Sstevel@tonic-gate * cyclic_fire() <-- CY_HIGH_LEVEL interrupt entry point
1260Sstevel@tonic-gate * cyclic_softint() <-- CY_LOCK/LOW_LEVEL soft interrupt entry point
1270Sstevel@tonic-gate *
1280Sstevel@tonic-gate * The backend-supplied interfaces (through the cyc_backend structure) are
1290Sstevel@tonic-gate * documented in detail in <sys/cyclic_impl.h>
1300Sstevel@tonic-gate *
1310Sstevel@tonic-gate *
1320Sstevel@tonic-gate * Cyclic Subsystem Implementation Overview
1330Sstevel@tonic-gate * ----------------------------------------
1340Sstevel@tonic-gate *
1350Sstevel@tonic-gate * The cyclic subsystem is designed to minimize interference between cyclics
1360Sstevel@tonic-gate * on different CPUs. Thus, all of the cyclic subsystem's data structures
1370Sstevel@tonic-gate * hang off of a per-CPU structure, cyc_cpu.
1380Sstevel@tonic-gate *
1390Sstevel@tonic-gate * Each cyc_cpu has a power-of-two sized array of cyclic structures (the
1400Sstevel@tonic-gate * cyp_cyclics member of the cyc_cpu structure). If cyclic_add() is called
1410Sstevel@tonic-gate * and there does not exist a free slot in the cyp_cyclics array, the size of
1420Sstevel@tonic-gate * the array will be doubled. The array will never shrink. Cyclics are
1430Sstevel@tonic-gate * referred to by their index in the cyp_cyclics array, which is of type
1440Sstevel@tonic-gate * cyc_index_t.
1450Sstevel@tonic-gate *
1460Sstevel@tonic-gate * The cyclics are kept sorted by expiration time in the cyc_cpu's heap. The
1470Sstevel@tonic-gate * heap is keyed by cyclic expiration time, with parents expiring earlier
1480Sstevel@tonic-gate * than their children.
1490Sstevel@tonic-gate *
1500Sstevel@tonic-gate * Heap Management
1510Sstevel@tonic-gate *
1520Sstevel@tonic-gate * The heap is managed primarily by cyclic_fire(). Upon entry, cyclic_fire()
1530Sstevel@tonic-gate * compares the root cyclic's expiration time to the current time. If the
1540Sstevel@tonic-gate * expiration time is in the past, cyclic_expire() is called on the root
1550Sstevel@tonic-gate * cyclic. Upon return from cyclic_expire(), the cyclic's new expiration time
1560Sstevel@tonic-gate * is derived by adding its interval to its old expiration time, and a
1570Sstevel@tonic-gate * downheap operation is performed. After the downheap, cyclic_fire()
1580Sstevel@tonic-gate * examines the (potentially changed) root cyclic, repeating the
1590Sstevel@tonic-gate * cyclic_expire()/add interval/cyclic_downheap() sequence until the root
1600Sstevel@tonic-gate * cyclic has an expiration time in the future. This expiration time
1610Sstevel@tonic-gate * (guaranteed to be the earliest in the heap) is then communicated to the
1620Sstevel@tonic-gate * backend via cyb_reprogram. Optimal backends will next call cyclic_fire()
1630Sstevel@tonic-gate * shortly after the root cyclic's expiration time.
1640Sstevel@tonic-gate *
1650Sstevel@tonic-gate * To allow efficient, deterministic downheap operations, we implement the
1660Sstevel@tonic-gate * heap as an array (the cyp_heap member of the cyc_cpu structure), with each
1670Sstevel@tonic-gate * element containing an index into the CPU's cyp_cyclics array.
1680Sstevel@tonic-gate *
1690Sstevel@tonic-gate * The heap is laid out in the array according to the following:
1700Sstevel@tonic-gate *
1710Sstevel@tonic-gate * 1. The root of the heap is always in the 0th element of the heap array
1720Sstevel@tonic-gate * 2. The left and right children of the nth element are element
1730Sstevel@tonic-gate * (((n + 1) << 1) - 1) and element ((n + 1) << 1), respectively.
1740Sstevel@tonic-gate *
1750Sstevel@tonic-gate * This layout is standard (see, e.g., Cormen's "Algorithms"); the proof
1760Sstevel@tonic-gate * that these constraints correctly lay out a heap (or indeed, any binary
1770Sstevel@tonic-gate * tree) is trivial and left to the reader.
1780Sstevel@tonic-gate *
1790Sstevel@tonic-gate * To see the heap by example, assume our cyclics array has the following
1800Sstevel@tonic-gate * members (at time t):
1810Sstevel@tonic-gate *
1820Sstevel@tonic-gate * cy_handler cy_level cy_expire
1830Sstevel@tonic-gate * ---------------------------------------------
1840Sstevel@tonic-gate * [ 0] clock() LOCK t+10000000
1850Sstevel@tonic-gate * [ 1] deadman() HIGH t+1000000000
1860Sstevel@tonic-gate * [ 2] clock_highres_fire() LOW t+100
1870Sstevel@tonic-gate * [ 3] clock_highres_fire() LOW t+1000
1880Sstevel@tonic-gate * [ 4] clock_highres_fire() LOW t+500
1890Sstevel@tonic-gate * [ 5] (free) -- --
1900Sstevel@tonic-gate * [ 6] (free) -- --
1910Sstevel@tonic-gate * [ 7] (free) -- --
1920Sstevel@tonic-gate *
1930Sstevel@tonic-gate * The heap array could be:
1940Sstevel@tonic-gate *
1950Sstevel@tonic-gate * [0] [1] [2] [3] [4] [5] [6] [7]
1960Sstevel@tonic-gate * +-----+-----+-----+-----+-----+-----+-----+-----+
1970Sstevel@tonic-gate * | | | | | | | | |
1980Sstevel@tonic-gate * | 2 | 3 | 4 | 0 | 1 | x | x | x |
1990Sstevel@tonic-gate * | | | | | | | | |
2000Sstevel@tonic-gate * +-----+-----+-----+-----+-----+-----+-----+-----+
2010Sstevel@tonic-gate *
2020Sstevel@tonic-gate * Graphically, this array corresponds to the following (excuse the ASCII art):
2030Sstevel@tonic-gate *
2040Sstevel@tonic-gate * 2
2050Sstevel@tonic-gate * |
2060Sstevel@tonic-gate * +------------------+------------------+
2070Sstevel@tonic-gate * 3 4
2080Sstevel@tonic-gate * |
2090Sstevel@tonic-gate * +---------+--------+
2100Sstevel@tonic-gate * 0 1
2110Sstevel@tonic-gate *
2120Sstevel@tonic-gate * Note that the heap is laid out by layer: all nodes at a given depth are
2130Sstevel@tonic-gate * stored in consecutive elements of the array. Moreover, layers of
2140Sstevel@tonic-gate * consecutive depths are in adjacent element ranges. This property
2150Sstevel@tonic-gate * guarantees high locality of reference during downheap operations.
2160Sstevel@tonic-gate * Specifically, we are guaranteed that we can downheap to a depth of
2170Sstevel@tonic-gate *
2180Sstevel@tonic-gate * lg (cache_line_size / sizeof (cyc_index_t))
2190Sstevel@tonic-gate *
2200Sstevel@tonic-gate * nodes with at most one cache miss. On UltraSPARC (64 byte e-cache line
2210Sstevel@tonic-gate * size), this corresponds to a depth of four nodes. Thus, if there are
2220Sstevel@tonic-gate * fewer than sixteen cyclics in the heap, downheaps on UltraSPARC miss at
2230Sstevel@tonic-gate * most once in the e-cache.
2240Sstevel@tonic-gate *
2250Sstevel@tonic-gate * Downheaps are required to compare siblings as they proceed down the
2260Sstevel@tonic-gate * heap. For downheaps proceeding beyond the one-cache-miss depth, every
2270Sstevel@tonic-gate * access to a left child could potentially miss in the cache. However,
2280Sstevel@tonic-gate * if we assume
2290Sstevel@tonic-gate *
2300Sstevel@tonic-gate * (cache_line_size / sizeof (cyc_index_t)) > 2,
2310Sstevel@tonic-gate *
2320Sstevel@tonic-gate * then all siblings are guaranteed to be on the same cache line. Thus, the
2330Sstevel@tonic-gate * miss on the left child will guarantee a hit on the right child; downheaps
2340Sstevel@tonic-gate * will incur at most one cache miss per layer beyond the one-cache-miss
2350Sstevel@tonic-gate * depth. The total number of cache misses for heap management during a
2360Sstevel@tonic-gate * downheap operation is thus bounded by
2370Sstevel@tonic-gate *
2380Sstevel@tonic-gate * lg (n) - lg (cache_line_size / sizeof (cyc_index_t))
2390Sstevel@tonic-gate *
2400Sstevel@tonic-gate * Traditional pointer-based heaps are implemented without regard to
2410Sstevel@tonic-gate * locality. Downheaps can thus incur two cache misses per layer (one for
2420Sstevel@tonic-gate * each child), but at most one cache miss at the root. This yields a bound
2430Sstevel@tonic-gate * of
2440Sstevel@tonic-gate *
2450Sstevel@tonic-gate * 2 * lg (n) - 1
2460Sstevel@tonic-gate *
2470Sstevel@tonic-gate * on the total cache misses.
2480Sstevel@tonic-gate *
2490Sstevel@tonic-gate * This difference may seem theoretically trivial (the difference is, after
2500Sstevel@tonic-gate * all, constant), but can become substantial in practice -- especially for
2510Sstevel@tonic-gate * caches with very large cache lines and high miss penalties (e.g. TLBs).
2520Sstevel@tonic-gate *
2530Sstevel@tonic-gate * Heaps must always be full, balanced trees. Heap management must therefore
2540Sstevel@tonic-gate * track the next point-of-insertion into the heap. In pointer-based heaps,
2550Sstevel@tonic-gate * recomputing this point takes O(lg (n)). Given the layout of the
2560Sstevel@tonic-gate * array-based implementation, however, the next point-of-insertion is
2570Sstevel@tonic-gate * always:
2580Sstevel@tonic-gate *
2590Sstevel@tonic-gate * heap[number_of_elements]
2600Sstevel@tonic-gate *
2610Sstevel@tonic-gate * We exploit this property by implementing the free-list in the usused
2620Sstevel@tonic-gate * heap elements. Heap insertion, therefore, consists only of filling in
2630Sstevel@tonic-gate * the cyclic at cyp_cyclics[cyp_heap[number_of_elements]], incrementing
2640Sstevel@tonic-gate * the number of elements, and performing an upheap. Heap deletion consists
2650Sstevel@tonic-gate * of decrementing the number of elements, swapping the to-be-deleted element
2660Sstevel@tonic-gate * with the element at cyp_heap[number_of_elements], and downheaping.
2670Sstevel@tonic-gate *
2680Sstevel@tonic-gate * Filling in more details in our earlier example:
2690Sstevel@tonic-gate *
2700Sstevel@tonic-gate * +--- free list head
2710Sstevel@tonic-gate * |
2720Sstevel@tonic-gate * V
2730Sstevel@tonic-gate *
2740Sstevel@tonic-gate * [0] [1] [2] [3] [4] [5] [6] [7]
2750Sstevel@tonic-gate * +-----+-----+-----+-----+-----+-----+-----+-----+
2760Sstevel@tonic-gate * | | | | | | | | |
2770Sstevel@tonic-gate * | 2 | 3 | 4 | 0 | 1 | 5 | 6 | 7 |
2780Sstevel@tonic-gate * | | | | | | | | |
2790Sstevel@tonic-gate * +-----+-----+-----+-----+-----+-----+-----+-----+
2800Sstevel@tonic-gate *
2810Sstevel@tonic-gate * To insert into this heap, we would just need to fill in the cyclic at
2820Sstevel@tonic-gate * cyp_cyclics[5], bump the number of elements (from 5 to 6) and perform
2830Sstevel@tonic-gate * an upheap.
2840Sstevel@tonic-gate *
2850Sstevel@tonic-gate * If we wanted to remove, say, cyp_cyclics[3], we would first scan for it
2860Sstevel@tonic-gate * in the cyp_heap, and discover it at cyp_heap[1]. We would then decrement
2870Sstevel@tonic-gate * the number of elements (from 5 to 4), swap cyp_heap[1] with cyp_heap[4],
2880Sstevel@tonic-gate * and perform a downheap from cyp_heap[1]. The linear scan is required
2890Sstevel@tonic-gate * because the cyclic does not keep a backpointer into the heap. This makes
2900Sstevel@tonic-gate * heap manipulation (e.g. downheaps) faster at the expense of removal
2910Sstevel@tonic-gate * operations.
2920Sstevel@tonic-gate *
2930Sstevel@tonic-gate * Expiry processing
2940Sstevel@tonic-gate *
2950Sstevel@tonic-gate * As alluded to above, cyclic_expire() is called by cyclic_fire() at
2960Sstevel@tonic-gate * CY_HIGH_LEVEL to expire a cyclic. Cyclic subsystem consumers are
2970Sstevel@tonic-gate * guaranteed that for an arbitrary time t in the future, their cyclic
2980Sstevel@tonic-gate * handler will have been called (t - cyt_when) / cyt_interval times. Thus,
2990Sstevel@tonic-gate * there must be a one-to-one mapping between a cyclic's expiration at
3000Sstevel@tonic-gate * CY_HIGH_LEVEL and its execution at the desired level (either CY_HIGH_LEVEL,
3010Sstevel@tonic-gate * CY_LOCK_LEVEL or CY_LOW_LEVEL).
3020Sstevel@tonic-gate *
3030Sstevel@tonic-gate * For CY_HIGH_LEVEL cyclics, this is trivial; cyclic_expire() simply needs
3040Sstevel@tonic-gate * to call the handler.
3050Sstevel@tonic-gate *
3060Sstevel@tonic-gate * For CY_LOCK_LEVEL and CY_LOW_LEVEL cyclics, however, there exists a
3070Sstevel@tonic-gate * potential disconnect: if the CPU is at an interrupt level less than
3080Sstevel@tonic-gate * CY_HIGH_LEVEL but greater than the level of a cyclic for a period of
3090Sstevel@tonic-gate * time longer than twice the cyclic's interval, the cyclic will be expired
3100Sstevel@tonic-gate * twice before it can be handled.
3110Sstevel@tonic-gate *
3120Sstevel@tonic-gate * To maintain the one-to-one mapping, we track the difference between the
3130Sstevel@tonic-gate * number of times a cyclic has been expired and the number of times it's
3140Sstevel@tonic-gate * been handled in a "pending count" (the cy_pend field of the cyclic
3150Sstevel@tonic-gate * structure). cyclic_expire() thus increments the cy_pend count for the
3160Sstevel@tonic-gate * expired cyclic and posts a soft interrupt at the desired level. In the
3170Sstevel@tonic-gate * cyclic subsystem's soft interrupt handler, cyclic_softint(), we repeatedly
3180Sstevel@tonic-gate * call the cyclic handler and decrement cy_pend until we have decremented
3190Sstevel@tonic-gate * cy_pend to zero.
3200Sstevel@tonic-gate *
3210Sstevel@tonic-gate * The Producer/Consumer Buffer
3220Sstevel@tonic-gate *
3230Sstevel@tonic-gate * If we wish to avoid a linear scan of the cyclics array at soft interrupt
3240Sstevel@tonic-gate * level, cyclic_softint() must be able to quickly determine which cyclics
3250Sstevel@tonic-gate * have a non-zero cy_pend count. We thus introduce a per-soft interrupt
3260Sstevel@tonic-gate * level producer/consumer buffer shared with CY_HIGH_LEVEL. These buffers
3270Sstevel@tonic-gate * are encapsulated in the cyc_pcbuffer structure, and, like cyp_heap, are
3280Sstevel@tonic-gate * implemented as cyc_index_t arrays (the cypc_buf member of the cyc_pcbuffer
3290Sstevel@tonic-gate * structure).
3300Sstevel@tonic-gate *
3310Sstevel@tonic-gate * The producer (cyclic_expire() running at CY_HIGH_LEVEL) enqueues a cyclic
3320Sstevel@tonic-gate * by storing the cyclic's index to cypc_buf[cypc_prodndx] and incrementing
3330Sstevel@tonic-gate * cypc_prodndx. The consumer (cyclic_softint() running at either
3340Sstevel@tonic-gate * CY_LOCK_LEVEL or CY_LOW_LEVEL) dequeues a cyclic by loading from
3350Sstevel@tonic-gate * cypc_buf[cypc_consndx] and bumping cypc_consndx. The buffer is empty when
3360Sstevel@tonic-gate * cypc_prodndx == cypc_consndx.
3370Sstevel@tonic-gate *
3380Sstevel@tonic-gate * To bound the size of the producer/consumer buffer, cyclic_expire() only
3390Sstevel@tonic-gate * enqueues a cyclic if its cy_pend was zero (if the cyclic's cy_pend is
3400Sstevel@tonic-gate * non-zero, cyclic_expire() only bumps cy_pend). Symmetrically,
3410Sstevel@tonic-gate * cyclic_softint() only consumes a cyclic after it has decremented the
3420Sstevel@tonic-gate * cy_pend count to zero.
3430Sstevel@tonic-gate *
3440Sstevel@tonic-gate * Returning to our example, here is what the CY_LOW_LEVEL producer/consumer
3450Sstevel@tonic-gate * buffer might look like:
3460Sstevel@tonic-gate *
3470Sstevel@tonic-gate * cypc_consndx ---+ +--- cypc_prodndx
3480Sstevel@tonic-gate * | |
3490Sstevel@tonic-gate * V V
3500Sstevel@tonic-gate *
3510Sstevel@tonic-gate * [0] [1] [2] [3] [4] [5] [6] [7]
3520Sstevel@tonic-gate * +-----+-----+-----+-----+-----+-----+-----+-----+
3530Sstevel@tonic-gate * | | | | | | | | |
3540Sstevel@tonic-gate * | x | x | 3 | 2 | 4 | x | x | x | <== cypc_buf
3550Sstevel@tonic-gate * | | | . | . | . | | | |
3560Sstevel@tonic-gate * +-----+-----+- | -+- | -+- | -+-----+-----+-----+
3570Sstevel@tonic-gate * | | |
3580Sstevel@tonic-gate * | | | cy_pend cy_handler
3590Sstevel@tonic-gate * | | | -------------------------
3600Sstevel@tonic-gate * | | | [ 0] 1 clock()
3610Sstevel@tonic-gate * | | | [ 1] 0 deadman()
3620Sstevel@tonic-gate * | +---- | -------> [ 2] 3 clock_highres_fire()
3630Sstevel@tonic-gate * +---------- | -------> [ 3] 1 clock_highres_fire()
3640Sstevel@tonic-gate * +--------> [ 4] 1 clock_highres_fire()
3650Sstevel@tonic-gate * [ 5] - (free)
3660Sstevel@tonic-gate * [ 6] - (free)
3670Sstevel@tonic-gate * [ 7] - (free)
3680Sstevel@tonic-gate *
3690Sstevel@tonic-gate * In particular, note that clock()'s cy_pend is 1 but that it is _not_ in
3700Sstevel@tonic-gate * this producer/consumer buffer; it would be enqueued in the CY_LOCK_LEVEL
3710Sstevel@tonic-gate * producer/consumer buffer.
3720Sstevel@tonic-gate *
3730Sstevel@tonic-gate * Locking
3740Sstevel@tonic-gate *
3750Sstevel@tonic-gate * Traditionally, access to per-CPU data structures shared between
3760Sstevel@tonic-gate * interrupt levels is serialized by manipulating programmable interrupt
3770Sstevel@tonic-gate * level: readers and writers are required to raise their interrupt level
3780Sstevel@tonic-gate * to that of the highest level writer.
3790Sstevel@tonic-gate *
3800Sstevel@tonic-gate * For the producer/consumer buffers (shared between cyclic_fire()/
3810Sstevel@tonic-gate * cyclic_expire() executing at CY_HIGH_LEVEL and cyclic_softint() executing
3820Sstevel@tonic-gate * at one of CY_LOCK_LEVEL or CY_LOW_LEVEL), forcing cyclic_softint() to raise
3830Sstevel@tonic-gate * programmable interrupt level is undesirable: aside from the additional
3840Sstevel@tonic-gate * latency incurred by manipulating interrupt level in the hot cy_pend
3850Sstevel@tonic-gate * processing path, this would create the potential for soft level cy_pend
3860Sstevel@tonic-gate * processing to delay CY_HIGH_LEVEL firing and expiry processing.
3870Sstevel@tonic-gate * CY_LOCK/LOW_LEVEL cyclics could thereby induce jitter in CY_HIGH_LEVEL
3880Sstevel@tonic-gate * cyclics.
3890Sstevel@tonic-gate *
3900Sstevel@tonic-gate * To minimize jitter, then, we would like the cyclic_fire()/cyclic_expire()
3910Sstevel@tonic-gate * and cyclic_softint() code paths to be lock-free.
3920Sstevel@tonic-gate *
3930Sstevel@tonic-gate * For cyclic_fire()/cyclic_expire(), lock-free execution is straightforward:
3940Sstevel@tonic-gate * because these routines execute at a higher interrupt level than
3950Sstevel@tonic-gate * cyclic_softint(), their actions on the producer/consumer buffer appear
3960Sstevel@tonic-gate * atomic. In particular, the increment of cy_pend appears to occur
3970Sstevel@tonic-gate * atomically with the increment of cypc_prodndx.
3980Sstevel@tonic-gate *
3990Sstevel@tonic-gate * For cyclic_softint(), however, lock-free execution requires more delicacy.
4000Sstevel@tonic-gate * When cyclic_softint() discovers a cyclic in the producer/consumer buffer,
4010Sstevel@tonic-gate * it calls the cyclic's handler and attempts to atomically decrement the
4020Sstevel@tonic-gate * cy_pend count with a compare&swap operation.
4030Sstevel@tonic-gate *
4040Sstevel@tonic-gate * If the compare&swap operation succeeds, cyclic_softint() behaves
4050Sstevel@tonic-gate * conditionally based on the value it atomically wrote to cy_pend:
4060Sstevel@tonic-gate *
4070Sstevel@tonic-gate * - If the cy_pend was decremented to 0, the cyclic has been consumed;
4080Sstevel@tonic-gate * cyclic_softint() increments the cypc_consndx and checks for more
4090Sstevel@tonic-gate * enqueued work.
4100Sstevel@tonic-gate *
4110Sstevel@tonic-gate * - If the count was decremented to a non-zero value, there is more work
4120Sstevel@tonic-gate * to be done on the cyclic; cyclic_softint() calls the cyclic handler
4130Sstevel@tonic-gate * and repeats the atomic decrement process.
4140Sstevel@tonic-gate *
4150Sstevel@tonic-gate * If the compare&swap operation fails, cyclic_softint() knows that
4160Sstevel@tonic-gate * cyclic_expire() has intervened and bumped the cy_pend count (resizes
4170Sstevel@tonic-gate * and removals complicate this, however -- see the sections on their
4180Sstevel@tonic-gate * operation, below). cyclic_softint() thus reloads cy_pend, and re-attempts
4190Sstevel@tonic-gate * the atomic decrement.
4200Sstevel@tonic-gate *
4210Sstevel@tonic-gate * Recall that we bound the size of the producer/consumer buffer by
4220Sstevel@tonic-gate * having cyclic_expire() only enqueue the specified cyclic if its
4230Sstevel@tonic-gate * cy_pend count is zero; this assures that each cyclic is enqueued at
4240Sstevel@tonic-gate * most once. This leads to a critical constraint on cyclic_softint(),
4250Sstevel@tonic-gate * however: after the compare&swap operation which successfully decrements
4260Sstevel@tonic-gate * cy_pend to zero, cyclic_softint() must _not_ re-examine the consumed
4270Sstevel@tonic-gate * cyclic. In part to obey this constraint, cyclic_softint() calls the
4280Sstevel@tonic-gate * cyclic handler before decrementing cy_pend.
4290Sstevel@tonic-gate *
4300Sstevel@tonic-gate * Resizing
4310Sstevel@tonic-gate *
4320Sstevel@tonic-gate * All of the discussion thus far has assumed a static number of cyclics.
4330Sstevel@tonic-gate * Obviously, static limitations are not practical; we need the capacity
4340Sstevel@tonic-gate * to resize our data structures dynamically.
4350Sstevel@tonic-gate *
4360Sstevel@tonic-gate * We resize our data structures lazily, and only on a per-CPU basis.
4370Sstevel@tonic-gate * The size of the data structures always doubles and never shrinks. We
4380Sstevel@tonic-gate * serialize adds (and thus resizes) on cpu_lock; we never need to deal
4390Sstevel@tonic-gate * with concurrent resizes. Resizes should be rare; they may induce jitter
4400Sstevel@tonic-gate * on the CPU being resized, but should not affect cyclic operation on other
4410Sstevel@tonic-gate * CPUs. Pending cyclics may not be dropped during a resize operation.
4420Sstevel@tonic-gate *
4430Sstevel@tonic-gate * Three key cyc_cpu data structures need to be resized: the cyclics array,
4440Sstevel@tonic-gate * the heap array and the producer/consumer buffers. Resizing the first two
4450Sstevel@tonic-gate * is relatively straightforward:
4460Sstevel@tonic-gate *
4470Sstevel@tonic-gate * 1. The new, larger arrays are allocated in cyclic_expand() (called
4480Sstevel@tonic-gate * from cyclic_add()).
4490Sstevel@tonic-gate * 2. cyclic_expand() cross calls cyclic_expand_xcall() on the CPU
4500Sstevel@tonic-gate * undergoing the resize.
4510Sstevel@tonic-gate * 3. cyclic_expand_xcall() raises interrupt level to CY_HIGH_LEVEL
4520Sstevel@tonic-gate * 4. The contents of the old arrays are copied into the new arrays.
4530Sstevel@tonic-gate * 5. The old cyclics array is bzero()'d
4540Sstevel@tonic-gate * 6. The pointers are updated.
4550Sstevel@tonic-gate *
4560Sstevel@tonic-gate * The producer/consumer buffer is dicier: cyclic_expand_xcall() may have
4570Sstevel@tonic-gate * interrupted cyclic_softint() in the middle of consumption. To resize the
4580Sstevel@tonic-gate * producer/consumer buffer, we implement up to two buffers per soft interrupt
4590Sstevel@tonic-gate * level: a hard buffer (the buffer being produced into by cyclic_expire())
4600Sstevel@tonic-gate * and a soft buffer (the buffer from which cyclic_softint() is consuming).
4610Sstevel@tonic-gate * During normal operation, the hard buffer and soft buffer point to the
4620Sstevel@tonic-gate * same underlying producer/consumer buffer.
4630Sstevel@tonic-gate *
4640Sstevel@tonic-gate * During a resize, however, cyclic_expand_xcall() changes the hard buffer
4650Sstevel@tonic-gate * to point to the new, larger producer/consumer buffer; all future
4660Sstevel@tonic-gate * cyclic_expire()'s will produce into the new buffer. cyclic_expand_xcall()
4670Sstevel@tonic-gate * then posts a CY_LOCK_LEVEL soft interrupt, landing in cyclic_softint().
4680Sstevel@tonic-gate *
4690Sstevel@tonic-gate * As under normal operation, cyclic_softint() will consume cyclics from
4700Sstevel@tonic-gate * its soft buffer. After the soft buffer is drained, however,
4710Sstevel@tonic-gate * cyclic_softint() will see that the hard buffer has changed. At that time,
4720Sstevel@tonic-gate * cyclic_softint() will change its soft buffer to point to the hard buffer,
4730Sstevel@tonic-gate * and repeat the producer/consumer buffer draining procedure.
4740Sstevel@tonic-gate *
4750Sstevel@tonic-gate * After the new buffer is drained, cyclic_softint() will determine if both
4760Sstevel@tonic-gate * soft levels have seen their new producer/consumer buffer. If both have,
4770Sstevel@tonic-gate * cyclic_softint() will post on the semaphore cyp_modify_wait. If not, a
4780Sstevel@tonic-gate * soft interrupt will be generated for the remaining level.
4790Sstevel@tonic-gate *
4800Sstevel@tonic-gate * cyclic_expand() blocks on the cyp_modify_wait semaphore (a semaphore is
4810Sstevel@tonic-gate * used instead of a condition variable because of the race between the
4820Sstevel@tonic-gate * sema_p() in cyclic_expand() and the sema_v() in cyclic_softint()). This
4830Sstevel@tonic-gate * allows cyclic_expand() to know when the resize operation is complete;
4840Sstevel@tonic-gate * all of the old buffers (the heap, the cyclics array and the producer/
4850Sstevel@tonic-gate * consumer buffers) can be freed.
4860Sstevel@tonic-gate *
4870Sstevel@tonic-gate * A final caveat on resizing: we described step (5) in the
4880Sstevel@tonic-gate * cyclic_expand_xcall() procedure without providing any motivation. This
4890Sstevel@tonic-gate * step addresses the problem of a cyclic_softint() attempting to decrement
4900Sstevel@tonic-gate * a cy_pend count while interrupted by a cyclic_expand_xcall(). Because
4910Sstevel@tonic-gate * cyclic_softint() has already called the handler by the time cy_pend is
4920Sstevel@tonic-gate * decremented, we want to assure that it doesn't decrement a cy_pend
4930Sstevel@tonic-gate * count in the old cyclics array. By zeroing the old cyclics array in
4940Sstevel@tonic-gate * cyclic_expand_xcall(), we are zeroing out every cy_pend count; when
4950Sstevel@tonic-gate * cyclic_softint() attempts to compare&swap on the cy_pend count, it will
4960Sstevel@tonic-gate * fail and recognize that the count has been zeroed. cyclic_softint() will
4970Sstevel@tonic-gate * update its stale copy of the cyp_cyclics pointer, re-read the cy_pend
4980Sstevel@tonic-gate * count from the new cyclics array, and re-attempt the compare&swap.
4990Sstevel@tonic-gate *
5000Sstevel@tonic-gate * Removals
5010Sstevel@tonic-gate *
5020Sstevel@tonic-gate * Cyclic removals should be rare. To simplify the implementation (and to
5030Sstevel@tonic-gate * allow optimization for the cyclic_fire()/cyclic_expire()/cyclic_softint()
5040Sstevel@tonic-gate * path), we force removals and adds to serialize on cpu_lock.
5050Sstevel@tonic-gate *
5060Sstevel@tonic-gate * Cyclic removal is complicated by a guarantee made to the consumer of
5070Sstevel@tonic-gate * the cyclic subsystem: after cyclic_remove() returns, the cyclic handler
5080Sstevel@tonic-gate * has returned and will never again be called.
5090Sstevel@tonic-gate *
5100Sstevel@tonic-gate * Here is the procedure for cyclic removal:
5110Sstevel@tonic-gate *
5120Sstevel@tonic-gate * 1. cyclic_remove() calls cyclic_remove_xcall() on the CPU undergoing
5130Sstevel@tonic-gate * the removal.
5140Sstevel@tonic-gate * 2. cyclic_remove_xcall() raises interrupt level to CY_HIGH_LEVEL
5150Sstevel@tonic-gate * 3. The current expiration time for the removed cyclic is recorded.
5160Sstevel@tonic-gate * 4. If the cy_pend count on the removed cyclic is non-zero, it
5170Sstevel@tonic-gate * is copied into cyp_rpend and subsequently zeroed.
5180Sstevel@tonic-gate * 5. The cyclic is removed from the heap
5190Sstevel@tonic-gate * 6. If the root of the heap has changed, the backend is reprogrammed.
5200Sstevel@tonic-gate * 7. If the cy_pend count was non-zero cyclic_remove() blocks on the
5210Sstevel@tonic-gate * cyp_modify_wait semaphore.
5220Sstevel@tonic-gate *
5230Sstevel@tonic-gate * The motivation for step (3) is explained in "Juggling", below.
5240Sstevel@tonic-gate *
5250Sstevel@tonic-gate * The cy_pend count is decremented in cyclic_softint() after the cyclic
5260Sstevel@tonic-gate * handler returns. Thus, if we find a cy_pend count of zero in step
5270Sstevel@tonic-gate * (4), we know that cyclic_remove() doesn't need to block.
5280Sstevel@tonic-gate *
5290Sstevel@tonic-gate * If the cy_pend count is non-zero, however, we must block in cyclic_remove()
5300Sstevel@tonic-gate * until cyclic_softint() has finished calling the cyclic handler. To let
5310Sstevel@tonic-gate * cyclic_softint() know that this cyclic has been removed, we zero the
5320Sstevel@tonic-gate * cy_pend count. This will cause cyclic_softint()'s compare&swap to fail.
5330Sstevel@tonic-gate * When cyclic_softint() sees the zero cy_pend count, it knows that it's been
5340Sstevel@tonic-gate * caught during a resize (see "Resizing", above) or that the cyclic has been
5350Sstevel@tonic-gate * removed. In the latter case, it calls cyclic_remove_pend() to call the
5360Sstevel@tonic-gate * cyclic handler cyp_rpend - 1 times, and posts on cyp_modify_wait.
5370Sstevel@tonic-gate *
5380Sstevel@tonic-gate * Juggling
5390Sstevel@tonic-gate *
5400Sstevel@tonic-gate * At first glance, cyclic juggling seems to be a difficult problem. The
5410Sstevel@tonic-gate * subsystem must guarantee that a cyclic doesn't execute simultaneously on
5420Sstevel@tonic-gate * different CPUs, while also assuring that a cyclic fires exactly once
5430Sstevel@tonic-gate * per interval. We solve this problem by leveraging a property of the
5440Sstevel@tonic-gate * platform: gethrtime() is required to increase in lock-step across
5450Sstevel@tonic-gate * multiple CPUs. Therefore, to juggle a cyclic, we remove it from its
5460Sstevel@tonic-gate * CPU, recording its expiration time in the remove cross call (step (3)
5470Sstevel@tonic-gate * in "Removing", above). We then add the cyclic to the new CPU, explicitly
5480Sstevel@tonic-gate * setting its expiration time to the time recorded in the removal. This
5490Sstevel@tonic-gate * leverages the existing cyclic expiry processing, which will compensate
5500Sstevel@tonic-gate * for any time lost while juggling.
5510Sstevel@tonic-gate *
552*8048SMadhavan.Venkataraman@Sun.COM * Reprogramming
553*8048SMadhavan.Venkataraman@Sun.COM *
554*8048SMadhavan.Venkataraman@Sun.COM * Normally, after a cyclic fires, its next expiration is computed from
555*8048SMadhavan.Venkataraman@Sun.COM * the current time and the cyclic interval. But there are situations when
556*8048SMadhavan.Venkataraman@Sun.COM * the next expiration needs to be reprogrammed by the kernel subsystem that
557*8048SMadhavan.Venkataraman@Sun.COM * is using the cyclic. cyclic_reprogram() allows this to be done. This,
558*8048SMadhavan.Venkataraman@Sun.COM * unlike the other kernel at-large cyclic API functions, is permitted to
559*8048SMadhavan.Venkataraman@Sun.COM * be called from the cyclic handler. This is because it does not use the
560*8048SMadhavan.Venkataraman@Sun.COM * cpu_lock to serialize access.
561*8048SMadhavan.Venkataraman@Sun.COM *
562*8048SMadhavan.Venkataraman@Sun.COM * When cyclic_reprogram() is called for an omni-cyclic, the operation is
563*8048SMadhavan.Venkataraman@Sun.COM * applied to the omni-cyclic's component on the current CPU.
564*8048SMadhavan.Venkataraman@Sun.COM *
565*8048SMadhavan.Venkataraman@Sun.COM * If a high-level cyclic handler reprograms its own cyclic, then
566*8048SMadhavan.Venkataraman@Sun.COM * cyclic_fire() detects that and does not recompute the cyclic's next
567*8048SMadhavan.Venkataraman@Sun.COM * expiration. However, for a lock-level or a low-level cyclic, the
568*8048SMadhavan.Venkataraman@Sun.COM * actual cyclic handler will execute at the lower PIL only after
569*8048SMadhavan.Venkataraman@Sun.COM * cyclic_fire() is done with all expired cyclics. To deal with this, such
570*8048SMadhavan.Venkataraman@Sun.COM * cyclics can be specified with a special interval of CY_INFINITY (INT64_MAX).
571*8048SMadhavan.Venkataraman@Sun.COM * cyclic_fire() recognizes this special value and recomputes the next
572*8048SMadhavan.Venkataraman@Sun.COM * expiration to CY_INFINITY. This effectively moves the cyclic to the
573*8048SMadhavan.Venkataraman@Sun.COM * bottom of the heap and prevents it from going off until its handler has
574*8048SMadhavan.Venkataraman@Sun.COM * had a chance to reprogram it. Infact, this is the way to create and reuse
575*8048SMadhavan.Venkataraman@Sun.COM * "one-shot" timers in the context of the cyclic subsystem without using
576*8048SMadhavan.Venkataraman@Sun.COM * cyclic_remove().
577*8048SMadhavan.Venkataraman@Sun.COM *
578*8048SMadhavan.Venkataraman@Sun.COM * Here is the procedure for cyclic reprogramming:
579*8048SMadhavan.Venkataraman@Sun.COM *
580*8048SMadhavan.Venkataraman@Sun.COM * 1. cyclic_reprogram() calls cyclic_reprogram_xcall() on the CPU
581*8048SMadhavan.Venkataraman@Sun.COM * that houses the cyclic.
582*8048SMadhavan.Venkataraman@Sun.COM * 2. cyclic_reprogram_xcall() raises interrupt level to CY_HIGH_LEVEL
583*8048SMadhavan.Venkataraman@Sun.COM * 3. The cyclic is located in the cyclic heap. The search for this is
584*8048SMadhavan.Venkataraman@Sun.COM * done from the bottom of the heap to the top as reprogrammable cyclics
585*8048SMadhavan.Venkataraman@Sun.COM * would be located closer to the bottom than the top.
586*8048SMadhavan.Venkataraman@Sun.COM * 4. The cyclic expiration is set and the cyclic is moved to its
587*8048SMadhavan.Venkataraman@Sun.COM * correct position in the heap (up or down depending on whether the
588*8048SMadhavan.Venkataraman@Sun.COM * new expiration is less than or greater than the old one).
589*8048SMadhavan.Venkataraman@Sun.COM * 5. If the cyclic move modified the root of the heap, the backend is
590*8048SMadhavan.Venkataraman@Sun.COM * reprogrammed.
591*8048SMadhavan.Venkataraman@Sun.COM *
592*8048SMadhavan.Venkataraman@Sun.COM * Reprogramming can be a frequent event (see the callout subsystem). So,
593*8048SMadhavan.Venkataraman@Sun.COM * the serialization used has to be efficient. As with all other cyclic
594*8048SMadhavan.Venkataraman@Sun.COM * operations, the interrupt level is raised during reprogramming. Plus,
595*8048SMadhavan.Venkataraman@Sun.COM * during reprogramming, the cyclic must not be juggled (regular cyclic)
596*8048SMadhavan.Venkataraman@Sun.COM * or stopped (omni-cyclic). The implementation defines a per-cyclic
597*8048SMadhavan.Venkataraman@Sun.COM * reader-writer lock to accomplish this. This lock is acquired in the
598*8048SMadhavan.Venkataraman@Sun.COM * reader mode by cyclic_reprogram() and writer mode by cyclic_juggle() and
599*8048SMadhavan.Venkataraman@Sun.COM * cyclic_omni_stop(). The reader-writer lock makes it efficient if
600*8048SMadhavan.Venkataraman@Sun.COM * an omni-cyclic is reprogrammed on different CPUs frequently.
601*8048SMadhavan.Venkataraman@Sun.COM *
602*8048SMadhavan.Venkataraman@Sun.COM * Note that since the cpu_lock is not used during reprogramming, it is
603*8048SMadhavan.Venkataraman@Sun.COM * the responsibility of the user of the reprogrammable cyclic to make sure
604*8048SMadhavan.Venkataraman@Sun.COM * that the cyclic is not removed via cyclic_remove() during reprogramming.
605*8048SMadhavan.Venkataraman@Sun.COM * This is not an unreasonable requirement as the user will typically have
606*8048SMadhavan.Venkataraman@Sun.COM * some sort of synchronization for its cyclic-related activities. This
607*8048SMadhavan.Venkataraman@Sun.COM * little caveat exists because the cyclic ID is not really an ID. It is
608*8048SMadhavan.Venkataraman@Sun.COM * implemented as a pointer to a structure.
6090Sstevel@tonic-gate */
6100Sstevel@tonic-gate #include <sys/cyclic_impl.h>
6110Sstevel@tonic-gate #include <sys/sysmacros.h>
6120Sstevel@tonic-gate #include <sys/systm.h>
6130Sstevel@tonic-gate #include <sys/atomic.h>
6140Sstevel@tonic-gate #include <sys/kmem.h>
6150Sstevel@tonic-gate #include <sys/cmn_err.h>
6160Sstevel@tonic-gate #include <sys/ddi.h>
6175864Sesaxe #include <sys/sdt.h>
6180Sstevel@tonic-gate
6190Sstevel@tonic-gate #ifdef CYCLIC_TRACE
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate /*
6220Sstevel@tonic-gate * cyc_trace_enabled is for the benefit of kernel debuggers.
6230Sstevel@tonic-gate */
6240Sstevel@tonic-gate int cyc_trace_enabled = 1;
6250Sstevel@tonic-gate static cyc_tracebuf_t cyc_ptrace;
6260Sstevel@tonic-gate static cyc_coverage_t cyc_coverage[CY_NCOVERAGE];
6270Sstevel@tonic-gate
6280Sstevel@tonic-gate /*
6290Sstevel@tonic-gate * Seen this anywhere?
6300Sstevel@tonic-gate */
6310Sstevel@tonic-gate static uint_t
cyclic_coverage_hash(char * p)6320Sstevel@tonic-gate cyclic_coverage_hash(char *p)
6330Sstevel@tonic-gate {
6340Sstevel@tonic-gate unsigned int g;
6350Sstevel@tonic-gate uint_t hval;
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate hval = 0;
6380Sstevel@tonic-gate while (*p) {
6390Sstevel@tonic-gate hval = (hval << 4) + *p++;
6400Sstevel@tonic-gate if ((g = (hval & 0xf0000000)) != 0)
6410Sstevel@tonic-gate hval ^= g >> 24;
6420Sstevel@tonic-gate hval &= ~g;
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate return (hval);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate static void
cyclic_coverage(char * why,int level,uint64_t arg0,uint64_t arg1)6480Sstevel@tonic-gate cyclic_coverage(char *why, int level, uint64_t arg0, uint64_t arg1)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate uint_t ndx, orig;
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate for (ndx = orig = cyclic_coverage_hash(why) % CY_NCOVERAGE; ; ) {
6530Sstevel@tonic-gate if (cyc_coverage[ndx].cyv_why == why)
6540Sstevel@tonic-gate break;
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate if (cyc_coverage[ndx].cyv_why != NULL ||
6570Sstevel@tonic-gate casptr(&cyc_coverage[ndx].cyv_why, NULL, why) != NULL) {
6580Sstevel@tonic-gate
6590Sstevel@tonic-gate if (++ndx == CY_NCOVERAGE)
6600Sstevel@tonic-gate ndx = 0;
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate if (ndx == orig)
6630Sstevel@tonic-gate panic("too many cyclic coverage points");
6640Sstevel@tonic-gate continue;
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate /*
6680Sstevel@tonic-gate * If we're here, we have successfully swung our guy into
6690Sstevel@tonic-gate * the position at "ndx".
6700Sstevel@tonic-gate */
6710Sstevel@tonic-gate break;
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate if (level == CY_PASSIVE_LEVEL)
6750Sstevel@tonic-gate cyc_coverage[ndx].cyv_passive_count++;
6760Sstevel@tonic-gate else
6770Sstevel@tonic-gate cyc_coverage[ndx].cyv_count[level]++;
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate cyc_coverage[ndx].cyv_arg0 = arg0;
6800Sstevel@tonic-gate cyc_coverage[ndx].cyv_arg1 = arg1;
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate #define CYC_TRACE(cpu, level, why, arg0, arg1) \
6840Sstevel@tonic-gate CYC_TRACE_IMPL(&cpu->cyp_trace[level], level, why, arg0, arg1)
6850Sstevel@tonic-gate
6860Sstevel@tonic-gate #define CYC_PTRACE(why, arg0, arg1) \
6870Sstevel@tonic-gate CYC_TRACE_IMPL(&cyc_ptrace, CY_PASSIVE_LEVEL, why, arg0, arg1)
6880Sstevel@tonic-gate
6890Sstevel@tonic-gate #define CYC_TRACE_IMPL(buf, level, why, a0, a1) { \
6900Sstevel@tonic-gate if (panicstr == NULL) { \
6910Sstevel@tonic-gate int _ndx = (buf)->cyt_ndx; \
6920Sstevel@tonic-gate cyc_tracerec_t *_rec = &(buf)->cyt_buf[_ndx]; \
6930Sstevel@tonic-gate (buf)->cyt_ndx = (++_ndx == CY_NTRACEREC) ? 0 : _ndx; \
6940Sstevel@tonic-gate _rec->cyt_tstamp = gethrtime_unscaled(); \
6950Sstevel@tonic-gate _rec->cyt_why = (why); \
6960Sstevel@tonic-gate _rec->cyt_arg0 = (uint64_t)(uintptr_t)(a0); \
6970Sstevel@tonic-gate _rec->cyt_arg1 = (uint64_t)(uintptr_t)(a1); \
6980Sstevel@tonic-gate cyclic_coverage(why, level, \
6990Sstevel@tonic-gate (uint64_t)(uintptr_t)(a0), (uint64_t)(uintptr_t)(a1)); \
7000Sstevel@tonic-gate } \
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate
7030Sstevel@tonic-gate #else
7040Sstevel@tonic-gate
7050Sstevel@tonic-gate static int cyc_trace_enabled = 0;
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate #define CYC_TRACE(cpu, level, why, arg0, arg1)
7080Sstevel@tonic-gate #define CYC_PTRACE(why, arg0, arg1)
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate #endif
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate #define CYC_TRACE0(cpu, level, why) CYC_TRACE(cpu, level, why, 0, 0)
7130Sstevel@tonic-gate #define CYC_TRACE1(cpu, level, why, arg0) CYC_TRACE(cpu, level, why, arg0, 0)
7140Sstevel@tonic-gate
7150Sstevel@tonic-gate #define CYC_PTRACE0(why) CYC_PTRACE(why, 0, 0)
7160Sstevel@tonic-gate #define CYC_PTRACE1(why, arg0) CYC_PTRACE(why, arg0, 0)
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate static kmem_cache_t *cyclic_id_cache;
7190Sstevel@tonic-gate static cyc_id_t *cyclic_id_head;
7200Sstevel@tonic-gate static hrtime_t cyclic_resolution;
7210Sstevel@tonic-gate static cyc_backend_t cyclic_backend;
7220Sstevel@tonic-gate
7230Sstevel@tonic-gate /*
7240Sstevel@tonic-gate * Returns 1 if the upheap propagated to the root, 0 if it did not. This
7250Sstevel@tonic-gate * allows the caller to reprogram the backend only when the root has been
7260Sstevel@tonic-gate * modified.
7270Sstevel@tonic-gate */
7280Sstevel@tonic-gate static int
cyclic_upheap(cyc_cpu_t * cpu,cyc_index_t ndx)7290Sstevel@tonic-gate cyclic_upheap(cyc_cpu_t *cpu, cyc_index_t ndx)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate cyclic_t *cyclics;
7320Sstevel@tonic-gate cyc_index_t *heap;
7330Sstevel@tonic-gate cyc_index_t heap_parent, heap_current = ndx;
7340Sstevel@tonic-gate cyc_index_t parent, current;
7350Sstevel@tonic-gate
7360Sstevel@tonic-gate if (heap_current == 0)
7370Sstevel@tonic-gate return (1);
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate heap = cpu->cyp_heap;
7400Sstevel@tonic-gate cyclics = cpu->cyp_cyclics;
7410Sstevel@tonic-gate heap_parent = CYC_HEAP_PARENT(heap_current);
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate for (;;) {
7440Sstevel@tonic-gate current = heap[heap_current];
7450Sstevel@tonic-gate parent = heap[heap_parent];
7460Sstevel@tonic-gate
7470Sstevel@tonic-gate /*
7480Sstevel@tonic-gate * We have an expiration time later than our parent; we're
7490Sstevel@tonic-gate * done.
7500Sstevel@tonic-gate */
7510Sstevel@tonic-gate if (cyclics[current].cy_expire >= cyclics[parent].cy_expire)
7520Sstevel@tonic-gate return (0);
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate /*
7550Sstevel@tonic-gate * We need to swap with our parent, and continue up the heap.
7560Sstevel@tonic-gate */
7570Sstevel@tonic-gate heap[heap_parent] = current;
7580Sstevel@tonic-gate heap[heap_current] = parent;
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate /*
7610Sstevel@tonic-gate * If we just reached the root, we're done.
7620Sstevel@tonic-gate */
7630Sstevel@tonic-gate if (heap_parent == 0)
7640Sstevel@tonic-gate return (1);
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate heap_current = heap_parent;
7670Sstevel@tonic-gate heap_parent = CYC_HEAP_PARENT(heap_current);
7680Sstevel@tonic-gate }
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate
7710Sstevel@tonic-gate static void
cyclic_downheap(cyc_cpu_t * cpu,cyc_index_t ndx)7720Sstevel@tonic-gate cyclic_downheap(cyc_cpu_t *cpu, cyc_index_t ndx)
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate cyclic_t *cyclics = cpu->cyp_cyclics;
7750Sstevel@tonic-gate cyc_index_t *heap = cpu->cyp_heap;
7760Sstevel@tonic-gate
7770Sstevel@tonic-gate cyc_index_t heap_left, heap_right, heap_me = ndx;
7780Sstevel@tonic-gate cyc_index_t left, right, me;
7790Sstevel@tonic-gate cyc_index_t nelems = cpu->cyp_nelems;
7800Sstevel@tonic-gate
7810Sstevel@tonic-gate for (;;) {
7820Sstevel@tonic-gate /*
7830Sstevel@tonic-gate * If we don't have a left child (i.e., we're a leaf), we're
7840Sstevel@tonic-gate * done.
7850Sstevel@tonic-gate */
7860Sstevel@tonic-gate if ((heap_left = CYC_HEAP_LEFT(heap_me)) >= nelems)
7870Sstevel@tonic-gate return;
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate left = heap[heap_left];
7900Sstevel@tonic-gate me = heap[heap_me];
7910Sstevel@tonic-gate
7920Sstevel@tonic-gate heap_right = CYC_HEAP_RIGHT(heap_me);
7930Sstevel@tonic-gate
7940Sstevel@tonic-gate /*
7950Sstevel@tonic-gate * Even if we don't have a right child, we still need to compare
7960Sstevel@tonic-gate * our expiration time against that of our left child.
7970Sstevel@tonic-gate */
7980Sstevel@tonic-gate if (heap_right >= nelems)
7990Sstevel@tonic-gate goto comp_left;
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate right = heap[heap_right];
8020Sstevel@tonic-gate
8030Sstevel@tonic-gate /*
8040Sstevel@tonic-gate * We have both a left and a right child. We need to compare
8050Sstevel@tonic-gate * the expiration times of the children to determine which
8060Sstevel@tonic-gate * expires earlier.
8070Sstevel@tonic-gate */
8080Sstevel@tonic-gate if (cyclics[right].cy_expire < cyclics[left].cy_expire) {
8090Sstevel@tonic-gate /*
8100Sstevel@tonic-gate * Our right child is the earlier of our children.
8110Sstevel@tonic-gate * We'll now compare our expiration time to its; if
8120Sstevel@tonic-gate * ours is the earlier, we're done.
8130Sstevel@tonic-gate */
8140Sstevel@tonic-gate if (cyclics[me].cy_expire <= cyclics[right].cy_expire)
8150Sstevel@tonic-gate return;
8160Sstevel@tonic-gate
8170Sstevel@tonic-gate /*
8180Sstevel@tonic-gate * Our right child expires earlier than we do; swap
8190Sstevel@tonic-gate * with our right child, and descend right.
8200Sstevel@tonic-gate */
8210Sstevel@tonic-gate heap[heap_right] = me;
8220Sstevel@tonic-gate heap[heap_me] = right;
8230Sstevel@tonic-gate heap_me = heap_right;
8240Sstevel@tonic-gate continue;
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate
8270Sstevel@tonic-gate comp_left:
8280Sstevel@tonic-gate /*
8290Sstevel@tonic-gate * Our left child is the earlier of our children (or we have
8300Sstevel@tonic-gate * no right child). We'll now compare our expiration time
8310Sstevel@tonic-gate * to its; if ours is the earlier, we're done.
8320Sstevel@tonic-gate */
8330Sstevel@tonic-gate if (cyclics[me].cy_expire <= cyclics[left].cy_expire)
8340Sstevel@tonic-gate return;
8350Sstevel@tonic-gate
8360Sstevel@tonic-gate /*
8370Sstevel@tonic-gate * Our left child expires earlier than we do; swap with our
8380Sstevel@tonic-gate * left child, and descend left.
8390Sstevel@tonic-gate */
8400Sstevel@tonic-gate heap[heap_left] = me;
8410Sstevel@tonic-gate heap[heap_me] = left;
8420Sstevel@tonic-gate heap_me = heap_left;
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate
8460Sstevel@tonic-gate static void
cyclic_expire(cyc_cpu_t * cpu,cyc_index_t ndx,cyclic_t * cyclic)8470Sstevel@tonic-gate cyclic_expire(cyc_cpu_t *cpu, cyc_index_t ndx, cyclic_t *cyclic)
8480Sstevel@tonic-gate {
8490Sstevel@tonic-gate cyc_backend_t *be = cpu->cyp_backend;
8500Sstevel@tonic-gate cyc_level_t level = cyclic->cy_level;
8510Sstevel@tonic-gate
8520Sstevel@tonic-gate /*
8530Sstevel@tonic-gate * If this is a CY_HIGH_LEVEL cyclic, just call the handler; we don't
8540Sstevel@tonic-gate * need to worry about the pend count for CY_HIGH_LEVEL cyclics.
8550Sstevel@tonic-gate */
8560Sstevel@tonic-gate if (level == CY_HIGH_LEVEL) {
8570Sstevel@tonic-gate cyc_func_t handler = cyclic->cy_handler;
8580Sstevel@tonic-gate void *arg = cyclic->cy_arg;
8590Sstevel@tonic-gate
8600Sstevel@tonic-gate CYC_TRACE(cpu, CY_HIGH_LEVEL, "handler-in", handler, arg);
8615864Sesaxe DTRACE_PROBE1(cyclic__start, cyclic_t *, cyclic);
8625864Sesaxe
8630Sstevel@tonic-gate (*handler)(arg);
8645864Sesaxe
8655864Sesaxe DTRACE_PROBE1(cyclic__end, cyclic_t *, cyclic);
8660Sstevel@tonic-gate CYC_TRACE(cpu, CY_HIGH_LEVEL, "handler-out", handler, arg);
8670Sstevel@tonic-gate
8680Sstevel@tonic-gate return;
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate
8710Sstevel@tonic-gate /*
8720Sstevel@tonic-gate * We're at CY_HIGH_LEVEL; this modification to cy_pend need not
8730Sstevel@tonic-gate * be atomic (the high interrupt level assures that it will appear
8740Sstevel@tonic-gate * atomic to any softint currently running).
8750Sstevel@tonic-gate */
8760Sstevel@tonic-gate if (cyclic->cy_pend++ == 0) {
8770Sstevel@tonic-gate cyc_softbuf_t *softbuf = &cpu->cyp_softbuf[level];
8780Sstevel@tonic-gate cyc_pcbuffer_t *pc = &softbuf->cys_buf[softbuf->cys_hard];
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate /*
8810Sstevel@tonic-gate * We need to enqueue this cyclic in the soft buffer.
8820Sstevel@tonic-gate */
8830Sstevel@tonic-gate CYC_TRACE(cpu, CY_HIGH_LEVEL, "expire-enq", cyclic,
8840Sstevel@tonic-gate pc->cypc_prodndx);
8850Sstevel@tonic-gate pc->cypc_buf[pc->cypc_prodndx++ & pc->cypc_sizemask] = ndx;
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate ASSERT(pc->cypc_prodndx != pc->cypc_consndx);
8880Sstevel@tonic-gate } else {
8890Sstevel@tonic-gate /*
8900Sstevel@tonic-gate * If the pend count is zero after we incremented it, then
8910Sstevel@tonic-gate * we've wrapped (i.e. we had a cy_pend count of over four
8920Sstevel@tonic-gate * billion. In this case, we clamp the pend count at
8930Sstevel@tonic-gate * UINT32_MAX. Yes, cyclics can be lost in this case.
8940Sstevel@tonic-gate */
8950Sstevel@tonic-gate if (cyclic->cy_pend == 0) {
8960Sstevel@tonic-gate CYC_TRACE1(cpu, CY_HIGH_LEVEL, "expire-wrap", cyclic);
8970Sstevel@tonic-gate cyclic->cy_pend = UINT32_MAX;
8980Sstevel@tonic-gate }
8990Sstevel@tonic-gate
9000Sstevel@tonic-gate CYC_TRACE(cpu, CY_HIGH_LEVEL, "expire-bump", cyclic, 0);
9010Sstevel@tonic-gate }
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate be->cyb_softint(be->cyb_arg, cyclic->cy_level);
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate
9060Sstevel@tonic-gate /*
9070Sstevel@tonic-gate * cyclic_fire(cpu_t *)
9080Sstevel@tonic-gate *
9090Sstevel@tonic-gate * Overview
9100Sstevel@tonic-gate *
9110Sstevel@tonic-gate * cyclic_fire() is the cyclic subsystem's CY_HIGH_LEVEL interrupt handler.
9120Sstevel@tonic-gate * Called by the cyclic backend.
9130Sstevel@tonic-gate *
9140Sstevel@tonic-gate * Arguments and notes
9150Sstevel@tonic-gate *
9160Sstevel@tonic-gate * The only argument is the CPU on which the interrupt is executing;
9170Sstevel@tonic-gate * backends must call into cyclic_fire() on the specified CPU.
9180Sstevel@tonic-gate *
9190Sstevel@tonic-gate * cyclic_fire() may be called spuriously without ill effect. Optimal
9200Sstevel@tonic-gate * backends will call into cyclic_fire() at or shortly after the time
9210Sstevel@tonic-gate * requested via cyb_reprogram(). However, calling cyclic_fire()
9220Sstevel@tonic-gate * arbitrarily late will only manifest latency bubbles; the correctness
9230Sstevel@tonic-gate * of the cyclic subsystem does not rely on the timeliness of the backend.
9240Sstevel@tonic-gate *
9250Sstevel@tonic-gate * cyclic_fire() is wait-free; it will not block or spin.
9260Sstevel@tonic-gate *
9270Sstevel@tonic-gate * Return values
9280Sstevel@tonic-gate *
9290Sstevel@tonic-gate * None.
9300Sstevel@tonic-gate *
9310Sstevel@tonic-gate * Caller's context
9320Sstevel@tonic-gate *
9330Sstevel@tonic-gate * cyclic_fire() must be called from CY_HIGH_LEVEL interrupt context.
9340Sstevel@tonic-gate */
9350Sstevel@tonic-gate void
cyclic_fire(cpu_t * c)9360Sstevel@tonic-gate cyclic_fire(cpu_t *c)
9370Sstevel@tonic-gate {
9380Sstevel@tonic-gate cyc_cpu_t *cpu = c->cpu_cyclic;
9390Sstevel@tonic-gate cyc_backend_t *be = cpu->cyp_backend;
9400Sstevel@tonic-gate cyc_index_t *heap = cpu->cyp_heap;
9410Sstevel@tonic-gate cyclic_t *cyclic, *cyclics = cpu->cyp_cyclics;
9420Sstevel@tonic-gate void *arg = be->cyb_arg;
9430Sstevel@tonic-gate hrtime_t now = gethrtime();
9440Sstevel@tonic-gate hrtime_t exp;
9450Sstevel@tonic-gate
9460Sstevel@tonic-gate CYC_TRACE(cpu, CY_HIGH_LEVEL, "fire", now, 0);
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate if (cpu->cyp_nelems == 0) {
9490Sstevel@tonic-gate /*
9500Sstevel@tonic-gate * This is a spurious fire. Count it as such, and blow
9510Sstevel@tonic-gate * out of here.
9520Sstevel@tonic-gate */
9530Sstevel@tonic-gate CYC_TRACE0(cpu, CY_HIGH_LEVEL, "fire-spurious");
9540Sstevel@tonic-gate return;
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate
9570Sstevel@tonic-gate for (;;) {
9580Sstevel@tonic-gate cyc_index_t ndx = heap[0];
9590Sstevel@tonic-gate
9600Sstevel@tonic-gate cyclic = &cyclics[ndx];
9610Sstevel@tonic-gate
9620Sstevel@tonic-gate ASSERT(!(cyclic->cy_flags & CYF_FREE));
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate CYC_TRACE(cpu, CY_HIGH_LEVEL, "fire-check", cyclic,
9650Sstevel@tonic-gate cyclic->cy_expire);
9660Sstevel@tonic-gate
9670Sstevel@tonic-gate if ((exp = cyclic->cy_expire) > now)
9680Sstevel@tonic-gate break;
9690Sstevel@tonic-gate
9700Sstevel@tonic-gate cyclic_expire(cpu, ndx, cyclic);
9710Sstevel@tonic-gate
9720Sstevel@tonic-gate /*
973*8048SMadhavan.Venkataraman@Sun.COM * If the handler reprogrammed the cyclic, then don't
974*8048SMadhavan.Venkataraman@Sun.COM * recompute the expiration. Then, if the interval is
975*8048SMadhavan.Venkataraman@Sun.COM * infinity, set the expiration to infinity. This can
976*8048SMadhavan.Venkataraman@Sun.COM * be used to create one-shot timers.
977*8048SMadhavan.Venkataraman@Sun.COM */
978*8048SMadhavan.Venkataraman@Sun.COM if (exp != cyclic->cy_expire) {
979*8048SMadhavan.Venkataraman@Sun.COM /*
980*8048SMadhavan.Venkataraman@Sun.COM * If a hi level cyclic reprograms itself,
981*8048SMadhavan.Venkataraman@Sun.COM * the heap adjustment and reprogramming of the
982*8048SMadhavan.Venkataraman@Sun.COM * clock source have already been done at this
983*8048SMadhavan.Venkataraman@Sun.COM * point. So, we can continue.
984*8048SMadhavan.Venkataraman@Sun.COM */
985*8048SMadhavan.Venkataraman@Sun.COM continue;
986*8048SMadhavan.Venkataraman@Sun.COM }
987*8048SMadhavan.Venkataraman@Sun.COM
988*8048SMadhavan.Venkataraman@Sun.COM if (cyclic->cy_interval == CY_INFINITY)
989*8048SMadhavan.Venkataraman@Sun.COM exp = CY_INFINITY;
990*8048SMadhavan.Venkataraman@Sun.COM else
991*8048SMadhavan.Venkataraman@Sun.COM exp += cyclic->cy_interval;
992*8048SMadhavan.Venkataraman@Sun.COM
993*8048SMadhavan.Venkataraman@Sun.COM /*
9940Sstevel@tonic-gate * If this cyclic will be set to next expire in the distant
9950Sstevel@tonic-gate * past, we have one of two situations:
9960Sstevel@tonic-gate *
9970Sstevel@tonic-gate * a) This is the first firing of a cyclic which had
9980Sstevel@tonic-gate * cy_expire set to 0.
9990Sstevel@tonic-gate *
10000Sstevel@tonic-gate * b) We are tragically late for a cyclic -- most likely
10010Sstevel@tonic-gate * due to being in the debugger.
10020Sstevel@tonic-gate *
10030Sstevel@tonic-gate * In either case, we set the new expiration time to be the
10040Sstevel@tonic-gate * the next interval boundary. This assures that the
10050Sstevel@tonic-gate * expiration time modulo the interval is invariant.
10060Sstevel@tonic-gate *
10070Sstevel@tonic-gate * We arbitrarily define "distant" to be one second (one second
10080Sstevel@tonic-gate * is chosen because it's shorter than any foray to the
10090Sstevel@tonic-gate * debugger while still being longer than any legitimate
10100Sstevel@tonic-gate * stretch at CY_HIGH_LEVEL).
10110Sstevel@tonic-gate */
10120Sstevel@tonic-gate
10130Sstevel@tonic-gate if (now - exp > NANOSEC) {
10140Sstevel@tonic-gate hrtime_t interval = cyclic->cy_interval;
10150Sstevel@tonic-gate
10160Sstevel@tonic-gate CYC_TRACE(cpu, CY_HIGH_LEVEL, exp == interval ?
10170Sstevel@tonic-gate "fire-first" : "fire-swing", now, exp);
10180Sstevel@tonic-gate
10190Sstevel@tonic-gate exp += ((now - exp) / interval + 1) * interval;
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate
10220Sstevel@tonic-gate cyclic->cy_expire = exp;
10230Sstevel@tonic-gate cyclic_downheap(cpu, 0);
10240Sstevel@tonic-gate }
10250Sstevel@tonic-gate
10260Sstevel@tonic-gate /*
10270Sstevel@tonic-gate * Now we have a cyclic in the root slot which isn't in the past;
10280Sstevel@tonic-gate * reprogram the interrupt source.
10290Sstevel@tonic-gate */
10300Sstevel@tonic-gate be->cyb_reprogram(arg, exp);
10310Sstevel@tonic-gate }
10320Sstevel@tonic-gate
10330Sstevel@tonic-gate static void
cyclic_remove_pend(cyc_cpu_t * cpu,cyc_level_t level,cyclic_t * cyclic)10340Sstevel@tonic-gate cyclic_remove_pend(cyc_cpu_t *cpu, cyc_level_t level, cyclic_t *cyclic)
10350Sstevel@tonic-gate {
10360Sstevel@tonic-gate cyc_func_t handler = cyclic->cy_handler;
10370Sstevel@tonic-gate void *arg = cyclic->cy_arg;
10380Sstevel@tonic-gate uint32_t i, rpend = cpu->cyp_rpend - 1;
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate ASSERT(cyclic->cy_flags & CYF_FREE);
10410Sstevel@tonic-gate ASSERT(cyclic->cy_pend == 0);
10420Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_REMOVING);
10430Sstevel@tonic-gate ASSERT(cpu->cyp_rpend > 0);
10440Sstevel@tonic-gate
10450Sstevel@tonic-gate CYC_TRACE(cpu, level, "remove-rpend", cyclic, cpu->cyp_rpend);
10460Sstevel@tonic-gate
10470Sstevel@tonic-gate /*
10480Sstevel@tonic-gate * Note that we only call the handler cyp_rpend - 1 times; this is
10490Sstevel@tonic-gate * to account for the handler call in cyclic_softint().
10500Sstevel@tonic-gate */
10510Sstevel@tonic-gate for (i = 0; i < rpend; i++) {
10520Sstevel@tonic-gate CYC_TRACE(cpu, level, "rpend-in", handler, arg);
10535864Sesaxe DTRACE_PROBE1(cyclic__start, cyclic_t *, cyclic);
10545864Sesaxe
10550Sstevel@tonic-gate (*handler)(arg);
10565864Sesaxe
10575864Sesaxe DTRACE_PROBE1(cyclic__end, cyclic_t *, cyclic);
10580Sstevel@tonic-gate CYC_TRACE(cpu, level, "rpend-out", handler, arg);
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate
10610Sstevel@tonic-gate /*
10620Sstevel@tonic-gate * We can now let the remove operation complete.
10630Sstevel@tonic-gate */
10640Sstevel@tonic-gate sema_v(&cpu->cyp_modify_wait);
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate
10670Sstevel@tonic-gate /*
10680Sstevel@tonic-gate * cyclic_softint(cpu_t *cpu, cyc_level_t level)
10690Sstevel@tonic-gate *
10700Sstevel@tonic-gate * Overview
10710Sstevel@tonic-gate *
10720Sstevel@tonic-gate * cyclic_softint() is the cyclic subsystem's CY_LOCK_LEVEL and CY_LOW_LEVEL
10730Sstevel@tonic-gate * soft interrupt handler. Called by the cyclic backend.
10740Sstevel@tonic-gate *
10750Sstevel@tonic-gate * Arguments and notes
10760Sstevel@tonic-gate *
10770Sstevel@tonic-gate * The first argument to cyclic_softint() is the CPU on which the interrupt
10780Sstevel@tonic-gate * is executing; backends must call into cyclic_softint() on the specified
10790Sstevel@tonic-gate * CPU. The second argument is the level of the soft interrupt; it must
10800Sstevel@tonic-gate * be one of CY_LOCK_LEVEL or CY_LOW_LEVEL.
10810Sstevel@tonic-gate *
10820Sstevel@tonic-gate * cyclic_softint() will call the handlers for cyclics pending at the
10830Sstevel@tonic-gate * specified level. cyclic_softint() will not return until all pending
10840Sstevel@tonic-gate * cyclics at the specified level have been dealt with; intervening
10850Sstevel@tonic-gate * CY_HIGH_LEVEL interrupts which enqueue cyclics at the specified level
10860Sstevel@tonic-gate * may therefore prolong cyclic_softint().
10870Sstevel@tonic-gate *
10880Sstevel@tonic-gate * cyclic_softint() never disables interrupts, and, if neither a
10890Sstevel@tonic-gate * cyclic_add() nor a cyclic_remove() is pending on the specified CPU, is
10900Sstevel@tonic-gate * lock-free. This assures that in the common case, cyclic_softint()
10910Sstevel@tonic-gate * completes without blocking, and never starves cyclic_fire(). If either
10920Sstevel@tonic-gate * cyclic_add() or cyclic_remove() is pending, cyclic_softint() may grab
10930Sstevel@tonic-gate * a dispatcher lock.
10940Sstevel@tonic-gate *
10950Sstevel@tonic-gate * While cyclic_softint() is designed for bounded latency, it is obviously
10960Sstevel@tonic-gate * at the mercy of its cyclic handlers. Because cyclic handlers may block
10970Sstevel@tonic-gate * arbitrarily, callers of cyclic_softint() should not rely upon
10980Sstevel@tonic-gate * deterministic completion.
10990Sstevel@tonic-gate *
11000Sstevel@tonic-gate * cyclic_softint() may be called spuriously without ill effect.
11010Sstevel@tonic-gate *
11020Sstevel@tonic-gate * Return value
11030Sstevel@tonic-gate *
11040Sstevel@tonic-gate * None.
11050Sstevel@tonic-gate *
11060Sstevel@tonic-gate * Caller's context
11070Sstevel@tonic-gate *
11080Sstevel@tonic-gate * The caller must be executing in soft interrupt context at either
11090Sstevel@tonic-gate * CY_LOCK_LEVEL or CY_LOW_LEVEL. The level passed to cyclic_softint()
11100Sstevel@tonic-gate * must match the level at which it is executing. On optimal backends,
11110Sstevel@tonic-gate * the caller will hold no locks. In any case, the caller may not hold
11120Sstevel@tonic-gate * cpu_lock or any lock acquired by any cyclic handler or held across
11130Sstevel@tonic-gate * any of cyclic_add(), cyclic_remove(), cyclic_bind() or cyclic_juggle().
11140Sstevel@tonic-gate */
11150Sstevel@tonic-gate void
cyclic_softint(cpu_t * c,cyc_level_t level)11160Sstevel@tonic-gate cyclic_softint(cpu_t *c, cyc_level_t level)
11170Sstevel@tonic-gate {
11180Sstevel@tonic-gate cyc_cpu_t *cpu = c->cpu_cyclic;
11190Sstevel@tonic-gate cyc_softbuf_t *softbuf;
11200Sstevel@tonic-gate int soft, *buf, consndx, resized = 0, intr_resized = 0;
11210Sstevel@tonic-gate cyc_pcbuffer_t *pc;
11220Sstevel@tonic-gate cyclic_t *cyclics = cpu->cyp_cyclics;
11230Sstevel@tonic-gate int sizemask;
11240Sstevel@tonic-gate
11250Sstevel@tonic-gate CYC_TRACE(cpu, level, "softint", cyclics, 0);
11260Sstevel@tonic-gate
11270Sstevel@tonic-gate ASSERT(level < CY_LOW_LEVEL + CY_SOFT_LEVELS);
11280Sstevel@tonic-gate
11290Sstevel@tonic-gate softbuf = &cpu->cyp_softbuf[level];
11300Sstevel@tonic-gate top:
11310Sstevel@tonic-gate soft = softbuf->cys_soft;
11320Sstevel@tonic-gate ASSERT(soft == 0 || soft == 1);
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate pc = &softbuf->cys_buf[soft];
11350Sstevel@tonic-gate buf = pc->cypc_buf;
11360Sstevel@tonic-gate consndx = pc->cypc_consndx;
11370Sstevel@tonic-gate sizemask = pc->cypc_sizemask;
11380Sstevel@tonic-gate
11390Sstevel@tonic-gate CYC_TRACE(cpu, level, "softint-top", cyclics, pc);
11400Sstevel@tonic-gate
11410Sstevel@tonic-gate while (consndx != pc->cypc_prodndx) {
11420Sstevel@tonic-gate int pend, npend, opend;
11430Sstevel@tonic-gate int consmasked = consndx & sizemask;
11440Sstevel@tonic-gate cyclic_t *cyclic = &cyclics[buf[consmasked]];
11450Sstevel@tonic-gate cyc_func_t handler = cyclic->cy_handler;
11460Sstevel@tonic-gate void *arg = cyclic->cy_arg;
11470Sstevel@tonic-gate
11480Sstevel@tonic-gate ASSERT(buf[consmasked] < cpu->cyp_size);
11490Sstevel@tonic-gate CYC_TRACE(cpu, level, "consuming", consndx, cyclic);
11500Sstevel@tonic-gate
11510Sstevel@tonic-gate /*
11520Sstevel@tonic-gate * We have found this cyclic in the pcbuffer. We know that
11530Sstevel@tonic-gate * one of the following is true:
11540Sstevel@tonic-gate *
11550Sstevel@tonic-gate * (a) The pend is non-zero. We need to execute the handler
11560Sstevel@tonic-gate * at least once.
11570Sstevel@tonic-gate *
11580Sstevel@tonic-gate * (b) The pend _was_ non-zero, but it's now zero due to a
11590Sstevel@tonic-gate * resize. We will call the handler once, see that we
11600Sstevel@tonic-gate * are in this case, and read the new cyclics buffer
11610Sstevel@tonic-gate * (and hence the old non-zero pend).
11620Sstevel@tonic-gate *
11630Sstevel@tonic-gate * (c) The pend _was_ non-zero, but it's now zero due to a
11640Sstevel@tonic-gate * removal. We will call the handler once, see that we
11650Sstevel@tonic-gate * are in this case, and call into cyclic_remove_pend()
11660Sstevel@tonic-gate * to call the cyclic rpend times. We will take into
11670Sstevel@tonic-gate * account that we have already called the handler once.
11680Sstevel@tonic-gate *
11690Sstevel@tonic-gate * Point is: it's safe to call the handler without first
11700Sstevel@tonic-gate * checking the pend.
11710Sstevel@tonic-gate */
11720Sstevel@tonic-gate do {
11730Sstevel@tonic-gate CYC_TRACE(cpu, level, "handler-in", handler, arg);
11745864Sesaxe DTRACE_PROBE1(cyclic__start, cyclic_t *, cyclic);
11755864Sesaxe
11760Sstevel@tonic-gate (*handler)(arg);
11775864Sesaxe
11785864Sesaxe DTRACE_PROBE1(cyclic__end, cyclic_t *, cyclic);
11790Sstevel@tonic-gate CYC_TRACE(cpu, level, "handler-out", handler, arg);
11800Sstevel@tonic-gate reread:
11810Sstevel@tonic-gate pend = cyclic->cy_pend;
11820Sstevel@tonic-gate npend = pend - 1;
11830Sstevel@tonic-gate
11840Sstevel@tonic-gate if (pend == 0) {
11850Sstevel@tonic-gate if (cpu->cyp_state == CYS_REMOVING) {
11860Sstevel@tonic-gate /*
11870Sstevel@tonic-gate * This cyclic has been removed while
11880Sstevel@tonic-gate * it had a non-zero pend count (we
11890Sstevel@tonic-gate * know it was non-zero because we
11900Sstevel@tonic-gate * found this cyclic in the pcbuffer).
11910Sstevel@tonic-gate * There must be a non-zero rpend for
11920Sstevel@tonic-gate * this CPU, and there must be a remove
11930Sstevel@tonic-gate * operation blocking; we'll call into
11940Sstevel@tonic-gate * cyclic_remove_pend() to clean this
11950Sstevel@tonic-gate * up, and break out of the pend loop.
11960Sstevel@tonic-gate */
11970Sstevel@tonic-gate cyclic_remove_pend(cpu, level, cyclic);
11980Sstevel@tonic-gate break;
11990Sstevel@tonic-gate }
12000Sstevel@tonic-gate
12010Sstevel@tonic-gate /*
12020Sstevel@tonic-gate * We must have had a resize interrupt us.
12030Sstevel@tonic-gate */
12040Sstevel@tonic-gate CYC_TRACE(cpu, level, "resize-int", cyclics, 0);
12050Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_EXPANDING);
12060Sstevel@tonic-gate ASSERT(cyclics != cpu->cyp_cyclics);
12070Sstevel@tonic-gate ASSERT(resized == 0);
12080Sstevel@tonic-gate ASSERT(intr_resized == 0);
12090Sstevel@tonic-gate intr_resized = 1;
12100Sstevel@tonic-gate cyclics = cpu->cyp_cyclics;
12110Sstevel@tonic-gate cyclic = &cyclics[buf[consmasked]];
12120Sstevel@tonic-gate ASSERT(cyclic->cy_handler == handler);
12130Sstevel@tonic-gate ASSERT(cyclic->cy_arg == arg);
12140Sstevel@tonic-gate goto reread;
12150Sstevel@tonic-gate }
12160Sstevel@tonic-gate
12170Sstevel@tonic-gate if ((opend =
12180Sstevel@tonic-gate cas32(&cyclic->cy_pend, pend, npend)) != pend) {
12190Sstevel@tonic-gate /*
12200Sstevel@tonic-gate * Our cas32 can fail for one of several
12210Sstevel@tonic-gate * reasons:
12220Sstevel@tonic-gate *
12230Sstevel@tonic-gate * (a) An intervening high level bumped up the
12240Sstevel@tonic-gate * pend count on this cyclic. In this
12250Sstevel@tonic-gate * case, we will see a higher pend.
12260Sstevel@tonic-gate *
12270Sstevel@tonic-gate * (b) The cyclics array has been yanked out
12280Sstevel@tonic-gate * from underneath us by a resize
12290Sstevel@tonic-gate * operation. In this case, pend is 0 and
12300Sstevel@tonic-gate * cyp_state is CYS_EXPANDING.
12310Sstevel@tonic-gate *
12320Sstevel@tonic-gate * (c) The cyclic has been removed by an
12330Sstevel@tonic-gate * intervening remove-xcall. In this case,
12340Sstevel@tonic-gate * pend will be 0, the cyp_state will be
12350Sstevel@tonic-gate * CYS_REMOVING, and the cyclic will be
12360Sstevel@tonic-gate * marked CYF_FREE.
12370Sstevel@tonic-gate *
12380Sstevel@tonic-gate * The assertion below checks that we are
12390Sstevel@tonic-gate * in one of the above situations. The
12400Sstevel@tonic-gate * action under all three is to return to
12410Sstevel@tonic-gate * the top of the loop.
12420Sstevel@tonic-gate */
12430Sstevel@tonic-gate CYC_TRACE(cpu, level, "cas-fail", opend, pend);
12440Sstevel@tonic-gate ASSERT(opend > pend || (opend == 0 &&
12450Sstevel@tonic-gate ((cyclics != cpu->cyp_cyclics &&
12460Sstevel@tonic-gate cpu->cyp_state == CYS_EXPANDING) ||
12470Sstevel@tonic-gate (cpu->cyp_state == CYS_REMOVING &&
12480Sstevel@tonic-gate (cyclic->cy_flags & CYF_FREE)))));
12490Sstevel@tonic-gate goto reread;
12500Sstevel@tonic-gate }
12510Sstevel@tonic-gate
12520Sstevel@tonic-gate /*
12530Sstevel@tonic-gate * Okay, so we've managed to successfully decrement
12540Sstevel@tonic-gate * pend. If we just decremented the pend to 0, we're
12550Sstevel@tonic-gate * done.
12560Sstevel@tonic-gate */
12570Sstevel@tonic-gate } while (npend > 0);
12580Sstevel@tonic-gate
12590Sstevel@tonic-gate pc->cypc_consndx = ++consndx;
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate
12620Sstevel@tonic-gate /*
12630Sstevel@tonic-gate * If the high level handler is no longer writing to the same
12640Sstevel@tonic-gate * buffer, then we've had a resize. We need to switch our soft
12650Sstevel@tonic-gate * index, and goto top.
12660Sstevel@tonic-gate */
12670Sstevel@tonic-gate if (soft != softbuf->cys_hard) {
12680Sstevel@tonic-gate /*
12690Sstevel@tonic-gate * We can assert that the other buffer has grown by exactly
12700Sstevel@tonic-gate * one factor of two.
12710Sstevel@tonic-gate */
12720Sstevel@tonic-gate CYC_TRACE(cpu, level, "buffer-grow", 0, 0);
12730Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_EXPANDING);
12740Sstevel@tonic-gate ASSERT(softbuf->cys_buf[softbuf->cys_hard].cypc_sizemask ==
12750Sstevel@tonic-gate (softbuf->cys_buf[soft].cypc_sizemask << 1) + 1 ||
12760Sstevel@tonic-gate softbuf->cys_buf[soft].cypc_sizemask == 0);
12770Sstevel@tonic-gate ASSERT(softbuf->cys_hard == (softbuf->cys_soft ^ 1));
12780Sstevel@tonic-gate
12790Sstevel@tonic-gate /*
12800Sstevel@tonic-gate * If our cached cyclics pointer doesn't match cyp_cyclics,
12810Sstevel@tonic-gate * then we took a resize between our last iteration of the
12820Sstevel@tonic-gate * pend loop and the check against softbuf->cys_hard.
12830Sstevel@tonic-gate */
12840Sstevel@tonic-gate if (cpu->cyp_cyclics != cyclics) {
12850Sstevel@tonic-gate CYC_TRACE1(cpu, level, "resize-int-int", consndx);
12860Sstevel@tonic-gate cyclics = cpu->cyp_cyclics;
12870Sstevel@tonic-gate }
12880Sstevel@tonic-gate
12890Sstevel@tonic-gate softbuf->cys_soft = softbuf->cys_hard;
12900Sstevel@tonic-gate
12910Sstevel@tonic-gate ASSERT(resized == 0);
12920Sstevel@tonic-gate resized = 1;
12930Sstevel@tonic-gate goto top;
12940Sstevel@tonic-gate }
12950Sstevel@tonic-gate
12960Sstevel@tonic-gate /*
12970Sstevel@tonic-gate * If we were interrupted by a resize operation, then we must have
12980Sstevel@tonic-gate * seen the hard index change.
12990Sstevel@tonic-gate */
13000Sstevel@tonic-gate ASSERT(!(intr_resized == 1 && resized == 0));
13010Sstevel@tonic-gate
13020Sstevel@tonic-gate if (resized) {
13030Sstevel@tonic-gate uint32_t lev, nlev;
13040Sstevel@tonic-gate
13050Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_EXPANDING);
13060Sstevel@tonic-gate
13070Sstevel@tonic-gate do {
13080Sstevel@tonic-gate lev = cpu->cyp_modify_levels;
13090Sstevel@tonic-gate nlev = lev + 1;
13100Sstevel@tonic-gate } while (cas32(&cpu->cyp_modify_levels, lev, nlev) != lev);
13110Sstevel@tonic-gate
13120Sstevel@tonic-gate /*
13130Sstevel@tonic-gate * If we are the last soft level to see the modification,
13140Sstevel@tonic-gate * post on cyp_modify_wait. Otherwise, (if we're not
13150Sstevel@tonic-gate * already at low level), post down to the next soft level.
13160Sstevel@tonic-gate */
13170Sstevel@tonic-gate if (nlev == CY_SOFT_LEVELS) {
13180Sstevel@tonic-gate CYC_TRACE0(cpu, level, "resize-kick");
13190Sstevel@tonic-gate sema_v(&cpu->cyp_modify_wait);
13200Sstevel@tonic-gate } else {
13210Sstevel@tonic-gate ASSERT(nlev < CY_SOFT_LEVELS);
13220Sstevel@tonic-gate if (level != CY_LOW_LEVEL) {
13230Sstevel@tonic-gate cyc_backend_t *be = cpu->cyp_backend;
13240Sstevel@tonic-gate
13250Sstevel@tonic-gate CYC_TRACE0(cpu, level, "resize-post");
13260Sstevel@tonic-gate be->cyb_softint(be->cyb_arg, level - 1);
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate }
13290Sstevel@tonic-gate }
13300Sstevel@tonic-gate }
13310Sstevel@tonic-gate
13320Sstevel@tonic-gate static void
cyclic_expand_xcall(cyc_xcallarg_t * arg)13330Sstevel@tonic-gate cyclic_expand_xcall(cyc_xcallarg_t *arg)
13340Sstevel@tonic-gate {
13350Sstevel@tonic-gate cyc_cpu_t *cpu = arg->cyx_cpu;
13360Sstevel@tonic-gate cyc_backend_t *be = cpu->cyp_backend;
13370Sstevel@tonic-gate cyb_arg_t bar = be->cyb_arg;
13380Sstevel@tonic-gate cyc_cookie_t cookie;
13390Sstevel@tonic-gate cyc_index_t new_size = arg->cyx_size, size = cpu->cyp_size, i;
13400Sstevel@tonic-gate cyc_index_t *new_heap = arg->cyx_heap;
13410Sstevel@tonic-gate cyclic_t *cyclics = cpu->cyp_cyclics, *new_cyclics = arg->cyx_cyclics;
13420Sstevel@tonic-gate
13430Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_EXPANDING);
13440Sstevel@tonic-gate
13450Sstevel@tonic-gate /*
13460Sstevel@tonic-gate * This is a little dicey. First, we'll raise our interrupt level
13470Sstevel@tonic-gate * to CY_HIGH_LEVEL. This CPU already has a new heap, cyclic array,
13480Sstevel@tonic-gate * etc.; we just need to bcopy them across. As for the softint
13490Sstevel@tonic-gate * buffers, we'll switch the active buffers. The actual softints will
13500Sstevel@tonic-gate * take care of consuming any pending cyclics in the old buffer.
13510Sstevel@tonic-gate */
13520Sstevel@tonic-gate cookie = be->cyb_set_level(bar, CY_HIGH_LEVEL);
13530Sstevel@tonic-gate
13540Sstevel@tonic-gate CYC_TRACE(cpu, CY_HIGH_LEVEL, "expand", new_size, 0);
13550Sstevel@tonic-gate
13560Sstevel@tonic-gate /*
13570Sstevel@tonic-gate * Assert that the new size is a power of 2.
13580Sstevel@tonic-gate */
13590Sstevel@tonic-gate ASSERT((new_size & new_size - 1) == 0);
13600Sstevel@tonic-gate ASSERT(new_size == (size << 1));
13610Sstevel@tonic-gate ASSERT(cpu->cyp_heap != NULL && cpu->cyp_cyclics != NULL);
13620Sstevel@tonic-gate
13630Sstevel@tonic-gate bcopy(cpu->cyp_heap, new_heap, sizeof (cyc_index_t) * size);
13640Sstevel@tonic-gate bcopy(cyclics, new_cyclics, sizeof (cyclic_t) * size);
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate /*
13670Sstevel@tonic-gate * Now run through the old cyclics array, setting pend to 0. To
13680Sstevel@tonic-gate * softints (which are executing at a lower priority level), the
13690Sstevel@tonic-gate * pends dropping to 0 will appear atomic with the cyp_cyclics
13700Sstevel@tonic-gate * pointer changing.
13710Sstevel@tonic-gate */
13720Sstevel@tonic-gate for (i = 0; i < size; i++)
13730Sstevel@tonic-gate cyclics[i].cy_pend = 0;
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate /*
13760Sstevel@tonic-gate * Set up the free list, and set all of the new cyclics to be CYF_FREE.
13770Sstevel@tonic-gate */
13780Sstevel@tonic-gate for (i = size; i < new_size; i++) {
13790Sstevel@tonic-gate new_heap[i] = i;
13800Sstevel@tonic-gate new_cyclics[i].cy_flags = CYF_FREE;
13810Sstevel@tonic-gate }
13820Sstevel@tonic-gate
13830Sstevel@tonic-gate /*
13840Sstevel@tonic-gate * We can go ahead and plow the value of cyp_heap and cyp_cyclics;
13850Sstevel@tonic-gate * cyclic_expand() has kept a copy.
13860Sstevel@tonic-gate */
13870Sstevel@tonic-gate cpu->cyp_heap = new_heap;
13880Sstevel@tonic-gate cpu->cyp_cyclics = new_cyclics;
13890Sstevel@tonic-gate cpu->cyp_size = new_size;
13900Sstevel@tonic-gate
13910Sstevel@tonic-gate /*
13920Sstevel@tonic-gate * We've switched over the heap and the cyclics array. Now we need
13930Sstevel@tonic-gate * to switch over our active softint buffer pointers.
13940Sstevel@tonic-gate */
13950Sstevel@tonic-gate for (i = CY_LOW_LEVEL; i < CY_LOW_LEVEL + CY_SOFT_LEVELS; i++) {
13960Sstevel@tonic-gate cyc_softbuf_t *softbuf = &cpu->cyp_softbuf[i];
13970Sstevel@tonic-gate uchar_t hard = softbuf->cys_hard;
13980Sstevel@tonic-gate
13990Sstevel@tonic-gate /*
14000Sstevel@tonic-gate * Assert that we're not in the middle of a resize operation.
14010Sstevel@tonic-gate */
14020Sstevel@tonic-gate ASSERT(hard == softbuf->cys_soft);
14030Sstevel@tonic-gate ASSERT(hard == 0 || hard == 1);
14040Sstevel@tonic-gate ASSERT(softbuf->cys_buf[hard].cypc_buf != NULL);
14050Sstevel@tonic-gate
14060Sstevel@tonic-gate softbuf->cys_hard = hard ^ 1;
14070Sstevel@tonic-gate
14080Sstevel@tonic-gate /*
14090Sstevel@tonic-gate * The caller (cyclic_expand()) is responsible for setting
14100Sstevel@tonic-gate * up the new producer-consumer buffer; assert that it's
14110Sstevel@tonic-gate * been done correctly.
14120Sstevel@tonic-gate */
14130Sstevel@tonic-gate ASSERT(softbuf->cys_buf[hard ^ 1].cypc_buf != NULL);
14140Sstevel@tonic-gate ASSERT(softbuf->cys_buf[hard ^ 1].cypc_prodndx == 0);
14150Sstevel@tonic-gate ASSERT(softbuf->cys_buf[hard ^ 1].cypc_consndx == 0);
14160Sstevel@tonic-gate }
14170Sstevel@tonic-gate
14180Sstevel@tonic-gate /*
14190Sstevel@tonic-gate * That's all there is to it; now we just need to postdown to
14200Sstevel@tonic-gate * get the softint chain going.
14210Sstevel@tonic-gate */
14220Sstevel@tonic-gate be->cyb_softint(bar, CY_HIGH_LEVEL - 1);
14230Sstevel@tonic-gate be->cyb_restore_level(bar, cookie);
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate
14260Sstevel@tonic-gate /*
14270Sstevel@tonic-gate * cyclic_expand() will cross call onto the CPU to perform the actual
14280Sstevel@tonic-gate * expand operation.
14290Sstevel@tonic-gate */
14300Sstevel@tonic-gate static void
cyclic_expand(cyc_cpu_t * cpu)14310Sstevel@tonic-gate cyclic_expand(cyc_cpu_t *cpu)
14320Sstevel@tonic-gate {
14330Sstevel@tonic-gate cyc_index_t new_size, old_size;
14340Sstevel@tonic-gate cyc_index_t *new_heap, *old_heap;
14350Sstevel@tonic-gate cyclic_t *new_cyclics, *old_cyclics;
14360Sstevel@tonic-gate cyc_xcallarg_t arg;
14370Sstevel@tonic-gate cyc_backend_t *be = cpu->cyp_backend;
14380Sstevel@tonic-gate char old_hard;
14390Sstevel@tonic-gate int i;
14400Sstevel@tonic-gate
14410Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
14420Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE);
14430Sstevel@tonic-gate
14440Sstevel@tonic-gate cpu->cyp_state = CYS_EXPANDING;
14450Sstevel@tonic-gate
14460Sstevel@tonic-gate old_heap = cpu->cyp_heap;
14470Sstevel@tonic-gate old_cyclics = cpu->cyp_cyclics;
14480Sstevel@tonic-gate
14490Sstevel@tonic-gate if ((new_size = ((old_size = cpu->cyp_size) << 1)) == 0) {
14500Sstevel@tonic-gate new_size = CY_DEFAULT_PERCPU;
14510Sstevel@tonic-gate ASSERT(old_heap == NULL && old_cyclics == NULL);
14520Sstevel@tonic-gate }
14530Sstevel@tonic-gate
14540Sstevel@tonic-gate /*
14550Sstevel@tonic-gate * Check that the new_size is a power of 2.
14560Sstevel@tonic-gate */
14570Sstevel@tonic-gate ASSERT((new_size - 1 & new_size) == 0);
14580Sstevel@tonic-gate
14590Sstevel@tonic-gate new_heap = kmem_alloc(sizeof (cyc_index_t) * new_size, KM_SLEEP);
14600Sstevel@tonic-gate new_cyclics = kmem_zalloc(sizeof (cyclic_t) * new_size, KM_SLEEP);
14610Sstevel@tonic-gate
14620Sstevel@tonic-gate /*
14630Sstevel@tonic-gate * We know that no other expansions are in progress (they serialize
14640Sstevel@tonic-gate * on cpu_lock), so we can safely read the softbuf metadata.
14650Sstevel@tonic-gate */
14660Sstevel@tonic-gate old_hard = cpu->cyp_softbuf[0].cys_hard;
14670Sstevel@tonic-gate
14680Sstevel@tonic-gate for (i = CY_LOW_LEVEL; i < CY_LOW_LEVEL + CY_SOFT_LEVELS; i++) {
14690Sstevel@tonic-gate cyc_softbuf_t *softbuf = &cpu->cyp_softbuf[i];
14700Sstevel@tonic-gate char hard = softbuf->cys_hard;
14710Sstevel@tonic-gate cyc_pcbuffer_t *pc = &softbuf->cys_buf[hard ^ 1];
14720Sstevel@tonic-gate
14730Sstevel@tonic-gate ASSERT(hard == old_hard);
14740Sstevel@tonic-gate ASSERT(hard == softbuf->cys_soft);
14750Sstevel@tonic-gate ASSERT(pc->cypc_buf == NULL);
14760Sstevel@tonic-gate
14770Sstevel@tonic-gate pc->cypc_buf =
14780Sstevel@tonic-gate kmem_alloc(sizeof (cyc_index_t) * new_size, KM_SLEEP);
14790Sstevel@tonic-gate pc->cypc_prodndx = pc->cypc_consndx = 0;
14800Sstevel@tonic-gate pc->cypc_sizemask = new_size - 1;
14810Sstevel@tonic-gate }
14820Sstevel@tonic-gate
14830Sstevel@tonic-gate arg.cyx_cpu = cpu;
14840Sstevel@tonic-gate arg.cyx_heap = new_heap;
14850Sstevel@tonic-gate arg.cyx_cyclics = new_cyclics;
14860Sstevel@tonic-gate arg.cyx_size = new_size;
14870Sstevel@tonic-gate
14880Sstevel@tonic-gate cpu->cyp_modify_levels = 0;
14890Sstevel@tonic-gate
14900Sstevel@tonic-gate be->cyb_xcall(be->cyb_arg, cpu->cyp_cpu,
14910Sstevel@tonic-gate (cyc_func_t)cyclic_expand_xcall, &arg);
14920Sstevel@tonic-gate
14930Sstevel@tonic-gate /*
14940Sstevel@tonic-gate * Now block, waiting for the resize operation to complete.
14950Sstevel@tonic-gate */
14960Sstevel@tonic-gate sema_p(&cpu->cyp_modify_wait);
14970Sstevel@tonic-gate ASSERT(cpu->cyp_modify_levels == CY_SOFT_LEVELS);
14980Sstevel@tonic-gate
14990Sstevel@tonic-gate /*
15000Sstevel@tonic-gate * The operation is complete; we can now free the old buffers.
15010Sstevel@tonic-gate */
15020Sstevel@tonic-gate for (i = CY_LOW_LEVEL; i < CY_LOW_LEVEL + CY_SOFT_LEVELS; i++) {
15030Sstevel@tonic-gate cyc_softbuf_t *softbuf = &cpu->cyp_softbuf[i];
15040Sstevel@tonic-gate char hard = softbuf->cys_hard;
15050Sstevel@tonic-gate cyc_pcbuffer_t *pc = &softbuf->cys_buf[hard ^ 1];
15060Sstevel@tonic-gate
15070Sstevel@tonic-gate ASSERT(hard == (old_hard ^ 1));
15080Sstevel@tonic-gate ASSERT(hard == softbuf->cys_soft);
15090Sstevel@tonic-gate
15100Sstevel@tonic-gate if (pc->cypc_buf == NULL)
15110Sstevel@tonic-gate continue;
15120Sstevel@tonic-gate
15130Sstevel@tonic-gate ASSERT(pc->cypc_sizemask == ((new_size - 1) >> 1));
15140Sstevel@tonic-gate
15150Sstevel@tonic-gate kmem_free(pc->cypc_buf,
15160Sstevel@tonic-gate sizeof (cyc_index_t) * (pc->cypc_sizemask + 1));
15170Sstevel@tonic-gate pc->cypc_buf = NULL;
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate
15200Sstevel@tonic-gate if (old_cyclics != NULL) {
15210Sstevel@tonic-gate ASSERT(old_heap != NULL);
15220Sstevel@tonic-gate ASSERT(old_size != 0);
15230Sstevel@tonic-gate kmem_free(old_cyclics, sizeof (cyclic_t) * old_size);
15240Sstevel@tonic-gate kmem_free(old_heap, sizeof (cyc_index_t) * old_size);
15250Sstevel@tonic-gate }
15260Sstevel@tonic-gate
15270Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_EXPANDING);
15280Sstevel@tonic-gate cpu->cyp_state = CYS_ONLINE;
15290Sstevel@tonic-gate }
15300Sstevel@tonic-gate
15310Sstevel@tonic-gate /*
15320Sstevel@tonic-gate * cyclic_pick_cpu will attempt to pick a CPU according to the constraints
15330Sstevel@tonic-gate * specified by the partition, bound CPU, and flags. Additionally,
15340Sstevel@tonic-gate * cyclic_pick_cpu() will not pick the avoid CPU; it will return NULL if
15350Sstevel@tonic-gate * the avoid CPU is the only CPU which satisfies the constraints.
15360Sstevel@tonic-gate *
15370Sstevel@tonic-gate * If CYF_CPU_BOUND is set in flags, the specified CPU must be non-NULL.
15380Sstevel@tonic-gate * If CYF_PART_BOUND is set in flags, the specified partition must be non-NULL.
15390Sstevel@tonic-gate * If both CYF_CPU_BOUND and CYF_PART_BOUND are set, the specified CPU must
15400Sstevel@tonic-gate * be in the specified partition.
15410Sstevel@tonic-gate */
15420Sstevel@tonic-gate static cyc_cpu_t *
cyclic_pick_cpu(cpupart_t * part,cpu_t * bound,cpu_t * avoid,uint16_t flags)15430Sstevel@tonic-gate cyclic_pick_cpu(cpupart_t *part, cpu_t *bound, cpu_t *avoid, uint16_t flags)
15440Sstevel@tonic-gate {
15450Sstevel@tonic-gate cpu_t *c, *start = (part != NULL) ? part->cp_cpulist : CPU;
15460Sstevel@tonic-gate cpu_t *online = NULL;
15470Sstevel@tonic-gate uintptr_t offset;
15480Sstevel@tonic-gate
15490Sstevel@tonic-gate CYC_PTRACE("pick-cpu", part, bound);
15500Sstevel@tonic-gate
15510Sstevel@tonic-gate ASSERT(!(flags & CYF_CPU_BOUND) || bound != NULL);
15520Sstevel@tonic-gate ASSERT(!(flags & CYF_PART_BOUND) || part != NULL);
15530Sstevel@tonic-gate
15540Sstevel@tonic-gate /*
15550Sstevel@tonic-gate * If we're bound to our CPU, there isn't much choice involved. We
15560Sstevel@tonic-gate * need to check that the CPU passed as bound is in the cpupart, and
15570Sstevel@tonic-gate * that the CPU that we're binding to has been configured.
15580Sstevel@tonic-gate */
15590Sstevel@tonic-gate if (flags & CYF_CPU_BOUND) {
15600Sstevel@tonic-gate CYC_PTRACE("pick-cpu-bound", bound, avoid);
15610Sstevel@tonic-gate
15620Sstevel@tonic-gate if ((flags & CYF_PART_BOUND) && bound->cpu_part != part)
15630Sstevel@tonic-gate panic("cyclic_pick_cpu: "
15640Sstevel@tonic-gate "CPU binding contradicts partition binding");
15650Sstevel@tonic-gate
15660Sstevel@tonic-gate if (bound == avoid)
15670Sstevel@tonic-gate return (NULL);
15680Sstevel@tonic-gate
15690Sstevel@tonic-gate if (bound->cpu_cyclic == NULL)
15700Sstevel@tonic-gate panic("cyclic_pick_cpu: "
15710Sstevel@tonic-gate "attempt to bind to non-configured CPU");
15720Sstevel@tonic-gate
15730Sstevel@tonic-gate return (bound->cpu_cyclic);
15740Sstevel@tonic-gate }
15750Sstevel@tonic-gate
15760Sstevel@tonic-gate if (flags & CYF_PART_BOUND) {
15770Sstevel@tonic-gate CYC_PTRACE("pick-part-bound", bound, avoid);
15780Sstevel@tonic-gate offset = offsetof(cpu_t, cpu_next_part);
15790Sstevel@tonic-gate } else {
15800Sstevel@tonic-gate offset = offsetof(cpu_t, cpu_next_onln);
15810Sstevel@tonic-gate }
15820Sstevel@tonic-gate
15830Sstevel@tonic-gate c = start;
15840Sstevel@tonic-gate do {
15850Sstevel@tonic-gate if (c->cpu_cyclic == NULL)
15860Sstevel@tonic-gate continue;
15870Sstevel@tonic-gate
15880Sstevel@tonic-gate if (c->cpu_cyclic->cyp_state == CYS_OFFLINE)
15890Sstevel@tonic-gate continue;
15900Sstevel@tonic-gate
15910Sstevel@tonic-gate if (c == avoid)
15920Sstevel@tonic-gate continue;
15930Sstevel@tonic-gate
15940Sstevel@tonic-gate if (c->cpu_flags & CPU_ENABLE)
15950Sstevel@tonic-gate goto found;
15960Sstevel@tonic-gate
15970Sstevel@tonic-gate if (online == NULL)
15980Sstevel@tonic-gate online = c;
15990Sstevel@tonic-gate } while ((c = *(cpu_t **)((uintptr_t)c + offset)) != start);
16000Sstevel@tonic-gate
16010Sstevel@tonic-gate /*
16020Sstevel@tonic-gate * If we're here, we're in one of two situations:
16030Sstevel@tonic-gate *
16040Sstevel@tonic-gate * (a) We have a partition-bound cyclic, and there is no CPU in
16050Sstevel@tonic-gate * our partition which is CPU_ENABLE'd. If we saw another
16060Sstevel@tonic-gate * non-CYS_OFFLINE CPU in our partition, we'll go with it.
16070Sstevel@tonic-gate * If not, the avoid CPU must be the only non-CYS_OFFLINE
16080Sstevel@tonic-gate * CPU in the partition; we're forced to return NULL.
16090Sstevel@tonic-gate *
16100Sstevel@tonic-gate * (b) We have a partition-unbound cyclic, in which case there
16110Sstevel@tonic-gate * must only be one CPU CPU_ENABLE'd, and it must be the one
16120Sstevel@tonic-gate * we're trying to avoid. If cyclic_juggle()/cyclic_offline()
16130Sstevel@tonic-gate * are called appropriately, this generally shouldn't happen
16140Sstevel@tonic-gate * (the offline should fail before getting to this code).
16150Sstevel@tonic-gate * At any rate: we can't avoid the avoid CPU, so we return
16160Sstevel@tonic-gate * NULL.
16170Sstevel@tonic-gate */
16180Sstevel@tonic-gate if (!(flags & CYF_PART_BOUND)) {
16190Sstevel@tonic-gate ASSERT(avoid->cpu_flags & CPU_ENABLE);
16200Sstevel@tonic-gate return (NULL);
16210Sstevel@tonic-gate }
16220Sstevel@tonic-gate
16230Sstevel@tonic-gate CYC_PTRACE("pick-no-intr", part, avoid);
16240Sstevel@tonic-gate
16250Sstevel@tonic-gate if ((c = online) != NULL)
16260Sstevel@tonic-gate goto found;
16270Sstevel@tonic-gate
16280Sstevel@tonic-gate CYC_PTRACE("pick-fail", part, avoid);
16290Sstevel@tonic-gate ASSERT(avoid->cpu_part == start->cpu_part);
16300Sstevel@tonic-gate return (NULL);
16310Sstevel@tonic-gate
16320Sstevel@tonic-gate found:
16330Sstevel@tonic-gate CYC_PTRACE("pick-cpu-found", c, avoid);
16340Sstevel@tonic-gate ASSERT(c != avoid);
16350Sstevel@tonic-gate ASSERT(c->cpu_cyclic != NULL);
16360Sstevel@tonic-gate
16370Sstevel@tonic-gate return (c->cpu_cyclic);
16380Sstevel@tonic-gate }
16390Sstevel@tonic-gate
16400Sstevel@tonic-gate static void
cyclic_add_xcall(cyc_xcallarg_t * arg)16410Sstevel@tonic-gate cyclic_add_xcall(cyc_xcallarg_t *arg)
16420Sstevel@tonic-gate {
16430Sstevel@tonic-gate cyc_cpu_t *cpu = arg->cyx_cpu;
16440Sstevel@tonic-gate cyc_handler_t *hdlr = arg->cyx_hdlr;
16450Sstevel@tonic-gate cyc_time_t *when = arg->cyx_when;
16460Sstevel@tonic-gate cyc_backend_t *be = cpu->cyp_backend;
16470Sstevel@tonic-gate cyc_index_t ndx, nelems;
16480Sstevel@tonic-gate cyc_cookie_t cookie;
16490Sstevel@tonic-gate cyb_arg_t bar = be->cyb_arg;
16500Sstevel@tonic-gate cyclic_t *cyclic;
16510Sstevel@tonic-gate
16520Sstevel@tonic-gate ASSERT(cpu->cyp_nelems < cpu->cyp_size);
16530Sstevel@tonic-gate
16540Sstevel@tonic-gate cookie = be->cyb_set_level(bar, CY_HIGH_LEVEL);
16550Sstevel@tonic-gate
16560Sstevel@tonic-gate CYC_TRACE(cpu, CY_HIGH_LEVEL,
16570Sstevel@tonic-gate "add-xcall", when->cyt_when, when->cyt_interval);
16580Sstevel@tonic-gate
16590Sstevel@tonic-gate nelems = cpu->cyp_nelems++;
16600Sstevel@tonic-gate
16610Sstevel@tonic-gate if (nelems == 0) {
16620Sstevel@tonic-gate /*
16630Sstevel@tonic-gate * If this is the first element, we need to enable the
16640Sstevel@tonic-gate * backend on this CPU.
16650Sstevel@tonic-gate */
16660Sstevel@tonic-gate CYC_TRACE0(cpu, CY_HIGH_LEVEL, "enabled");
16670Sstevel@tonic-gate be->cyb_enable(bar);
16680Sstevel@tonic-gate }
16690Sstevel@tonic-gate
16700Sstevel@tonic-gate ndx = cpu->cyp_heap[nelems];
16710Sstevel@tonic-gate cyclic = &cpu->cyp_cyclics[ndx];
16720Sstevel@tonic-gate
16730Sstevel@tonic-gate ASSERT(cyclic->cy_flags == CYF_FREE);
16740Sstevel@tonic-gate cyclic->cy_interval = when->cyt_interval;
16750Sstevel@tonic-gate
16760Sstevel@tonic-gate if (when->cyt_when == 0) {
16770Sstevel@tonic-gate /*
16780Sstevel@tonic-gate * If a start time hasn't been explicitly specified, we'll
16790Sstevel@tonic-gate * start on the next interval boundary.
16800Sstevel@tonic-gate */
16810Sstevel@tonic-gate cyclic->cy_expire = (gethrtime() / cyclic->cy_interval + 1) *
16820Sstevel@tonic-gate cyclic->cy_interval;
16830Sstevel@tonic-gate } else {
16840Sstevel@tonic-gate cyclic->cy_expire = when->cyt_when;
16850Sstevel@tonic-gate }
16860Sstevel@tonic-gate
16870Sstevel@tonic-gate cyclic->cy_handler = hdlr->cyh_func;
16880Sstevel@tonic-gate cyclic->cy_arg = hdlr->cyh_arg;
16890Sstevel@tonic-gate cyclic->cy_level = hdlr->cyh_level;
16900Sstevel@tonic-gate cyclic->cy_flags = arg->cyx_flags;
16910Sstevel@tonic-gate
16920Sstevel@tonic-gate if (cyclic_upheap(cpu, nelems)) {
16930Sstevel@tonic-gate hrtime_t exp = cyclic->cy_expire;
16940Sstevel@tonic-gate
16950Sstevel@tonic-gate CYC_TRACE(cpu, CY_HIGH_LEVEL, "add-reprog", cyclic, exp);
16960Sstevel@tonic-gate
16970Sstevel@tonic-gate /*
16980Sstevel@tonic-gate * If our upheap propagated to the root, we need to
16990Sstevel@tonic-gate * reprogram the interrupt source.
17000Sstevel@tonic-gate */
17010Sstevel@tonic-gate be->cyb_reprogram(bar, exp);
17020Sstevel@tonic-gate }
17030Sstevel@tonic-gate be->cyb_restore_level(bar, cookie);
17040Sstevel@tonic-gate
17050Sstevel@tonic-gate arg->cyx_ndx = ndx;
17060Sstevel@tonic-gate }
17070Sstevel@tonic-gate
17080Sstevel@tonic-gate static cyc_index_t
cyclic_add_here(cyc_cpu_t * cpu,cyc_handler_t * hdlr,cyc_time_t * when,uint16_t flags)17090Sstevel@tonic-gate cyclic_add_here(cyc_cpu_t *cpu, cyc_handler_t *hdlr,
17100Sstevel@tonic-gate cyc_time_t *when, uint16_t flags)
17110Sstevel@tonic-gate {
17120Sstevel@tonic-gate cyc_backend_t *be = cpu->cyp_backend;
17130Sstevel@tonic-gate cyb_arg_t bar = be->cyb_arg;
17140Sstevel@tonic-gate cyc_xcallarg_t arg;
17150Sstevel@tonic-gate
17160Sstevel@tonic-gate CYC_PTRACE("add-cpu", cpu, hdlr->cyh_func);
17170Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
17180Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE);
17190Sstevel@tonic-gate ASSERT(!(cpu->cyp_cpu->cpu_flags & CPU_OFFLINE));
17200Sstevel@tonic-gate ASSERT(when->cyt_when >= 0 && when->cyt_interval > 0);
17210Sstevel@tonic-gate
17220Sstevel@tonic-gate if (cpu->cyp_nelems == cpu->cyp_size) {
17230Sstevel@tonic-gate /*
17240Sstevel@tonic-gate * This is expensive; it will cross call onto the other
17250Sstevel@tonic-gate * CPU to perform the expansion.
17260Sstevel@tonic-gate */
17270Sstevel@tonic-gate cyclic_expand(cpu);
17280Sstevel@tonic-gate ASSERT(cpu->cyp_nelems < cpu->cyp_size);
17290Sstevel@tonic-gate }
17300Sstevel@tonic-gate
17310Sstevel@tonic-gate /*
17320Sstevel@tonic-gate * By now, we know that we're going to be able to successfully
17330Sstevel@tonic-gate * perform the add. Now cross call over to the CPU of interest to
17340Sstevel@tonic-gate * actually add our cyclic.
17350Sstevel@tonic-gate */
17360Sstevel@tonic-gate arg.cyx_cpu = cpu;
17370Sstevel@tonic-gate arg.cyx_hdlr = hdlr;
17380Sstevel@tonic-gate arg.cyx_when = when;
17390Sstevel@tonic-gate arg.cyx_flags = flags;
17400Sstevel@tonic-gate
17410Sstevel@tonic-gate be->cyb_xcall(bar, cpu->cyp_cpu, (cyc_func_t)cyclic_add_xcall, &arg);
17420Sstevel@tonic-gate
17430Sstevel@tonic-gate CYC_PTRACE("add-cpu-done", cpu, arg.cyx_ndx);
17440Sstevel@tonic-gate
17450Sstevel@tonic-gate return (arg.cyx_ndx);
17460Sstevel@tonic-gate }
17470Sstevel@tonic-gate
17480Sstevel@tonic-gate static void
cyclic_remove_xcall(cyc_xcallarg_t * arg)17490Sstevel@tonic-gate cyclic_remove_xcall(cyc_xcallarg_t *arg)
17500Sstevel@tonic-gate {
17510Sstevel@tonic-gate cyc_cpu_t *cpu = arg->cyx_cpu;
17520Sstevel@tonic-gate cyc_backend_t *be = cpu->cyp_backend;
17530Sstevel@tonic-gate cyb_arg_t bar = be->cyb_arg;
17540Sstevel@tonic-gate cyc_cookie_t cookie;
1755*8048SMadhavan.Venkataraman@Sun.COM cyc_index_t ndx = arg->cyx_ndx, nelems, i;
1756*8048SMadhavan.Venkataraman@Sun.COM cyc_index_t *heap, last;
17570Sstevel@tonic-gate cyclic_t *cyclic;
17580Sstevel@tonic-gate #ifdef DEBUG
17590Sstevel@tonic-gate cyc_index_t root;
17600Sstevel@tonic-gate #endif
17610Sstevel@tonic-gate
17620Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_REMOVING);
17630Sstevel@tonic-gate
17640Sstevel@tonic-gate cookie = be->cyb_set_level(bar, CY_HIGH_LEVEL);
17650Sstevel@tonic-gate
17660Sstevel@tonic-gate CYC_TRACE1(cpu, CY_HIGH_LEVEL, "remove-xcall", ndx);
17670Sstevel@tonic-gate
1768*8048SMadhavan.Venkataraman@Sun.COM heap = cpu->cyp_heap;
1769*8048SMadhavan.Venkataraman@Sun.COM nelems = cpu->cyp_nelems;
1770*8048SMadhavan.Venkataraman@Sun.COM ASSERT(nelems > 0);
17710Sstevel@tonic-gate cyclic = &cpu->cyp_cyclics[ndx];
17720Sstevel@tonic-gate
17730Sstevel@tonic-gate /*
17740Sstevel@tonic-gate * Grab the current expiration time. If this cyclic is being
17750Sstevel@tonic-gate * removed as part of a juggling operation, the expiration time
17760Sstevel@tonic-gate * will be used when the cyclic is added to the new CPU.
17770Sstevel@tonic-gate */
17780Sstevel@tonic-gate if (arg->cyx_when != NULL) {
17790Sstevel@tonic-gate arg->cyx_when->cyt_when = cyclic->cy_expire;
17800Sstevel@tonic-gate arg->cyx_when->cyt_interval = cyclic->cy_interval;
17810Sstevel@tonic-gate }
17820Sstevel@tonic-gate
17830Sstevel@tonic-gate if (cyclic->cy_pend != 0) {
17840Sstevel@tonic-gate /*
17850Sstevel@tonic-gate * The pend is non-zero; this cyclic is currently being
17860Sstevel@tonic-gate * executed (or will be executed shortly). If the caller
17870Sstevel@tonic-gate * refuses to wait, we must return (doing nothing). Otherwise,
17880Sstevel@tonic-gate * we will stash the pend value * in this CPU's rpend, and
17890Sstevel@tonic-gate * then zero it out. The softint in the pend loop will see
17900Sstevel@tonic-gate * that we have zeroed out pend, and will call the cyclic
17910Sstevel@tonic-gate * handler rpend times. The caller will wait until the
17920Sstevel@tonic-gate * softint has completed calling the cyclic handler.
17930Sstevel@tonic-gate */
17940Sstevel@tonic-gate if (arg->cyx_wait == CY_NOWAIT) {
17950Sstevel@tonic-gate arg->cyx_wait = CY_WAIT;
17960Sstevel@tonic-gate goto out;
17970Sstevel@tonic-gate }
17980Sstevel@tonic-gate
17990Sstevel@tonic-gate ASSERT(cyclic->cy_level != CY_HIGH_LEVEL);
18000Sstevel@tonic-gate CYC_TRACE1(cpu, CY_HIGH_LEVEL, "remove-pend", cyclic->cy_pend);
18010Sstevel@tonic-gate cpu->cyp_rpend = cyclic->cy_pend;
18020Sstevel@tonic-gate cyclic->cy_pend = 0;
18030Sstevel@tonic-gate }
18040Sstevel@tonic-gate
18050Sstevel@tonic-gate /*
18060Sstevel@tonic-gate * Now set the flags to CYF_FREE. We don't need a membar_enter()
18070Sstevel@tonic-gate * between zeroing pend and setting the flags because we're at
18080Sstevel@tonic-gate * CY_HIGH_LEVEL (that is, the zeroing of pend and the setting
18090Sstevel@tonic-gate * of cy_flags appear atomic to softints).
18100Sstevel@tonic-gate */
18110Sstevel@tonic-gate cyclic->cy_flags = CYF_FREE;
18120Sstevel@tonic-gate
18130Sstevel@tonic-gate for (i = 0; i < nelems; i++) {
18140Sstevel@tonic-gate if (heap[i] == ndx)
18150Sstevel@tonic-gate break;
18160Sstevel@tonic-gate }
18170Sstevel@tonic-gate
18180Sstevel@tonic-gate if (i == nelems)
18190Sstevel@tonic-gate panic("attempt to remove non-existent cyclic");
18200Sstevel@tonic-gate
18210Sstevel@tonic-gate cpu->cyp_nelems = --nelems;
18220Sstevel@tonic-gate
18230Sstevel@tonic-gate if (nelems == 0) {
18240Sstevel@tonic-gate /*
18250Sstevel@tonic-gate * If we just removed the last element, then we need to
18260Sstevel@tonic-gate * disable the backend on this CPU.
18270Sstevel@tonic-gate */
18280Sstevel@tonic-gate CYC_TRACE0(cpu, CY_HIGH_LEVEL, "disabled");
18290Sstevel@tonic-gate be->cyb_disable(bar);
18300Sstevel@tonic-gate }
18310Sstevel@tonic-gate
18320Sstevel@tonic-gate if (i == nelems) {
18330Sstevel@tonic-gate /*
18340Sstevel@tonic-gate * If we just removed the last element of the heap, then
18350Sstevel@tonic-gate * we don't have to downheap.
18360Sstevel@tonic-gate */
18370Sstevel@tonic-gate CYC_TRACE0(cpu, CY_HIGH_LEVEL, "remove-bottom");
18380Sstevel@tonic-gate goto out;
18390Sstevel@tonic-gate }
18400Sstevel@tonic-gate
18410Sstevel@tonic-gate #ifdef DEBUG
18420Sstevel@tonic-gate root = heap[0];
18430Sstevel@tonic-gate #endif
18440Sstevel@tonic-gate
18450Sstevel@tonic-gate /*
18460Sstevel@tonic-gate * Swap the last element of the heap with the one we want to
18470Sstevel@tonic-gate * remove, and downheap (this has the implicit effect of putting
18480Sstevel@tonic-gate * the newly freed element on the free list).
18490Sstevel@tonic-gate */
18500Sstevel@tonic-gate heap[i] = (last = heap[nelems]);
18510Sstevel@tonic-gate heap[nelems] = ndx;
18520Sstevel@tonic-gate
18530Sstevel@tonic-gate if (i == 0) {
18540Sstevel@tonic-gate CYC_TRACE0(cpu, CY_HIGH_LEVEL, "remove-root");
18550Sstevel@tonic-gate cyclic_downheap(cpu, 0);
18560Sstevel@tonic-gate } else {
18570Sstevel@tonic-gate if (cyclic_upheap(cpu, i) == 0) {
18580Sstevel@tonic-gate /*
18590Sstevel@tonic-gate * The upheap didn't propagate to the root; if it
18600Sstevel@tonic-gate * didn't propagate at all, we need to downheap.
18610Sstevel@tonic-gate */
18620Sstevel@tonic-gate CYC_TRACE0(cpu, CY_HIGH_LEVEL, "remove-no-root");
18630Sstevel@tonic-gate if (heap[i] == last) {
18640Sstevel@tonic-gate CYC_TRACE0(cpu, CY_HIGH_LEVEL, "remove-no-up");
18650Sstevel@tonic-gate cyclic_downheap(cpu, i);
18660Sstevel@tonic-gate }
18670Sstevel@tonic-gate ASSERT(heap[0] == root);
18680Sstevel@tonic-gate goto out;
18690Sstevel@tonic-gate }
18700Sstevel@tonic-gate }
18710Sstevel@tonic-gate
18720Sstevel@tonic-gate /*
18730Sstevel@tonic-gate * We're here because we changed the root; we need to reprogram
18740Sstevel@tonic-gate * the clock source.
18750Sstevel@tonic-gate */
18760Sstevel@tonic-gate cyclic = &cpu->cyp_cyclics[heap[0]];
18770Sstevel@tonic-gate
18780Sstevel@tonic-gate CYC_TRACE0(cpu, CY_HIGH_LEVEL, "remove-reprog");
18790Sstevel@tonic-gate
18800Sstevel@tonic-gate ASSERT(nelems != 0);
18810Sstevel@tonic-gate be->cyb_reprogram(bar, cyclic->cy_expire);
18820Sstevel@tonic-gate out:
18830Sstevel@tonic-gate be->cyb_restore_level(bar, cookie);
18840Sstevel@tonic-gate }
18850Sstevel@tonic-gate
18860Sstevel@tonic-gate static int
cyclic_remove_here(cyc_cpu_t * cpu,cyc_index_t ndx,cyc_time_t * when,int wait)18870Sstevel@tonic-gate cyclic_remove_here(cyc_cpu_t *cpu, cyc_index_t ndx, cyc_time_t *when, int wait)
18880Sstevel@tonic-gate {
18890Sstevel@tonic-gate cyc_backend_t *be = cpu->cyp_backend;
18900Sstevel@tonic-gate cyc_xcallarg_t arg;
18910Sstevel@tonic-gate cyclic_t *cyclic = &cpu->cyp_cyclics[ndx];
18920Sstevel@tonic-gate cyc_level_t level = cyclic->cy_level;
18930Sstevel@tonic-gate
18940Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
18950Sstevel@tonic-gate ASSERT(cpu->cyp_rpend == 0);
18960Sstevel@tonic-gate ASSERT(wait == CY_WAIT || wait == CY_NOWAIT);
18970Sstevel@tonic-gate
18980Sstevel@tonic-gate arg.cyx_ndx = ndx;
18990Sstevel@tonic-gate arg.cyx_cpu = cpu;
19000Sstevel@tonic-gate arg.cyx_when = when;
19010Sstevel@tonic-gate arg.cyx_wait = wait;
19020Sstevel@tonic-gate
19030Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE);
19040Sstevel@tonic-gate cpu->cyp_state = CYS_REMOVING;
19050Sstevel@tonic-gate
19060Sstevel@tonic-gate be->cyb_xcall(be->cyb_arg, cpu->cyp_cpu,
19070Sstevel@tonic-gate (cyc_func_t)cyclic_remove_xcall, &arg);
19080Sstevel@tonic-gate
19090Sstevel@tonic-gate /*
19100Sstevel@tonic-gate * If the cyclic we removed wasn't at CY_HIGH_LEVEL, then we need to
19110Sstevel@tonic-gate * check the cyp_rpend. If it's non-zero, then we need to wait here
19120Sstevel@tonic-gate * for all pending cyclic handlers to run.
19130Sstevel@tonic-gate */
19140Sstevel@tonic-gate ASSERT(!(level == CY_HIGH_LEVEL && cpu->cyp_rpend != 0));
19150Sstevel@tonic-gate ASSERT(!(wait == CY_NOWAIT && cpu->cyp_rpend != 0));
19160Sstevel@tonic-gate ASSERT(!(arg.cyx_wait == CY_NOWAIT && cpu->cyp_rpend != 0));
19170Sstevel@tonic-gate
19180Sstevel@tonic-gate if (wait != arg.cyx_wait) {
19190Sstevel@tonic-gate /*
19200Sstevel@tonic-gate * We are being told that we must wait if we want to
19210Sstevel@tonic-gate * remove this cyclic; put the CPU back in the CYS_ONLINE
19220Sstevel@tonic-gate * state and return failure.
19230Sstevel@tonic-gate */
19240Sstevel@tonic-gate ASSERT(wait == CY_NOWAIT && arg.cyx_wait == CY_WAIT);
19250Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_REMOVING);
19260Sstevel@tonic-gate cpu->cyp_state = CYS_ONLINE;
19270Sstevel@tonic-gate
19280Sstevel@tonic-gate return (0);
19290Sstevel@tonic-gate }
19300Sstevel@tonic-gate
19310Sstevel@tonic-gate if (cpu->cyp_rpend != 0)
19320Sstevel@tonic-gate sema_p(&cpu->cyp_modify_wait);
19330Sstevel@tonic-gate
19340Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_REMOVING);
19350Sstevel@tonic-gate
19360Sstevel@tonic-gate cpu->cyp_rpend = 0;
19370Sstevel@tonic-gate cpu->cyp_state = CYS_ONLINE;
19380Sstevel@tonic-gate
19390Sstevel@tonic-gate return (1);
19400Sstevel@tonic-gate }
19410Sstevel@tonic-gate
19420Sstevel@tonic-gate /*
1943*8048SMadhavan.Venkataraman@Sun.COM * If cyclic_reprogram() is called on the same CPU as the cyclic's CPU, then
1944*8048SMadhavan.Venkataraman@Sun.COM * it calls this function directly. Else, it invokes this function through
1945*8048SMadhavan.Venkataraman@Sun.COM * an X-call to the cyclic's CPU.
1946*8048SMadhavan.Venkataraman@Sun.COM */
1947*8048SMadhavan.Venkataraman@Sun.COM static void
cyclic_reprogram_cyclic(cyc_cpu_t * cpu,cyc_index_t ndx,hrtime_t expire)1948*8048SMadhavan.Venkataraman@Sun.COM cyclic_reprogram_cyclic(cyc_cpu_t *cpu, cyc_index_t ndx, hrtime_t expire)
1949*8048SMadhavan.Venkataraman@Sun.COM {
1950*8048SMadhavan.Venkataraman@Sun.COM cyc_backend_t *be = cpu->cyp_backend;
1951*8048SMadhavan.Venkataraman@Sun.COM cyb_arg_t bar = be->cyb_arg;
1952*8048SMadhavan.Venkataraman@Sun.COM cyc_cookie_t cookie;
1953*8048SMadhavan.Venkataraman@Sun.COM cyc_index_t nelems, i;
1954*8048SMadhavan.Venkataraman@Sun.COM cyc_index_t *heap;
1955*8048SMadhavan.Venkataraman@Sun.COM cyclic_t *cyclic;
1956*8048SMadhavan.Venkataraman@Sun.COM hrtime_t oexpire;
1957*8048SMadhavan.Venkataraman@Sun.COM int reprog;
1958*8048SMadhavan.Venkataraman@Sun.COM
1959*8048SMadhavan.Venkataraman@Sun.COM cookie = be->cyb_set_level(bar, CY_HIGH_LEVEL);
1960*8048SMadhavan.Venkataraman@Sun.COM
1961*8048SMadhavan.Venkataraman@Sun.COM CYC_TRACE1(cpu, CY_HIGH_LEVEL, "reprog-xcall", ndx);
1962*8048SMadhavan.Venkataraman@Sun.COM
1963*8048SMadhavan.Venkataraman@Sun.COM nelems = cpu->cyp_nelems;
1964*8048SMadhavan.Venkataraman@Sun.COM ASSERT(nelems > 0);
1965*8048SMadhavan.Venkataraman@Sun.COM heap = cpu->cyp_heap;
1966*8048SMadhavan.Venkataraman@Sun.COM
1967*8048SMadhavan.Venkataraman@Sun.COM /*
1968*8048SMadhavan.Venkataraman@Sun.COM * Reprogrammed cyclics are typically one-shot ones that get
1969*8048SMadhavan.Venkataraman@Sun.COM * set to infinity on every expiration. We shorten the search by
1970*8048SMadhavan.Venkataraman@Sun.COM * searching from the bottom of the heap to the top instead of the
1971*8048SMadhavan.Venkataraman@Sun.COM * other way around.
1972*8048SMadhavan.Venkataraman@Sun.COM */
1973*8048SMadhavan.Venkataraman@Sun.COM for (i = nelems - 1; i >= 0; i--) {
1974*8048SMadhavan.Venkataraman@Sun.COM if (heap[i] == ndx)
1975*8048SMadhavan.Venkataraman@Sun.COM break;
1976*8048SMadhavan.Venkataraman@Sun.COM }
1977*8048SMadhavan.Venkataraman@Sun.COM if (i < 0)
1978*8048SMadhavan.Venkataraman@Sun.COM panic("attempt to reprogram non-existent cyclic");
1979*8048SMadhavan.Venkataraman@Sun.COM
1980*8048SMadhavan.Venkataraman@Sun.COM cyclic = &cpu->cyp_cyclics[ndx];
1981*8048SMadhavan.Venkataraman@Sun.COM oexpire = cyclic->cy_expire;
1982*8048SMadhavan.Venkataraman@Sun.COM cyclic->cy_expire = expire;
1983*8048SMadhavan.Venkataraman@Sun.COM
1984*8048SMadhavan.Venkataraman@Sun.COM reprog = (i == 0);
1985*8048SMadhavan.Venkataraman@Sun.COM if (expire > oexpire) {
1986*8048SMadhavan.Venkataraman@Sun.COM CYC_TRACE1(cpu, CY_HIGH_LEVEL, "reprog-down", i);
1987*8048SMadhavan.Venkataraman@Sun.COM cyclic_downheap(cpu, i);
1988*8048SMadhavan.Venkataraman@Sun.COM } else if (i > 0) {
1989*8048SMadhavan.Venkataraman@Sun.COM CYC_TRACE1(cpu, CY_HIGH_LEVEL, "reprog-up", i);
1990*8048SMadhavan.Venkataraman@Sun.COM reprog = cyclic_upheap(cpu, i);
1991*8048SMadhavan.Venkataraman@Sun.COM }
1992*8048SMadhavan.Venkataraman@Sun.COM
1993*8048SMadhavan.Venkataraman@Sun.COM if (reprog && (cpu->cyp_state != CYS_SUSPENDED)) {
1994*8048SMadhavan.Venkataraman@Sun.COM /*
1995*8048SMadhavan.Venkataraman@Sun.COM * The root changed. Reprogram the clock source.
1996*8048SMadhavan.Venkataraman@Sun.COM */
1997*8048SMadhavan.Venkataraman@Sun.COM CYC_TRACE0(cpu, CY_HIGH_LEVEL, "reprog-root");
1998*8048SMadhavan.Venkataraman@Sun.COM cyclic = &cpu->cyp_cyclics[heap[0]];
1999*8048SMadhavan.Venkataraman@Sun.COM be->cyb_reprogram(bar, cyclic->cy_expire);
2000*8048SMadhavan.Venkataraman@Sun.COM }
2001*8048SMadhavan.Venkataraman@Sun.COM
2002*8048SMadhavan.Venkataraman@Sun.COM be->cyb_restore_level(bar, cookie);
2003*8048SMadhavan.Venkataraman@Sun.COM }
2004*8048SMadhavan.Venkataraman@Sun.COM
2005*8048SMadhavan.Venkataraman@Sun.COM static void
cyclic_reprogram_xcall(cyc_xcallarg_t * arg)2006*8048SMadhavan.Venkataraman@Sun.COM cyclic_reprogram_xcall(cyc_xcallarg_t *arg)
2007*8048SMadhavan.Venkataraman@Sun.COM {
2008*8048SMadhavan.Venkataraman@Sun.COM cyclic_reprogram_cyclic(arg->cyx_cpu, arg->cyx_ndx,
2009*8048SMadhavan.Venkataraman@Sun.COM arg->cyx_when->cyt_when);
2010*8048SMadhavan.Venkataraman@Sun.COM }
2011*8048SMadhavan.Venkataraman@Sun.COM
2012*8048SMadhavan.Venkataraman@Sun.COM static void
cyclic_reprogram_here(cyc_cpu_t * cpu,cyc_index_t ndx,hrtime_t expiration)2013*8048SMadhavan.Venkataraman@Sun.COM cyclic_reprogram_here(cyc_cpu_t *cpu, cyc_index_t ndx, hrtime_t expiration)
2014*8048SMadhavan.Venkataraman@Sun.COM {
2015*8048SMadhavan.Venkataraman@Sun.COM cyc_backend_t *be = cpu->cyp_backend;
2016*8048SMadhavan.Venkataraman@Sun.COM cyc_xcallarg_t arg;
2017*8048SMadhavan.Venkataraman@Sun.COM cyc_time_t when;
2018*8048SMadhavan.Venkataraman@Sun.COM
2019*8048SMadhavan.Venkataraman@Sun.COM ASSERT(expiration > 0);
2020*8048SMadhavan.Venkataraman@Sun.COM
2021*8048SMadhavan.Venkataraman@Sun.COM arg.cyx_ndx = ndx;
2022*8048SMadhavan.Venkataraman@Sun.COM arg.cyx_cpu = cpu;
2023*8048SMadhavan.Venkataraman@Sun.COM arg.cyx_when = &when;
2024*8048SMadhavan.Venkataraman@Sun.COM when.cyt_when = expiration;
2025*8048SMadhavan.Venkataraman@Sun.COM
2026*8048SMadhavan.Venkataraman@Sun.COM be->cyb_xcall(be->cyb_arg, cpu->cyp_cpu,
2027*8048SMadhavan.Venkataraman@Sun.COM (cyc_func_t)cyclic_reprogram_xcall, &arg);
2028*8048SMadhavan.Venkataraman@Sun.COM }
2029*8048SMadhavan.Venkataraman@Sun.COM
2030*8048SMadhavan.Venkataraman@Sun.COM /*
20310Sstevel@tonic-gate * cyclic_juggle_one_to() should only be called when the source cyclic
20320Sstevel@tonic-gate * can be juggled and the destination CPU is known to be able to accept
20330Sstevel@tonic-gate * it.
20340Sstevel@tonic-gate */
20350Sstevel@tonic-gate static void
cyclic_juggle_one_to(cyc_id_t * idp,cyc_cpu_t * dest)20360Sstevel@tonic-gate cyclic_juggle_one_to(cyc_id_t *idp, cyc_cpu_t *dest)
20370Sstevel@tonic-gate {
20380Sstevel@tonic-gate cyc_cpu_t *src = idp->cyi_cpu;
20390Sstevel@tonic-gate cyc_index_t ndx = idp->cyi_ndx;
20400Sstevel@tonic-gate cyc_time_t when;
20410Sstevel@tonic-gate cyc_handler_t hdlr;
20420Sstevel@tonic-gate cyclic_t *cyclic;
20430Sstevel@tonic-gate uint16_t flags;
20440Sstevel@tonic-gate hrtime_t delay;
20450Sstevel@tonic-gate
20460Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
20470Sstevel@tonic-gate ASSERT(src != NULL && idp->cyi_omni_list == NULL);
20480Sstevel@tonic-gate ASSERT(!(dest->cyp_cpu->cpu_flags & (CPU_QUIESCED | CPU_OFFLINE)));
20490Sstevel@tonic-gate CYC_PTRACE("juggle-one-to", idp, dest);
20500Sstevel@tonic-gate
20510Sstevel@tonic-gate cyclic = &src->cyp_cyclics[ndx];
20520Sstevel@tonic-gate
20530Sstevel@tonic-gate flags = cyclic->cy_flags;
20540Sstevel@tonic-gate ASSERT(!(flags & CYF_CPU_BOUND) && !(flags & CYF_FREE));
20550Sstevel@tonic-gate
20560Sstevel@tonic-gate hdlr.cyh_func = cyclic->cy_handler;
20570Sstevel@tonic-gate hdlr.cyh_level = cyclic->cy_level;
20580Sstevel@tonic-gate hdlr.cyh_arg = cyclic->cy_arg;
20590Sstevel@tonic-gate
20600Sstevel@tonic-gate /*
20610Sstevel@tonic-gate * Before we begin the juggling process, see if the destination
20620Sstevel@tonic-gate * CPU requires an expansion. If it does, we'll perform the
20630Sstevel@tonic-gate * expansion before removing the cyclic. This is to prevent us
20640Sstevel@tonic-gate * from blocking while a system-critical cyclic (notably, the clock
20650Sstevel@tonic-gate * cyclic) isn't on a CPU.
20660Sstevel@tonic-gate */
20670Sstevel@tonic-gate if (dest->cyp_nelems == dest->cyp_size) {
20680Sstevel@tonic-gate CYC_PTRACE("remove-expand", idp, dest);
20690Sstevel@tonic-gate cyclic_expand(dest);
20700Sstevel@tonic-gate ASSERT(dest->cyp_nelems < dest->cyp_size);
20710Sstevel@tonic-gate }
20720Sstevel@tonic-gate
20730Sstevel@tonic-gate /*
2074*8048SMadhavan.Venkataraman@Sun.COM * Prevent a reprogram of this cyclic while we are relocating it.
2075*8048SMadhavan.Venkataraman@Sun.COM * Otherwise, cyclic_reprogram_here() will end up sending an X-call
2076*8048SMadhavan.Venkataraman@Sun.COM * to the wrong CPU.
2077*8048SMadhavan.Venkataraman@Sun.COM */
2078*8048SMadhavan.Venkataraman@Sun.COM rw_enter(&idp->cyi_lock, RW_WRITER);
2079*8048SMadhavan.Venkataraman@Sun.COM
2080*8048SMadhavan.Venkataraman@Sun.COM /*
20810Sstevel@tonic-gate * Remove the cyclic from the source. As mentioned above, we cannot
20820Sstevel@tonic-gate * block during this operation; if we cannot remove the cyclic
20830Sstevel@tonic-gate * without waiting, we spin for a time shorter than the interval, and
20840Sstevel@tonic-gate * reattempt the (non-blocking) removal. If we continue to fail,
20850Sstevel@tonic-gate * we will exponentially back off (up to half of the interval).
20860Sstevel@tonic-gate * Note that the removal will ultimately succeed -- even if the
20870Sstevel@tonic-gate * cyclic handler is blocked on a resource held by a thread which we
20880Sstevel@tonic-gate * have preempted, priority inheritance assures that the preempted
20890Sstevel@tonic-gate * thread will preempt us and continue to progress.
20900Sstevel@tonic-gate */
20910Sstevel@tonic-gate for (delay = NANOSEC / MICROSEC; ; delay <<= 1) {
20920Sstevel@tonic-gate /*
20930Sstevel@tonic-gate * Before we begin this operation, disable kernel preemption.
20940Sstevel@tonic-gate */
20950Sstevel@tonic-gate kpreempt_disable();
20960Sstevel@tonic-gate if (cyclic_remove_here(src, ndx, &when, CY_NOWAIT))
20970Sstevel@tonic-gate break;
20980Sstevel@tonic-gate
20990Sstevel@tonic-gate /*
21000Sstevel@tonic-gate * The operation failed; enable kernel preemption while
21010Sstevel@tonic-gate * spinning.
21020Sstevel@tonic-gate */
21030Sstevel@tonic-gate kpreempt_enable();
21040Sstevel@tonic-gate
21050Sstevel@tonic-gate CYC_PTRACE("remove-retry", idp, src);
21060Sstevel@tonic-gate
21070Sstevel@tonic-gate if (delay > (cyclic->cy_interval >> 1))
21080Sstevel@tonic-gate delay = cyclic->cy_interval >> 1;
21090Sstevel@tonic-gate
2110*8048SMadhavan.Venkataraman@Sun.COM /*
2111*8048SMadhavan.Venkataraman@Sun.COM * Drop the RW lock to avoid a deadlock with the cyclic
2112*8048SMadhavan.Venkataraman@Sun.COM * handler (because it can potentially call cyclic_reprogram().
2113*8048SMadhavan.Venkataraman@Sun.COM */
2114*8048SMadhavan.Venkataraman@Sun.COM rw_exit(&idp->cyi_lock);
21150Sstevel@tonic-gate drv_usecwait((clock_t)(delay / (NANOSEC / MICROSEC)));
2116*8048SMadhavan.Venkataraman@Sun.COM rw_enter(&idp->cyi_lock, RW_WRITER);
21170Sstevel@tonic-gate }
21180Sstevel@tonic-gate
21190Sstevel@tonic-gate /*
21200Sstevel@tonic-gate * Now add the cyclic to the destination. This won't block; we
21210Sstevel@tonic-gate * performed any necessary (blocking) expansion of the destination
21220Sstevel@tonic-gate * CPU before removing the cyclic from the source CPU.
21230Sstevel@tonic-gate */
21240Sstevel@tonic-gate idp->cyi_ndx = cyclic_add_here(dest, &hdlr, &when, flags);
21250Sstevel@tonic-gate idp->cyi_cpu = dest;
21260Sstevel@tonic-gate kpreempt_enable();
2127*8048SMadhavan.Venkataraman@Sun.COM
2128*8048SMadhavan.Venkataraman@Sun.COM /*
2129*8048SMadhavan.Venkataraman@Sun.COM * Now that we have successfully relocated the cyclic, allow
2130*8048SMadhavan.Venkataraman@Sun.COM * it to be reprogrammed.
2131*8048SMadhavan.Venkataraman@Sun.COM */
2132*8048SMadhavan.Venkataraman@Sun.COM rw_exit(&idp->cyi_lock);
21330Sstevel@tonic-gate }
21340Sstevel@tonic-gate
21350Sstevel@tonic-gate static int
cyclic_juggle_one(cyc_id_t * idp)21360Sstevel@tonic-gate cyclic_juggle_one(cyc_id_t *idp)
21370Sstevel@tonic-gate {
21380Sstevel@tonic-gate cyc_index_t ndx = idp->cyi_ndx;
21390Sstevel@tonic-gate cyc_cpu_t *cpu = idp->cyi_cpu, *dest;
21400Sstevel@tonic-gate cyclic_t *cyclic = &cpu->cyp_cyclics[ndx];
21410Sstevel@tonic-gate cpu_t *c = cpu->cyp_cpu;
21420Sstevel@tonic-gate cpupart_t *part = c->cpu_part;
21430Sstevel@tonic-gate
21440Sstevel@tonic-gate CYC_PTRACE("juggle-one", idp, cpu);
21450Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
21460Sstevel@tonic-gate ASSERT(!(c->cpu_flags & CPU_OFFLINE));
21470Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE);
21480Sstevel@tonic-gate ASSERT(!(cyclic->cy_flags & CYF_FREE));
21490Sstevel@tonic-gate
21500Sstevel@tonic-gate if ((dest = cyclic_pick_cpu(part, c, c, cyclic->cy_flags)) == NULL) {
21510Sstevel@tonic-gate /*
21520Sstevel@tonic-gate * Bad news: this cyclic can't be juggled.
21530Sstevel@tonic-gate */
21540Sstevel@tonic-gate CYC_PTRACE("juggle-fail", idp, cpu)
21550Sstevel@tonic-gate return (0);
21560Sstevel@tonic-gate }
21570Sstevel@tonic-gate
21580Sstevel@tonic-gate cyclic_juggle_one_to(idp, dest);
21590Sstevel@tonic-gate
21600Sstevel@tonic-gate return (1);
21610Sstevel@tonic-gate }
21620Sstevel@tonic-gate
21630Sstevel@tonic-gate static void
cyclic_unbind_cpu(cyclic_id_t id)21640Sstevel@tonic-gate cyclic_unbind_cpu(cyclic_id_t id)
21650Sstevel@tonic-gate {
21660Sstevel@tonic-gate cyc_id_t *idp = (cyc_id_t *)id;
21670Sstevel@tonic-gate cyc_cpu_t *cpu = idp->cyi_cpu;
21680Sstevel@tonic-gate cpu_t *c = cpu->cyp_cpu;
21690Sstevel@tonic-gate cyclic_t *cyclic = &cpu->cyp_cyclics[idp->cyi_ndx];
21700Sstevel@tonic-gate
21710Sstevel@tonic-gate CYC_PTRACE("unbind-cpu", id, cpu);
21720Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
21730Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE);
21740Sstevel@tonic-gate ASSERT(!(cyclic->cy_flags & CYF_FREE));
21750Sstevel@tonic-gate ASSERT(cyclic->cy_flags & CYF_CPU_BOUND);
21760Sstevel@tonic-gate
21770Sstevel@tonic-gate cyclic->cy_flags &= ~CYF_CPU_BOUND;
21780Sstevel@tonic-gate
21790Sstevel@tonic-gate /*
21800Sstevel@tonic-gate * If we were bound to CPU which has interrupts disabled, we need
21810Sstevel@tonic-gate * to juggle away. This can only fail if we are bound to a
21820Sstevel@tonic-gate * processor set, and if every CPU in the processor set has
21830Sstevel@tonic-gate * interrupts disabled.
21840Sstevel@tonic-gate */
21850Sstevel@tonic-gate if (!(c->cpu_flags & CPU_ENABLE)) {
21860Sstevel@tonic-gate int res = cyclic_juggle_one(idp);
21870Sstevel@tonic-gate
21880Sstevel@tonic-gate ASSERT((res && idp->cyi_cpu != cpu) ||
21890Sstevel@tonic-gate (!res && (cyclic->cy_flags & CYF_PART_BOUND)));
21900Sstevel@tonic-gate }
21910Sstevel@tonic-gate }
21920Sstevel@tonic-gate
21930Sstevel@tonic-gate static void
cyclic_bind_cpu(cyclic_id_t id,cpu_t * d)21940Sstevel@tonic-gate cyclic_bind_cpu(cyclic_id_t id, cpu_t *d)
21950Sstevel@tonic-gate {
21960Sstevel@tonic-gate cyc_id_t *idp = (cyc_id_t *)id;
21970Sstevel@tonic-gate cyc_cpu_t *dest = d->cpu_cyclic, *cpu = idp->cyi_cpu;
21980Sstevel@tonic-gate cpu_t *c = cpu->cyp_cpu;
21990Sstevel@tonic-gate cyclic_t *cyclic = &cpu->cyp_cyclics[idp->cyi_ndx];
22000Sstevel@tonic-gate cpupart_t *part = c->cpu_part;
22010Sstevel@tonic-gate
22020Sstevel@tonic-gate CYC_PTRACE("bind-cpu", id, dest);
22030Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
22040Sstevel@tonic-gate ASSERT(!(d->cpu_flags & CPU_OFFLINE));
22050Sstevel@tonic-gate ASSERT(!(c->cpu_flags & CPU_OFFLINE));
22060Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE);
22070Sstevel@tonic-gate ASSERT(dest != NULL);
22080Sstevel@tonic-gate ASSERT(dest->cyp_state == CYS_ONLINE);
22090Sstevel@tonic-gate ASSERT(!(cyclic->cy_flags & CYF_FREE));
22100Sstevel@tonic-gate ASSERT(!(cyclic->cy_flags & CYF_CPU_BOUND));
22110Sstevel@tonic-gate
22120Sstevel@tonic-gate dest = cyclic_pick_cpu(part, d, NULL, cyclic->cy_flags | CYF_CPU_BOUND);
22130Sstevel@tonic-gate
22140Sstevel@tonic-gate if (dest != cpu) {
22150Sstevel@tonic-gate cyclic_juggle_one_to(idp, dest);
22160Sstevel@tonic-gate cyclic = &dest->cyp_cyclics[idp->cyi_ndx];
22170Sstevel@tonic-gate }
22180Sstevel@tonic-gate
22190Sstevel@tonic-gate cyclic->cy_flags |= CYF_CPU_BOUND;
22200Sstevel@tonic-gate }
22210Sstevel@tonic-gate
22220Sstevel@tonic-gate static void
cyclic_unbind_cpupart(cyclic_id_t id)22230Sstevel@tonic-gate cyclic_unbind_cpupart(cyclic_id_t id)
22240Sstevel@tonic-gate {
22250Sstevel@tonic-gate cyc_id_t *idp = (cyc_id_t *)id;
22260Sstevel@tonic-gate cyc_cpu_t *cpu = idp->cyi_cpu;
22270Sstevel@tonic-gate cpu_t *c = cpu->cyp_cpu;
22280Sstevel@tonic-gate cyclic_t *cyc = &cpu->cyp_cyclics[idp->cyi_ndx];
22290Sstevel@tonic-gate
22300Sstevel@tonic-gate CYC_PTRACE("unbind-part", idp, c->cpu_part);
22310Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
22320Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE);
22330Sstevel@tonic-gate ASSERT(!(cyc->cy_flags & CYF_FREE));
22340Sstevel@tonic-gate ASSERT(cyc->cy_flags & CYF_PART_BOUND);
22350Sstevel@tonic-gate
22360Sstevel@tonic-gate cyc->cy_flags &= ~CYF_PART_BOUND;
22370Sstevel@tonic-gate
22380Sstevel@tonic-gate /*
22390Sstevel@tonic-gate * If we're on a CPU which has interrupts disabled (and if this cyclic
22400Sstevel@tonic-gate * isn't bound to the CPU), we need to juggle away.
22410Sstevel@tonic-gate */
22420Sstevel@tonic-gate if (!(c->cpu_flags & CPU_ENABLE) && !(cyc->cy_flags & CYF_CPU_BOUND)) {
22430Sstevel@tonic-gate int res = cyclic_juggle_one(idp);
22440Sstevel@tonic-gate
22450Sstevel@tonic-gate ASSERT(res && idp->cyi_cpu != cpu);
22460Sstevel@tonic-gate }
22470Sstevel@tonic-gate }
22480Sstevel@tonic-gate
22490Sstevel@tonic-gate static void
cyclic_bind_cpupart(cyclic_id_t id,cpupart_t * part)22500Sstevel@tonic-gate cyclic_bind_cpupart(cyclic_id_t id, cpupart_t *part)
22510Sstevel@tonic-gate {
22520Sstevel@tonic-gate cyc_id_t *idp = (cyc_id_t *)id;
22530Sstevel@tonic-gate cyc_cpu_t *cpu = idp->cyi_cpu, *dest;
22540Sstevel@tonic-gate cpu_t *c = cpu->cyp_cpu;
22550Sstevel@tonic-gate cyclic_t *cyc = &cpu->cyp_cyclics[idp->cyi_ndx];
22560Sstevel@tonic-gate
22570Sstevel@tonic-gate CYC_PTRACE("bind-part", idp, part);
22580Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
22590Sstevel@tonic-gate ASSERT(!(c->cpu_flags & CPU_OFFLINE));
22600Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE);
22610Sstevel@tonic-gate ASSERT(!(cyc->cy_flags & CYF_FREE));
22620Sstevel@tonic-gate ASSERT(!(cyc->cy_flags & CYF_PART_BOUND));
22630Sstevel@tonic-gate ASSERT(part->cp_ncpus > 0);
22640Sstevel@tonic-gate
22650Sstevel@tonic-gate dest = cyclic_pick_cpu(part, c, NULL, cyc->cy_flags | CYF_PART_BOUND);
22660Sstevel@tonic-gate
22670Sstevel@tonic-gate if (dest != cpu) {
22680Sstevel@tonic-gate cyclic_juggle_one_to(idp, dest);
22690Sstevel@tonic-gate cyc = &dest->cyp_cyclics[idp->cyi_ndx];
22700Sstevel@tonic-gate }
22710Sstevel@tonic-gate
22720Sstevel@tonic-gate cyc->cy_flags |= CYF_PART_BOUND;
22730Sstevel@tonic-gate }
22740Sstevel@tonic-gate
22750Sstevel@tonic-gate static void
cyclic_configure(cpu_t * c)22760Sstevel@tonic-gate cyclic_configure(cpu_t *c)
22770Sstevel@tonic-gate {
22780Sstevel@tonic-gate cyc_cpu_t *cpu = kmem_zalloc(sizeof (cyc_cpu_t), KM_SLEEP);
22790Sstevel@tonic-gate cyc_backend_t *nbe = kmem_zalloc(sizeof (cyc_backend_t), KM_SLEEP);
22800Sstevel@tonic-gate int i;
22810Sstevel@tonic-gate
22820Sstevel@tonic-gate CYC_PTRACE1("configure", cpu);
22830Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
22840Sstevel@tonic-gate
22850Sstevel@tonic-gate if (cyclic_id_cache == NULL)
22860Sstevel@tonic-gate cyclic_id_cache = kmem_cache_create("cyclic_id_cache",
22870Sstevel@tonic-gate sizeof (cyc_id_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
22880Sstevel@tonic-gate
22890Sstevel@tonic-gate cpu->cyp_cpu = c;
22900Sstevel@tonic-gate
22910Sstevel@tonic-gate sema_init(&cpu->cyp_modify_wait, 0, NULL, SEMA_DEFAULT, NULL);
22920Sstevel@tonic-gate
22930Sstevel@tonic-gate cpu->cyp_size = 1;
22940Sstevel@tonic-gate cpu->cyp_heap = kmem_zalloc(sizeof (cyc_index_t), KM_SLEEP);
22950Sstevel@tonic-gate cpu->cyp_cyclics = kmem_zalloc(sizeof (cyclic_t), KM_SLEEP);
22960Sstevel@tonic-gate cpu->cyp_cyclics->cy_flags = CYF_FREE;
22970Sstevel@tonic-gate
22980Sstevel@tonic-gate for (i = CY_LOW_LEVEL; i < CY_LOW_LEVEL + CY_SOFT_LEVELS; i++) {
22990Sstevel@tonic-gate /*
23000Sstevel@tonic-gate * We don't need to set the sizemask; it's already zero
23010Sstevel@tonic-gate * (which is the appropriate sizemask for a size of 1).
23020Sstevel@tonic-gate */
23030Sstevel@tonic-gate cpu->cyp_softbuf[i].cys_buf[0].cypc_buf =
23040Sstevel@tonic-gate kmem_alloc(sizeof (cyc_index_t), KM_SLEEP);
23050Sstevel@tonic-gate }
23060Sstevel@tonic-gate
23070Sstevel@tonic-gate cpu->cyp_state = CYS_OFFLINE;
23080Sstevel@tonic-gate
23090Sstevel@tonic-gate /*
23100Sstevel@tonic-gate * Setup the backend for this CPU.
23110Sstevel@tonic-gate */
23120Sstevel@tonic-gate bcopy(&cyclic_backend, nbe, sizeof (cyc_backend_t));
23130Sstevel@tonic-gate nbe->cyb_arg = nbe->cyb_configure(c);
23140Sstevel@tonic-gate cpu->cyp_backend = nbe;
23150Sstevel@tonic-gate
23160Sstevel@tonic-gate /*
23170Sstevel@tonic-gate * On platforms where stray interrupts may be taken during startup,
23180Sstevel@tonic-gate * the CPU's cpu_cyclic pointer serves as an indicator that the
23190Sstevel@tonic-gate * cyclic subsystem for this CPU is prepared to field interrupts.
23200Sstevel@tonic-gate */
23210Sstevel@tonic-gate membar_producer();
23220Sstevel@tonic-gate
23230Sstevel@tonic-gate c->cpu_cyclic = cpu;
23240Sstevel@tonic-gate }
23250Sstevel@tonic-gate
23260Sstevel@tonic-gate static void
cyclic_unconfigure(cpu_t * c)23270Sstevel@tonic-gate cyclic_unconfigure(cpu_t *c)
23280Sstevel@tonic-gate {
23290Sstevel@tonic-gate cyc_cpu_t *cpu = c->cpu_cyclic;
23300Sstevel@tonic-gate cyc_backend_t *be = cpu->cyp_backend;
23310Sstevel@tonic-gate cyb_arg_t bar = be->cyb_arg;
23320Sstevel@tonic-gate int i;
23330Sstevel@tonic-gate
23340Sstevel@tonic-gate CYC_PTRACE1("unconfigure", cpu);
23350Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
23360Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_OFFLINE);
23370Sstevel@tonic-gate ASSERT(cpu->cyp_nelems == 0);
23380Sstevel@tonic-gate
23390Sstevel@tonic-gate /*
23400Sstevel@tonic-gate * Let the backend know that the CPU is being yanked, and free up
23410Sstevel@tonic-gate * the backend structure.
23420Sstevel@tonic-gate */
23430Sstevel@tonic-gate be->cyb_unconfigure(bar);
23440Sstevel@tonic-gate kmem_free(be, sizeof (cyc_backend_t));
23450Sstevel@tonic-gate cpu->cyp_backend = NULL;
23460Sstevel@tonic-gate
23470Sstevel@tonic-gate /*
23480Sstevel@tonic-gate * Free up the producer/consumer buffers at each of the soft levels.
23490Sstevel@tonic-gate */
23500Sstevel@tonic-gate for (i = CY_LOW_LEVEL; i < CY_LOW_LEVEL + CY_SOFT_LEVELS; i++) {
23510Sstevel@tonic-gate cyc_softbuf_t *softbuf = &cpu->cyp_softbuf[i];
23520Sstevel@tonic-gate uchar_t hard = softbuf->cys_hard;
23530Sstevel@tonic-gate cyc_pcbuffer_t *pc = &softbuf->cys_buf[hard];
23540Sstevel@tonic-gate size_t bufsize = sizeof (cyc_index_t) * (pc->cypc_sizemask + 1);
23550Sstevel@tonic-gate
23560Sstevel@tonic-gate /*
23570Sstevel@tonic-gate * Assert that we're not in the middle of a resize operation.
23580Sstevel@tonic-gate */
23590Sstevel@tonic-gate ASSERT(hard == softbuf->cys_soft);
23600Sstevel@tonic-gate ASSERT(hard == 0 || hard == 1);
23610Sstevel@tonic-gate ASSERT(pc->cypc_buf != NULL);
23620Sstevel@tonic-gate ASSERT(softbuf->cys_buf[hard ^ 1].cypc_buf == NULL);
23630Sstevel@tonic-gate
23640Sstevel@tonic-gate kmem_free(pc->cypc_buf, bufsize);
23650Sstevel@tonic-gate pc->cypc_buf = NULL;
23660Sstevel@tonic-gate }
23670Sstevel@tonic-gate
23680Sstevel@tonic-gate /*
23690Sstevel@tonic-gate * Finally, clean up our remaining dynamic structures and NULL out
23700Sstevel@tonic-gate * the cpu_cyclic pointer.
23710Sstevel@tonic-gate */
23720Sstevel@tonic-gate kmem_free(cpu->cyp_cyclics, cpu->cyp_size * sizeof (cyclic_t));
23730Sstevel@tonic-gate kmem_free(cpu->cyp_heap, cpu->cyp_size * sizeof (cyc_index_t));
23740Sstevel@tonic-gate kmem_free(cpu, sizeof (cyc_cpu_t));
23750Sstevel@tonic-gate
23760Sstevel@tonic-gate c->cpu_cyclic = NULL;
23770Sstevel@tonic-gate }
23780Sstevel@tonic-gate
23790Sstevel@tonic-gate static int
cyclic_cpu_setup(cpu_setup_t what,int id)23800Sstevel@tonic-gate cyclic_cpu_setup(cpu_setup_t what, int id)
23810Sstevel@tonic-gate {
23820Sstevel@tonic-gate /*
23830Sstevel@tonic-gate * We are guaranteed that there is still/already an entry in the
23840Sstevel@tonic-gate * cpu array for this CPU.
23850Sstevel@tonic-gate */
23860Sstevel@tonic-gate cpu_t *c = cpu[id];
23870Sstevel@tonic-gate cyc_cpu_t *cyp = c->cpu_cyclic;
23880Sstevel@tonic-gate
23890Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
23900Sstevel@tonic-gate
23910Sstevel@tonic-gate switch (what) {
23920Sstevel@tonic-gate case CPU_CONFIG:
23930Sstevel@tonic-gate ASSERT(cyp == NULL);
23940Sstevel@tonic-gate cyclic_configure(c);
23950Sstevel@tonic-gate break;
23960Sstevel@tonic-gate
23970Sstevel@tonic-gate case CPU_UNCONFIG:
23980Sstevel@tonic-gate ASSERT(cyp != NULL && cyp->cyp_state == CYS_OFFLINE);
23990Sstevel@tonic-gate cyclic_unconfigure(c);
24000Sstevel@tonic-gate break;
24010Sstevel@tonic-gate
24020Sstevel@tonic-gate default:
24030Sstevel@tonic-gate break;
24040Sstevel@tonic-gate }
24050Sstevel@tonic-gate
24060Sstevel@tonic-gate return (0);
24070Sstevel@tonic-gate }
24080Sstevel@tonic-gate
24090Sstevel@tonic-gate static void
cyclic_suspend_xcall(cyc_xcallarg_t * arg)24100Sstevel@tonic-gate cyclic_suspend_xcall(cyc_xcallarg_t *arg)
24110Sstevel@tonic-gate {
24120Sstevel@tonic-gate cyc_cpu_t *cpu = arg->cyx_cpu;
24130Sstevel@tonic-gate cyc_backend_t *be = cpu->cyp_backend;
24140Sstevel@tonic-gate cyc_cookie_t cookie;
24150Sstevel@tonic-gate cyb_arg_t bar = be->cyb_arg;
24160Sstevel@tonic-gate
24170Sstevel@tonic-gate cookie = be->cyb_set_level(bar, CY_HIGH_LEVEL);
24180Sstevel@tonic-gate
24190Sstevel@tonic-gate CYC_TRACE1(cpu, CY_HIGH_LEVEL, "suspend-xcall", cpu->cyp_nelems);
24200Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE || cpu->cyp_state == CYS_OFFLINE);
24210Sstevel@tonic-gate
24220Sstevel@tonic-gate /*
24230Sstevel@tonic-gate * We won't disable this CPU unless it has a non-zero number of
24240Sstevel@tonic-gate * elements (cpu_lock assures that no one else may be attempting
24250Sstevel@tonic-gate * to disable this CPU).
24260Sstevel@tonic-gate */
24270Sstevel@tonic-gate if (cpu->cyp_nelems > 0) {
24280Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE);
24290Sstevel@tonic-gate be->cyb_disable(bar);
24300Sstevel@tonic-gate }
24310Sstevel@tonic-gate
24320Sstevel@tonic-gate if (cpu->cyp_state == CYS_ONLINE)
24330Sstevel@tonic-gate cpu->cyp_state = CYS_SUSPENDED;
24340Sstevel@tonic-gate
24350Sstevel@tonic-gate be->cyb_suspend(bar);
24360Sstevel@tonic-gate be->cyb_restore_level(bar, cookie);
24370Sstevel@tonic-gate }
24380Sstevel@tonic-gate
24390Sstevel@tonic-gate static void
cyclic_resume_xcall(cyc_xcallarg_t * arg)24400Sstevel@tonic-gate cyclic_resume_xcall(cyc_xcallarg_t *arg)
24410Sstevel@tonic-gate {
24420Sstevel@tonic-gate cyc_cpu_t *cpu = arg->cyx_cpu;
24430Sstevel@tonic-gate cyc_backend_t *be = cpu->cyp_backend;
24440Sstevel@tonic-gate cyc_cookie_t cookie;
24450Sstevel@tonic-gate cyb_arg_t bar = be->cyb_arg;
24460Sstevel@tonic-gate cyc_state_t state = cpu->cyp_state;
24470Sstevel@tonic-gate
24480Sstevel@tonic-gate cookie = be->cyb_set_level(bar, CY_HIGH_LEVEL);
24490Sstevel@tonic-gate
24500Sstevel@tonic-gate CYC_TRACE1(cpu, CY_HIGH_LEVEL, "resume-xcall", cpu->cyp_nelems);
24510Sstevel@tonic-gate ASSERT(state == CYS_SUSPENDED || state == CYS_OFFLINE);
24520Sstevel@tonic-gate
24530Sstevel@tonic-gate be->cyb_resume(bar);
24540Sstevel@tonic-gate
24550Sstevel@tonic-gate /*
24560Sstevel@tonic-gate * We won't enable this CPU unless it has a non-zero number of
24570Sstevel@tonic-gate * elements.
24580Sstevel@tonic-gate */
24590Sstevel@tonic-gate if (cpu->cyp_nelems > 0) {
24600Sstevel@tonic-gate cyclic_t *cyclic = &cpu->cyp_cyclics[cpu->cyp_heap[0]];
24610Sstevel@tonic-gate hrtime_t exp = cyclic->cy_expire;
24620Sstevel@tonic-gate
24630Sstevel@tonic-gate CYC_TRACE(cpu, CY_HIGH_LEVEL, "resume-reprog", cyclic, exp);
24640Sstevel@tonic-gate ASSERT(state == CYS_SUSPENDED);
24650Sstevel@tonic-gate be->cyb_enable(bar);
24660Sstevel@tonic-gate be->cyb_reprogram(bar, exp);
24670Sstevel@tonic-gate }
24680Sstevel@tonic-gate
24690Sstevel@tonic-gate if (state == CYS_SUSPENDED)
24700Sstevel@tonic-gate cpu->cyp_state = CYS_ONLINE;
24710Sstevel@tonic-gate
24720Sstevel@tonic-gate CYC_TRACE1(cpu, CY_HIGH_LEVEL, "resume-done", cpu->cyp_nelems);
24730Sstevel@tonic-gate be->cyb_restore_level(bar, cookie);
24740Sstevel@tonic-gate }
24750Sstevel@tonic-gate
24760Sstevel@tonic-gate static void
cyclic_omni_start(cyc_id_t * idp,cyc_cpu_t * cpu)24770Sstevel@tonic-gate cyclic_omni_start(cyc_id_t *idp, cyc_cpu_t *cpu)
24780Sstevel@tonic-gate {
24790Sstevel@tonic-gate cyc_omni_handler_t *omni = &idp->cyi_omni_hdlr;
24800Sstevel@tonic-gate cyc_omni_cpu_t *ocpu = kmem_alloc(sizeof (cyc_omni_cpu_t), KM_SLEEP);
24810Sstevel@tonic-gate cyc_handler_t hdlr;
24820Sstevel@tonic-gate cyc_time_t when;
24830Sstevel@tonic-gate
24840Sstevel@tonic-gate CYC_PTRACE("omni-start", cpu, idp);
24850Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
24860Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE);
24870Sstevel@tonic-gate ASSERT(idp->cyi_cpu == NULL);
24880Sstevel@tonic-gate
24890Sstevel@tonic-gate hdlr.cyh_func = NULL;
24900Sstevel@tonic-gate hdlr.cyh_arg = NULL;
24910Sstevel@tonic-gate hdlr.cyh_level = CY_LEVELS;
24920Sstevel@tonic-gate
24930Sstevel@tonic-gate when.cyt_when = 0;
24940Sstevel@tonic-gate when.cyt_interval = 0;
24950Sstevel@tonic-gate
24960Sstevel@tonic-gate omni->cyo_online(omni->cyo_arg, cpu->cyp_cpu, &hdlr, &when);
24970Sstevel@tonic-gate
24980Sstevel@tonic-gate ASSERT(hdlr.cyh_func != NULL);
24990Sstevel@tonic-gate ASSERT(hdlr.cyh_level < CY_LEVELS);
25000Sstevel@tonic-gate ASSERT(when.cyt_when >= 0 && when.cyt_interval > 0);
25010Sstevel@tonic-gate
25020Sstevel@tonic-gate ocpu->cyo_cpu = cpu;
25030Sstevel@tonic-gate ocpu->cyo_arg = hdlr.cyh_arg;
25040Sstevel@tonic-gate ocpu->cyo_ndx = cyclic_add_here(cpu, &hdlr, &when, 0);
25050Sstevel@tonic-gate ocpu->cyo_next = idp->cyi_omni_list;
25060Sstevel@tonic-gate idp->cyi_omni_list = ocpu;
25070Sstevel@tonic-gate }
25080Sstevel@tonic-gate
25090Sstevel@tonic-gate static void
cyclic_omni_stop(cyc_id_t * idp,cyc_cpu_t * cpu)25100Sstevel@tonic-gate cyclic_omni_stop(cyc_id_t *idp, cyc_cpu_t *cpu)
25110Sstevel@tonic-gate {
25120Sstevel@tonic-gate cyc_omni_handler_t *omni = &idp->cyi_omni_hdlr;
25130Sstevel@tonic-gate cyc_omni_cpu_t *ocpu = idp->cyi_omni_list, *prev = NULL;
2514*8048SMadhavan.Venkataraman@Sun.COM clock_t delay;
2515*8048SMadhavan.Venkataraman@Sun.COM int ret;
25160Sstevel@tonic-gate
25170Sstevel@tonic-gate CYC_PTRACE("omni-stop", cpu, idp);
25180Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
25190Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE);
25200Sstevel@tonic-gate ASSERT(idp->cyi_cpu == NULL);
25210Sstevel@tonic-gate ASSERT(ocpu != NULL);
25220Sstevel@tonic-gate
2523*8048SMadhavan.Venkataraman@Sun.COM /*
2524*8048SMadhavan.Venkataraman@Sun.COM * Prevent a reprogram of this cyclic while we are removing it.
2525*8048SMadhavan.Venkataraman@Sun.COM * Otherwise, cyclic_reprogram_here() will end up sending an X-call
2526*8048SMadhavan.Venkataraman@Sun.COM * to the offlined CPU.
2527*8048SMadhavan.Venkataraman@Sun.COM */
2528*8048SMadhavan.Venkataraman@Sun.COM rw_enter(&idp->cyi_lock, RW_WRITER);
2529*8048SMadhavan.Venkataraman@Sun.COM
25300Sstevel@tonic-gate while (ocpu != NULL && ocpu->cyo_cpu != cpu) {
25310Sstevel@tonic-gate prev = ocpu;
25320Sstevel@tonic-gate ocpu = ocpu->cyo_next;
25330Sstevel@tonic-gate }
25340Sstevel@tonic-gate
25350Sstevel@tonic-gate /*
25360Sstevel@tonic-gate * We _must_ have found an cyc_omni_cpu which corresponds to this
25370Sstevel@tonic-gate * CPU -- the definition of an omnipresent cyclic is that it runs
25380Sstevel@tonic-gate * on all online CPUs.
25390Sstevel@tonic-gate */
25400Sstevel@tonic-gate ASSERT(ocpu != NULL);
25410Sstevel@tonic-gate
25420Sstevel@tonic-gate if (prev == NULL) {
25430Sstevel@tonic-gate idp->cyi_omni_list = ocpu->cyo_next;
25440Sstevel@tonic-gate } else {
25450Sstevel@tonic-gate prev->cyo_next = ocpu->cyo_next;
25460Sstevel@tonic-gate }
25470Sstevel@tonic-gate
2548*8048SMadhavan.Venkataraman@Sun.COM /*
2549*8048SMadhavan.Venkataraman@Sun.COM * Remove the cyclic from the source. We cannot block during this
2550*8048SMadhavan.Venkataraman@Sun.COM * operation because we are holding the cyi_lock which can be held
2551*8048SMadhavan.Venkataraman@Sun.COM * by the cyclic handler via cyclic_reprogram().
2552*8048SMadhavan.Venkataraman@Sun.COM *
2553*8048SMadhavan.Venkataraman@Sun.COM * If we cannot remove the cyclic without waiting, we spin for a time,
2554*8048SMadhavan.Venkataraman@Sun.COM * and reattempt the (non-blocking) removal. If the handler is blocked
2555*8048SMadhavan.Venkataraman@Sun.COM * on the cyi_lock, then we let go of it in the spin loop to give
2556*8048SMadhavan.Venkataraman@Sun.COM * the handler a chance to run. Note that the removal will ultimately
2557*8048SMadhavan.Venkataraman@Sun.COM * succeed -- even if the cyclic handler is blocked on a resource
2558*8048SMadhavan.Venkataraman@Sun.COM * held by a thread which we have preempted, priority inheritance
2559*8048SMadhavan.Venkataraman@Sun.COM * assures that the preempted thread will preempt us and continue
2560*8048SMadhavan.Venkataraman@Sun.COM * to progress.
2561*8048SMadhavan.Venkataraman@Sun.COM */
2562*8048SMadhavan.Venkataraman@Sun.COM for (delay = 1; ; delay <<= 1) {
2563*8048SMadhavan.Venkataraman@Sun.COM /*
2564*8048SMadhavan.Venkataraman@Sun.COM * Before we begin this operation, disable kernel preemption.
2565*8048SMadhavan.Venkataraman@Sun.COM */
2566*8048SMadhavan.Venkataraman@Sun.COM kpreempt_disable();
2567*8048SMadhavan.Venkataraman@Sun.COM ret = cyclic_remove_here(ocpu->cyo_cpu, ocpu->cyo_ndx, NULL,
2568*8048SMadhavan.Venkataraman@Sun.COM CY_NOWAIT);
2569*8048SMadhavan.Venkataraman@Sun.COM /*
2570*8048SMadhavan.Venkataraman@Sun.COM * Enable kernel preemption while spinning.
2571*8048SMadhavan.Venkataraman@Sun.COM */
2572*8048SMadhavan.Venkataraman@Sun.COM kpreempt_enable();
2573*8048SMadhavan.Venkataraman@Sun.COM
2574*8048SMadhavan.Venkataraman@Sun.COM if (ret)
2575*8048SMadhavan.Venkataraman@Sun.COM break;
2576*8048SMadhavan.Venkataraman@Sun.COM
2577*8048SMadhavan.Venkataraman@Sun.COM CYC_PTRACE("remove-omni-retry", idp, ocpu->cyo_cpu);
2578*8048SMadhavan.Venkataraman@Sun.COM
2579*8048SMadhavan.Venkataraman@Sun.COM /*
2580*8048SMadhavan.Venkataraman@Sun.COM * Drop the RW lock to avoid a deadlock with the cyclic
2581*8048SMadhavan.Venkataraman@Sun.COM * handler (because it can potentially call cyclic_reprogram().
2582*8048SMadhavan.Venkataraman@Sun.COM */
2583*8048SMadhavan.Venkataraman@Sun.COM rw_exit(&idp->cyi_lock);
2584*8048SMadhavan.Venkataraman@Sun.COM drv_usecwait(delay);
2585*8048SMadhavan.Venkataraman@Sun.COM rw_enter(&idp->cyi_lock, RW_WRITER);
2586*8048SMadhavan.Venkataraman@Sun.COM }
2587*8048SMadhavan.Venkataraman@Sun.COM
2588*8048SMadhavan.Venkataraman@Sun.COM /*
2589*8048SMadhavan.Venkataraman@Sun.COM * Now that we have successfully removed the cyclic, allow the omni
2590*8048SMadhavan.Venkataraman@Sun.COM * cyclic to be reprogrammed on other CPUs.
2591*8048SMadhavan.Venkataraman@Sun.COM */
2592*8048SMadhavan.Venkataraman@Sun.COM rw_exit(&idp->cyi_lock);
25930Sstevel@tonic-gate
25940Sstevel@tonic-gate /*
25950Sstevel@tonic-gate * The cyclic has been removed from this CPU; time to call the
25960Sstevel@tonic-gate * omnipresent offline handler.
25970Sstevel@tonic-gate */
25980Sstevel@tonic-gate if (omni->cyo_offline != NULL)
25990Sstevel@tonic-gate omni->cyo_offline(omni->cyo_arg, cpu->cyp_cpu, ocpu->cyo_arg);
26000Sstevel@tonic-gate
26010Sstevel@tonic-gate kmem_free(ocpu, sizeof (cyc_omni_cpu_t));
26020Sstevel@tonic-gate }
26030Sstevel@tonic-gate
26040Sstevel@tonic-gate static cyc_id_t *
cyclic_new_id()26050Sstevel@tonic-gate cyclic_new_id()
26060Sstevel@tonic-gate {
26070Sstevel@tonic-gate cyc_id_t *idp;
26080Sstevel@tonic-gate
26090Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
26100Sstevel@tonic-gate
26110Sstevel@tonic-gate idp = kmem_cache_alloc(cyclic_id_cache, KM_SLEEP);
26120Sstevel@tonic-gate
26130Sstevel@tonic-gate /*
26140Sstevel@tonic-gate * The cyi_cpu field of the cyc_id_t structure tracks the CPU
26150Sstevel@tonic-gate * associated with the cyclic. If and only if this field is NULL, the
26160Sstevel@tonic-gate * cyc_id_t is an omnipresent cyclic. Note that cyi_omni_list may be
26170Sstevel@tonic-gate * NULL for an omnipresent cyclic while the cyclic is being created
26180Sstevel@tonic-gate * or destroyed.
26190Sstevel@tonic-gate */
26200Sstevel@tonic-gate idp->cyi_cpu = NULL;
26210Sstevel@tonic-gate idp->cyi_ndx = 0;
2622*8048SMadhavan.Venkataraman@Sun.COM rw_init(&idp->cyi_lock, NULL, RW_DEFAULT, NULL);
26230Sstevel@tonic-gate
26240Sstevel@tonic-gate idp->cyi_next = cyclic_id_head;
26250Sstevel@tonic-gate idp->cyi_prev = NULL;
26260Sstevel@tonic-gate idp->cyi_omni_list = NULL;
26270Sstevel@tonic-gate
26280Sstevel@tonic-gate if (cyclic_id_head != NULL) {
26290Sstevel@tonic-gate ASSERT(cyclic_id_head->cyi_prev == NULL);
26300Sstevel@tonic-gate cyclic_id_head->cyi_prev = idp;
26310Sstevel@tonic-gate }
26320Sstevel@tonic-gate
26330Sstevel@tonic-gate cyclic_id_head = idp;
26340Sstevel@tonic-gate
26350Sstevel@tonic-gate return (idp);
26360Sstevel@tonic-gate }
26370Sstevel@tonic-gate
26380Sstevel@tonic-gate /*
26390Sstevel@tonic-gate * cyclic_id_t cyclic_add(cyc_handler_t *, cyc_time_t *)
26400Sstevel@tonic-gate *
26410Sstevel@tonic-gate * Overview
26420Sstevel@tonic-gate *
26430Sstevel@tonic-gate * cyclic_add() will create an unbound cyclic with the specified handler and
26440Sstevel@tonic-gate * interval. The cyclic will run on a CPU which both has interrupts enabled
26450Sstevel@tonic-gate * and is in the system CPU partition.
26460Sstevel@tonic-gate *
26470Sstevel@tonic-gate * Arguments and notes
26480Sstevel@tonic-gate *
26490Sstevel@tonic-gate * As its first argument, cyclic_add() takes a cyc_handler, which has the
26500Sstevel@tonic-gate * following members:
26510Sstevel@tonic-gate *
26520Sstevel@tonic-gate * cyc_func_t cyh_func <-- Cyclic handler
26530Sstevel@tonic-gate * void *cyh_arg <-- Argument to cyclic handler
26540Sstevel@tonic-gate * cyc_level_t cyh_level <-- Level at which to fire; must be one of
26550Sstevel@tonic-gate * CY_LOW_LEVEL, CY_LOCK_LEVEL or CY_HIGH_LEVEL
26560Sstevel@tonic-gate *
26570Sstevel@tonic-gate * Note that cyh_level is _not_ an ipl or spl; it must be one the
26580Sstevel@tonic-gate * CY_*_LEVELs. This layer of abstraction allows the platform to define
26590Sstevel@tonic-gate * the precise interrupt priority levels, within the following constraints:
26600Sstevel@tonic-gate *
26610Sstevel@tonic-gate * CY_LOCK_LEVEL must map to LOCK_LEVEL
26620Sstevel@tonic-gate * CY_HIGH_LEVEL must map to an ipl greater than LOCK_LEVEL
26630Sstevel@tonic-gate * CY_LOW_LEVEL must map to an ipl below LOCK_LEVEL
26640Sstevel@tonic-gate *
26650Sstevel@tonic-gate * In addition to a cyc_handler, cyclic_add() takes a cyc_time, which
26660Sstevel@tonic-gate * has the following members:
26670Sstevel@tonic-gate *
26680Sstevel@tonic-gate * hrtime_t cyt_when <-- Absolute time, in nanoseconds since boot, at
26690Sstevel@tonic-gate * which to start firing
26700Sstevel@tonic-gate * hrtime_t cyt_interval <-- Length of interval, in nanoseconds
26710Sstevel@tonic-gate *
26720Sstevel@tonic-gate * gethrtime() is the time source for nanoseconds since boot. If cyt_when
26730Sstevel@tonic-gate * is set to 0, the cyclic will start to fire when cyt_interval next
26740Sstevel@tonic-gate * divides the number of nanoseconds since boot.
26750Sstevel@tonic-gate *
26760Sstevel@tonic-gate * The cyt_interval field _must_ be filled in by the caller; one-shots are
26770Sstevel@tonic-gate * _not_ explicitly supported by the cyclic subsystem (cyclic_add() will
26780Sstevel@tonic-gate * assert that cyt_interval is non-zero). The maximum value for either
26790Sstevel@tonic-gate * field is INT64_MAX; the caller is responsible for assuring that
26800Sstevel@tonic-gate * cyt_when + cyt_interval <= INT64_MAX. Neither field may be negative.
26810Sstevel@tonic-gate *
26820Sstevel@tonic-gate * For an arbitrary time t in the future, the cyclic handler is guaranteed
26830Sstevel@tonic-gate * to have been called (t - cyt_when) / cyt_interval times. This will
26840Sstevel@tonic-gate * be true even if interrupts have been disabled for periods greater than
26850Sstevel@tonic-gate * cyt_interval nanoseconds. In order to compensate for such periods,
26860Sstevel@tonic-gate * the cyclic handler may be called a finite number of times with an
26870Sstevel@tonic-gate * arbitrarily small interval.
26880Sstevel@tonic-gate *
26890Sstevel@tonic-gate * The cyclic subsystem will not enforce any lower bound on the interval;
26900Sstevel@tonic-gate * if the interval is less than the time required to process an interrupt,
26910Sstevel@tonic-gate * the CPU will wedge. It's the responsibility of the caller to assure that
26920Sstevel@tonic-gate * either the value of the interval is sane, or that its caller has
26930Sstevel@tonic-gate * sufficient privilege to deny service (i.e. its caller is root).
26940Sstevel@tonic-gate *
26950Sstevel@tonic-gate * The cyclic handler is guaranteed to be single threaded, even while the
26960Sstevel@tonic-gate * cyclic is being juggled between CPUs (see cyclic_juggle(), below).
26970Sstevel@tonic-gate * That is, a given cyclic handler will never be executed simultaneously
26980Sstevel@tonic-gate * on different CPUs.
26990Sstevel@tonic-gate *
27000Sstevel@tonic-gate * Return value
27010Sstevel@tonic-gate *
27020Sstevel@tonic-gate * cyclic_add() returns a cyclic_id_t, which is guaranteed to be a value
27030Sstevel@tonic-gate * other than CYCLIC_NONE. cyclic_add() cannot fail.
27040Sstevel@tonic-gate *
27050Sstevel@tonic-gate * Caller's context
27060Sstevel@tonic-gate *
27070Sstevel@tonic-gate * cpu_lock must be held by the caller, and the caller must not be in
27080Sstevel@tonic-gate * interrupt context. cyclic_add() will perform a KM_SLEEP kernel
27090Sstevel@tonic-gate * memory allocation, so the usual rules (e.g. p_lock cannot be held)
27100Sstevel@tonic-gate * apply. A cyclic may be added even in the presence of CPUs that have
27110Sstevel@tonic-gate * not been configured with respect to the cyclic subsystem, but only
27120Sstevel@tonic-gate * configured CPUs will be eligible to run the new cyclic.
27130Sstevel@tonic-gate *
27140Sstevel@tonic-gate * Cyclic handler's context
27150Sstevel@tonic-gate *
27160Sstevel@tonic-gate * Cyclic handlers will be executed in the interrupt context corresponding
27170Sstevel@tonic-gate * to the specified level (i.e. either high, lock or low level). The
27180Sstevel@tonic-gate * usual context rules apply.
27190Sstevel@tonic-gate *
27200Sstevel@tonic-gate * A cyclic handler may not grab ANY locks held by the caller of any of
27210Sstevel@tonic-gate * cyclic_add(), cyclic_remove() or cyclic_bind(); the implementation of
27220Sstevel@tonic-gate * these functions may require blocking on cyclic handler completion.
27230Sstevel@tonic-gate * Moreover, cyclic handlers may not make any call back into the cyclic
27240Sstevel@tonic-gate * subsystem.
27250Sstevel@tonic-gate */
27260Sstevel@tonic-gate cyclic_id_t
cyclic_add(cyc_handler_t * hdlr,cyc_time_t * when)27270Sstevel@tonic-gate cyclic_add(cyc_handler_t *hdlr, cyc_time_t *when)
27280Sstevel@tonic-gate {
27290Sstevel@tonic-gate cyc_id_t *idp = cyclic_new_id();
27300Sstevel@tonic-gate
27310Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
27320Sstevel@tonic-gate ASSERT(when->cyt_when >= 0 && when->cyt_interval > 0);
27330Sstevel@tonic-gate
27340Sstevel@tonic-gate idp->cyi_cpu = cyclic_pick_cpu(NULL, NULL, NULL, 0);
27350Sstevel@tonic-gate idp->cyi_ndx = cyclic_add_here(idp->cyi_cpu, hdlr, when, 0);
27360Sstevel@tonic-gate
27370Sstevel@tonic-gate return ((uintptr_t)idp);
27380Sstevel@tonic-gate }
27390Sstevel@tonic-gate
27400Sstevel@tonic-gate /*
27410Sstevel@tonic-gate * cyclic_id_t cyclic_add_omni(cyc_omni_handler_t *)
27420Sstevel@tonic-gate *
27430Sstevel@tonic-gate * Overview
27440Sstevel@tonic-gate *
27450Sstevel@tonic-gate * cyclic_add_omni() will create an omnipresent cyclic with the specified
27460Sstevel@tonic-gate * online and offline handlers. Omnipresent cyclics run on all online
27470Sstevel@tonic-gate * CPUs, including CPUs which have unbound interrupts disabled.
27480Sstevel@tonic-gate *
27490Sstevel@tonic-gate * Arguments
27500Sstevel@tonic-gate *
27510Sstevel@tonic-gate * As its only argument, cyclic_add_omni() takes a cyc_omni_handler, which
27520Sstevel@tonic-gate * has the following members:
27530Sstevel@tonic-gate *
27540Sstevel@tonic-gate * void (*cyo_online)() <-- Online handler
27550Sstevel@tonic-gate * void (*cyo_offline)() <-- Offline handler
27560Sstevel@tonic-gate * void *cyo_arg <-- Argument to be passed to on/offline handlers
27570Sstevel@tonic-gate *
27580Sstevel@tonic-gate * Online handler
27590Sstevel@tonic-gate *
27600Sstevel@tonic-gate * The cyo_online member is a pointer to a function which has the following
27610Sstevel@tonic-gate * four arguments:
27620Sstevel@tonic-gate *
27630Sstevel@tonic-gate * void * <-- Argument (cyo_arg)
27640Sstevel@tonic-gate * cpu_t * <-- Pointer to CPU about to be onlined
27650Sstevel@tonic-gate * cyc_handler_t * <-- Pointer to cyc_handler_t; must be filled in
27660Sstevel@tonic-gate * by omni online handler
27670Sstevel@tonic-gate * cyc_time_t * <-- Pointer to cyc_time_t; must be filled in by
27680Sstevel@tonic-gate * omni online handler
27690Sstevel@tonic-gate *
27700Sstevel@tonic-gate * The omni cyclic online handler is always called _before_ the omni
27710Sstevel@tonic-gate * cyclic begins to fire on the specified CPU. As the above argument
27720Sstevel@tonic-gate * description implies, the online handler must fill in the two structures
27730Sstevel@tonic-gate * passed to it: the cyc_handler_t and the cyc_time_t. These are the
27740Sstevel@tonic-gate * same two structures passed to cyclic_add(), outlined above. This
27750Sstevel@tonic-gate * allows the omni cyclic to have maximum flexibility; different CPUs may
27760Sstevel@tonic-gate * optionally
27770Sstevel@tonic-gate *
27780Sstevel@tonic-gate * (a) have different intervals
27790Sstevel@tonic-gate * (b) be explicitly in or out of phase with one another
27800Sstevel@tonic-gate * (c) have different handlers
27810Sstevel@tonic-gate * (d) have different handler arguments
27820Sstevel@tonic-gate * (e) fire at different levels
27830Sstevel@tonic-gate *
27840Sstevel@tonic-gate * Of these, (e) seems somewhat dubious, but is nonetheless allowed.
27850Sstevel@tonic-gate *
27860Sstevel@tonic-gate * The omni online handler is called in the same context as cyclic_add(),
27870Sstevel@tonic-gate * and has the same liberties: omni online handlers may perform KM_SLEEP
27880Sstevel@tonic-gate * kernel memory allocations, and may grab locks which are also acquired
27890Sstevel@tonic-gate * by cyclic handlers. However, omni cyclic online handlers may _not_
27900Sstevel@tonic-gate * call back into the cyclic subsystem, and should be generally careful
27910Sstevel@tonic-gate * about calling into arbitrary kernel subsystems.
27920Sstevel@tonic-gate *
27930Sstevel@tonic-gate * Offline handler
27940Sstevel@tonic-gate *
27950Sstevel@tonic-gate * The cyo_offline member is a pointer to a function which has the following
27960Sstevel@tonic-gate * three arguments:
27970Sstevel@tonic-gate *
27980Sstevel@tonic-gate * void * <-- Argument (cyo_arg)
27990Sstevel@tonic-gate * cpu_t * <-- Pointer to CPU about to be offlined
28000Sstevel@tonic-gate * void * <-- CPU's cyclic argument (that is, value
28010Sstevel@tonic-gate * to which cyh_arg member of the cyc_handler_t
28020Sstevel@tonic-gate * was set in the omni online handler)
28030Sstevel@tonic-gate *
28040Sstevel@tonic-gate * The omni cyclic offline handler is always called _after_ the omni
28050Sstevel@tonic-gate * cyclic has ceased firing on the specified CPU. Its purpose is to
28060Sstevel@tonic-gate * allow cleanup of any resources dynamically allocated in the omni cyclic
28070Sstevel@tonic-gate * online handler. The context of the offline handler is identical to
28080Sstevel@tonic-gate * that of the online handler; the same constraints and liberties apply.
28090Sstevel@tonic-gate *
28100Sstevel@tonic-gate * The offline handler is optional; it may be NULL.
28110Sstevel@tonic-gate *
28120Sstevel@tonic-gate * Return value
28130Sstevel@tonic-gate *
28140Sstevel@tonic-gate * cyclic_add_omni() returns a cyclic_id_t, which is guaranteed to be a
28150Sstevel@tonic-gate * value other than CYCLIC_NONE. cyclic_add_omni() cannot fail.
28160Sstevel@tonic-gate *
28170Sstevel@tonic-gate * Caller's context
28180Sstevel@tonic-gate *
28190Sstevel@tonic-gate * The caller's context is identical to that of cyclic_add(), specified
28200Sstevel@tonic-gate * above.
28210Sstevel@tonic-gate */
28220Sstevel@tonic-gate cyclic_id_t
cyclic_add_omni(cyc_omni_handler_t * omni)28230Sstevel@tonic-gate cyclic_add_omni(cyc_omni_handler_t *omni)
28240Sstevel@tonic-gate {
28250Sstevel@tonic-gate cyc_id_t *idp = cyclic_new_id();
28260Sstevel@tonic-gate cyc_cpu_t *cpu;
28270Sstevel@tonic-gate cpu_t *c;
28280Sstevel@tonic-gate
28290Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
28300Sstevel@tonic-gate ASSERT(omni != NULL && omni->cyo_online != NULL);
28310Sstevel@tonic-gate
28320Sstevel@tonic-gate idp->cyi_omni_hdlr = *omni;
28330Sstevel@tonic-gate
28340Sstevel@tonic-gate c = cpu_list;
28350Sstevel@tonic-gate do {
28360Sstevel@tonic-gate if ((cpu = c->cpu_cyclic) == NULL)
28370Sstevel@tonic-gate continue;
28380Sstevel@tonic-gate
28390Sstevel@tonic-gate if (cpu->cyp_state != CYS_ONLINE) {
28400Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_OFFLINE);
28410Sstevel@tonic-gate continue;
28420Sstevel@tonic-gate }
28430Sstevel@tonic-gate
28440Sstevel@tonic-gate cyclic_omni_start(idp, cpu);
28450Sstevel@tonic-gate } while ((c = c->cpu_next) != cpu_list);
28460Sstevel@tonic-gate
28470Sstevel@tonic-gate /*
28480Sstevel@tonic-gate * We must have found at least one online CPU on which to run
28490Sstevel@tonic-gate * this cyclic.
28500Sstevel@tonic-gate */
28510Sstevel@tonic-gate ASSERT(idp->cyi_omni_list != NULL);
28520Sstevel@tonic-gate ASSERT(idp->cyi_cpu == NULL);
28530Sstevel@tonic-gate
28540Sstevel@tonic-gate return ((uintptr_t)idp);
28550Sstevel@tonic-gate }
28560Sstevel@tonic-gate
28570Sstevel@tonic-gate /*
28580Sstevel@tonic-gate * void cyclic_remove(cyclic_id_t)
28590Sstevel@tonic-gate *
28600Sstevel@tonic-gate * Overview
28610Sstevel@tonic-gate *
28620Sstevel@tonic-gate * cyclic_remove() will remove the specified cyclic from the system.
28630Sstevel@tonic-gate *
28640Sstevel@tonic-gate * Arguments and notes
28650Sstevel@tonic-gate *
28660Sstevel@tonic-gate * The only argument is a cyclic_id returned from either cyclic_add() or
28670Sstevel@tonic-gate * cyclic_add_omni().
28680Sstevel@tonic-gate *
28690Sstevel@tonic-gate * By the time cyclic_remove() returns, the caller is guaranteed that the
28700Sstevel@tonic-gate * removed cyclic handler has completed execution (this is the same
28710Sstevel@tonic-gate * semantic that untimeout() provides). As a result, cyclic_remove() may
28720Sstevel@tonic-gate * need to block, waiting for the removed cyclic to complete execution.
28730Sstevel@tonic-gate * This leads to an important constraint on the caller: no lock may be
28740Sstevel@tonic-gate * held across cyclic_remove() that also may be acquired by a cyclic
28750Sstevel@tonic-gate * handler.
28760Sstevel@tonic-gate *
28770Sstevel@tonic-gate * Return value
28780Sstevel@tonic-gate *
28790Sstevel@tonic-gate * None; cyclic_remove() always succeeds.
28800Sstevel@tonic-gate *
28810Sstevel@tonic-gate * Caller's context
28820Sstevel@tonic-gate *
28830Sstevel@tonic-gate * cpu_lock must be held by the caller, and the caller must not be in
28840Sstevel@tonic-gate * interrupt context. The caller may not hold any locks which are also
28850Sstevel@tonic-gate * grabbed by any cyclic handler. See "Arguments and notes", above.
28860Sstevel@tonic-gate */
28870Sstevel@tonic-gate void
cyclic_remove(cyclic_id_t id)28880Sstevel@tonic-gate cyclic_remove(cyclic_id_t id)
28890Sstevel@tonic-gate {
28900Sstevel@tonic-gate cyc_id_t *idp = (cyc_id_t *)id;
28910Sstevel@tonic-gate cyc_id_t *prev = idp->cyi_prev, *next = idp->cyi_next;
28920Sstevel@tonic-gate cyc_cpu_t *cpu = idp->cyi_cpu;
28930Sstevel@tonic-gate
28940Sstevel@tonic-gate CYC_PTRACE("remove", idp, idp->cyi_cpu);
28950Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
28960Sstevel@tonic-gate
28970Sstevel@tonic-gate if (cpu != NULL) {
28980Sstevel@tonic-gate (void) cyclic_remove_here(cpu, idp->cyi_ndx, NULL, CY_WAIT);
28990Sstevel@tonic-gate } else {
29000Sstevel@tonic-gate ASSERT(idp->cyi_omni_list != NULL);
29010Sstevel@tonic-gate while (idp->cyi_omni_list != NULL)
29020Sstevel@tonic-gate cyclic_omni_stop(idp, idp->cyi_omni_list->cyo_cpu);
29030Sstevel@tonic-gate }
29040Sstevel@tonic-gate
29050Sstevel@tonic-gate if (prev != NULL) {
29060Sstevel@tonic-gate ASSERT(cyclic_id_head != idp);
29070Sstevel@tonic-gate prev->cyi_next = next;
29080Sstevel@tonic-gate } else {
29090Sstevel@tonic-gate ASSERT(cyclic_id_head == idp);
29100Sstevel@tonic-gate cyclic_id_head = next;
29110Sstevel@tonic-gate }
29120Sstevel@tonic-gate
29130Sstevel@tonic-gate if (next != NULL)
29140Sstevel@tonic-gate next->cyi_prev = prev;
29150Sstevel@tonic-gate
29160Sstevel@tonic-gate kmem_cache_free(cyclic_id_cache, idp);
29170Sstevel@tonic-gate }
29180Sstevel@tonic-gate
29190Sstevel@tonic-gate /*
29200Sstevel@tonic-gate * void cyclic_bind(cyclic_id_t, cpu_t *, cpupart_t *)
29210Sstevel@tonic-gate *
29220Sstevel@tonic-gate * Overview
29230Sstevel@tonic-gate *
29240Sstevel@tonic-gate * cyclic_bind() atomically changes the CPU and CPU partition bindings
29250Sstevel@tonic-gate * of a cyclic.
29260Sstevel@tonic-gate *
29270Sstevel@tonic-gate * Arguments and notes
29280Sstevel@tonic-gate *
29290Sstevel@tonic-gate * The first argument is a cyclic_id retuned from cyclic_add().
29300Sstevel@tonic-gate * cyclic_bind() may _not_ be called on a cyclic_id returned from
29310Sstevel@tonic-gate * cyclic_add_omni().
29320Sstevel@tonic-gate *
29330Sstevel@tonic-gate * The second argument specifies the CPU to which to bind the specified
29340Sstevel@tonic-gate * cyclic. If the specified cyclic is bound to a CPU other than the one
29350Sstevel@tonic-gate * specified, it will be unbound from its bound CPU. Unbinding the cyclic
29360Sstevel@tonic-gate * from its CPU may cause it to be juggled to another CPU. If the specified
29370Sstevel@tonic-gate * CPU is non-NULL, the cyclic will be subsequently rebound to the specified
29380Sstevel@tonic-gate * CPU.
29390Sstevel@tonic-gate *
29400Sstevel@tonic-gate * If a CPU with bound cyclics is transitioned into the P_NOINTR state,
29410Sstevel@tonic-gate * only cyclics not bound to the CPU can be juggled away; CPU-bound cyclics
29420Sstevel@tonic-gate * will continue to fire on the P_NOINTR CPU. A CPU with bound cyclics
29430Sstevel@tonic-gate * cannot be offlined (attempts to offline the CPU will return EBUSY).
29440Sstevel@tonic-gate * Likewise, cyclics may not be bound to an offline CPU; if the caller
29450Sstevel@tonic-gate * attempts to bind a cyclic to an offline CPU, the cyclic subsystem will
29460Sstevel@tonic-gate * panic.
29470Sstevel@tonic-gate *
29480Sstevel@tonic-gate * The third argument specifies the CPU partition to which to bind the
29490Sstevel@tonic-gate * specified cyclic. If the specified cyclic is bound to a CPU partition
29500Sstevel@tonic-gate * other than the one specified, it will be unbound from its bound
29510Sstevel@tonic-gate * partition. Unbinding the cyclic from its CPU partition may cause it
29520Sstevel@tonic-gate * to be juggled to another CPU. If the specified CPU partition is
29530Sstevel@tonic-gate * non-NULL, the cyclic will be subsequently rebound to the specified CPU
29540Sstevel@tonic-gate * partition.
29550Sstevel@tonic-gate *
29560Sstevel@tonic-gate * It is the caller's responsibility to assure that the specified CPU
29570Sstevel@tonic-gate * partition contains a CPU. If it does not, the cyclic subsystem will
29580Sstevel@tonic-gate * panic. A CPU partition with bound cyclics cannot be destroyed (attempts
29590Sstevel@tonic-gate * to destroy the partition will return EBUSY). If a CPU with
29600Sstevel@tonic-gate * partition-bound cyclics is transitioned into the P_NOINTR state, cyclics
29610Sstevel@tonic-gate * bound to the CPU's partition (but not bound to the CPU) will be juggled
29620Sstevel@tonic-gate * away only if there exists another CPU in the partition in the P_ONLINE
29630Sstevel@tonic-gate * state.
29640Sstevel@tonic-gate *
29650Sstevel@tonic-gate * It is the caller's responsibility to assure that the specified CPU and
29660Sstevel@tonic-gate * CPU partition are self-consistent. If both parameters are non-NULL,
29670Sstevel@tonic-gate * and the specified CPU partition does not contain the specified CPU, the
29680Sstevel@tonic-gate * cyclic subsystem will panic.
29690Sstevel@tonic-gate *
29700Sstevel@tonic-gate * It is the caller's responsibility to assure that the specified CPU has
29710Sstevel@tonic-gate * been configured with respect to the cyclic subsystem. Generally, this
29720Sstevel@tonic-gate * is always true for valid, on-line CPUs. The only periods of time during
29730Sstevel@tonic-gate * which this may not be true are during MP boot (i.e. after cyclic_init()
29740Sstevel@tonic-gate * is called but before cyclic_mp_init() is called) or during dynamic
29750Sstevel@tonic-gate * reconfiguration; cyclic_bind() should only be called with great care
29760Sstevel@tonic-gate * from these contexts.
29770Sstevel@tonic-gate *
29780Sstevel@tonic-gate * Return value
29790Sstevel@tonic-gate *
29800Sstevel@tonic-gate * None; cyclic_bind() always succeeds.
29810Sstevel@tonic-gate *
29820Sstevel@tonic-gate * Caller's context
29830Sstevel@tonic-gate *
29840Sstevel@tonic-gate * cpu_lock must be held by the caller, and the caller must not be in
29850Sstevel@tonic-gate * interrupt context. The caller may not hold any locks which are also
29860Sstevel@tonic-gate * grabbed by any cyclic handler.
29870Sstevel@tonic-gate */
29880Sstevel@tonic-gate void
cyclic_bind(cyclic_id_t id,cpu_t * d,cpupart_t * part)29890Sstevel@tonic-gate cyclic_bind(cyclic_id_t id, cpu_t *d, cpupart_t *part)
29900Sstevel@tonic-gate {
29910Sstevel@tonic-gate cyc_id_t *idp = (cyc_id_t *)id;
29920Sstevel@tonic-gate cyc_cpu_t *cpu = idp->cyi_cpu;
29930Sstevel@tonic-gate cpu_t *c;
29940Sstevel@tonic-gate uint16_t flags;
29950Sstevel@tonic-gate
29960Sstevel@tonic-gate CYC_PTRACE("bind", d, part);
29970Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
29980Sstevel@tonic-gate ASSERT(part == NULL || d == NULL || d->cpu_part == part);
29990Sstevel@tonic-gate
30000Sstevel@tonic-gate if (cpu == NULL) {
30010Sstevel@tonic-gate ASSERT(idp->cyi_omni_list != NULL);
30020Sstevel@tonic-gate panic("attempt to change binding of omnipresent cyclic");
30030Sstevel@tonic-gate }
30040Sstevel@tonic-gate
30050Sstevel@tonic-gate c = cpu->cyp_cpu;
30060Sstevel@tonic-gate flags = cpu->cyp_cyclics[idp->cyi_ndx].cy_flags;
30070Sstevel@tonic-gate
30080Sstevel@tonic-gate if (c != d && (flags & CYF_CPU_BOUND))
30090Sstevel@tonic-gate cyclic_unbind_cpu(id);
30100Sstevel@tonic-gate
30110Sstevel@tonic-gate /*
30120Sstevel@tonic-gate * Reload our cpu (we may have migrated). We don't have to reload
30130Sstevel@tonic-gate * the flags field here; if we were CYF_PART_BOUND on entry, we are
30140Sstevel@tonic-gate * CYF_PART_BOUND now.
30150Sstevel@tonic-gate */
30160Sstevel@tonic-gate cpu = idp->cyi_cpu;
30170Sstevel@tonic-gate c = cpu->cyp_cpu;
30180Sstevel@tonic-gate
30190Sstevel@tonic-gate if (part != c->cpu_part && (flags & CYF_PART_BOUND))
30200Sstevel@tonic-gate cyclic_unbind_cpupart(id);
30210Sstevel@tonic-gate
30220Sstevel@tonic-gate /*
30230Sstevel@tonic-gate * Now reload the flags field, asserting that if we are CPU bound,
30240Sstevel@tonic-gate * the CPU was specified (and likewise, if we are partition bound,
30250Sstevel@tonic-gate * the partition was specified).
30260Sstevel@tonic-gate */
30270Sstevel@tonic-gate cpu = idp->cyi_cpu;
30280Sstevel@tonic-gate c = cpu->cyp_cpu;
30290Sstevel@tonic-gate flags = cpu->cyp_cyclics[idp->cyi_ndx].cy_flags;
30300Sstevel@tonic-gate ASSERT(!(flags & CYF_CPU_BOUND) || c == d);
30310Sstevel@tonic-gate ASSERT(!(flags & CYF_PART_BOUND) || c->cpu_part == part);
30320Sstevel@tonic-gate
30330Sstevel@tonic-gate if (!(flags & CYF_CPU_BOUND) && d != NULL)
30340Sstevel@tonic-gate cyclic_bind_cpu(id, d);
30350Sstevel@tonic-gate
30360Sstevel@tonic-gate if (!(flags & CYF_PART_BOUND) && part != NULL)
30370Sstevel@tonic-gate cyclic_bind_cpupart(id, part);
30380Sstevel@tonic-gate }
30390Sstevel@tonic-gate
3040*8048SMadhavan.Venkataraman@Sun.COM int
cyclic_reprogram(cyclic_id_t id,hrtime_t expiration)3041*8048SMadhavan.Venkataraman@Sun.COM cyclic_reprogram(cyclic_id_t id, hrtime_t expiration)
3042*8048SMadhavan.Venkataraman@Sun.COM {
3043*8048SMadhavan.Venkataraman@Sun.COM cyc_id_t *idp = (cyc_id_t *)id;
3044*8048SMadhavan.Venkataraman@Sun.COM cyc_cpu_t *cpu;
3045*8048SMadhavan.Venkataraman@Sun.COM cyc_omni_cpu_t *ocpu;
3046*8048SMadhavan.Venkataraman@Sun.COM cyc_index_t ndx;
3047*8048SMadhavan.Venkataraman@Sun.COM
3048*8048SMadhavan.Venkataraman@Sun.COM ASSERT(expiration > 0);
3049*8048SMadhavan.Venkataraman@Sun.COM
3050*8048SMadhavan.Venkataraman@Sun.COM CYC_PTRACE("reprog", idp, idp->cyi_cpu);
3051*8048SMadhavan.Venkataraman@Sun.COM
3052*8048SMadhavan.Venkataraman@Sun.COM kpreempt_disable();
3053*8048SMadhavan.Venkataraman@Sun.COM
3054*8048SMadhavan.Venkataraman@Sun.COM /*
3055*8048SMadhavan.Venkataraman@Sun.COM * Prevent the cyclic from moving or disappearing while we reprogram.
3056*8048SMadhavan.Venkataraman@Sun.COM */
3057*8048SMadhavan.Venkataraman@Sun.COM rw_enter(&idp->cyi_lock, RW_READER);
3058*8048SMadhavan.Venkataraman@Sun.COM
3059*8048SMadhavan.Venkataraman@Sun.COM if (idp->cyi_cpu == NULL) {
3060*8048SMadhavan.Venkataraman@Sun.COM ASSERT(curthread->t_preempt > 0);
3061*8048SMadhavan.Venkataraman@Sun.COM cpu = CPU->cpu_cyclic;
3062*8048SMadhavan.Venkataraman@Sun.COM
3063*8048SMadhavan.Venkataraman@Sun.COM /*
3064*8048SMadhavan.Venkataraman@Sun.COM * For an omni cyclic, we reprogram the cyclic corresponding
3065*8048SMadhavan.Venkataraman@Sun.COM * to the current CPU. Look for it in the list.
3066*8048SMadhavan.Venkataraman@Sun.COM */
3067*8048SMadhavan.Venkataraman@Sun.COM ocpu = idp->cyi_omni_list;
3068*8048SMadhavan.Venkataraman@Sun.COM while (ocpu != NULL) {
3069*8048SMadhavan.Venkataraman@Sun.COM if (ocpu->cyo_cpu == cpu)
3070*8048SMadhavan.Venkataraman@Sun.COM break;
3071*8048SMadhavan.Venkataraman@Sun.COM ocpu = ocpu->cyo_next;
3072*8048SMadhavan.Venkataraman@Sun.COM }
3073*8048SMadhavan.Venkataraman@Sun.COM
3074*8048SMadhavan.Venkataraman@Sun.COM if (ocpu == NULL) {
3075*8048SMadhavan.Venkataraman@Sun.COM /*
3076*8048SMadhavan.Venkataraman@Sun.COM * Didn't find it. This means that CPU offline
3077*8048SMadhavan.Venkataraman@Sun.COM * must have removed it racing with us. So,
3078*8048SMadhavan.Venkataraman@Sun.COM * nothing to do.
3079*8048SMadhavan.Venkataraman@Sun.COM */
3080*8048SMadhavan.Venkataraman@Sun.COM rw_exit(&idp->cyi_lock);
3081*8048SMadhavan.Venkataraman@Sun.COM
3082*8048SMadhavan.Venkataraman@Sun.COM kpreempt_enable();
3083*8048SMadhavan.Venkataraman@Sun.COM
3084*8048SMadhavan.Venkataraman@Sun.COM return (0);
3085*8048SMadhavan.Venkataraman@Sun.COM }
3086*8048SMadhavan.Venkataraman@Sun.COM ndx = ocpu->cyo_ndx;
3087*8048SMadhavan.Venkataraman@Sun.COM } else {
3088*8048SMadhavan.Venkataraman@Sun.COM cpu = idp->cyi_cpu;
3089*8048SMadhavan.Venkataraman@Sun.COM ndx = idp->cyi_ndx;
3090*8048SMadhavan.Venkataraman@Sun.COM }
3091*8048SMadhavan.Venkataraman@Sun.COM
3092*8048SMadhavan.Venkataraman@Sun.COM if (cpu->cyp_cpu == CPU)
3093*8048SMadhavan.Venkataraman@Sun.COM cyclic_reprogram_cyclic(cpu, ndx, expiration);
3094*8048SMadhavan.Venkataraman@Sun.COM else
3095*8048SMadhavan.Venkataraman@Sun.COM cyclic_reprogram_here(cpu, ndx, expiration);
3096*8048SMadhavan.Venkataraman@Sun.COM
3097*8048SMadhavan.Venkataraman@Sun.COM /*
3098*8048SMadhavan.Venkataraman@Sun.COM * Allow the cyclic to be moved or removed.
3099*8048SMadhavan.Venkataraman@Sun.COM */
3100*8048SMadhavan.Venkataraman@Sun.COM rw_exit(&idp->cyi_lock);
3101*8048SMadhavan.Venkataraman@Sun.COM
3102*8048SMadhavan.Venkataraman@Sun.COM kpreempt_enable();
3103*8048SMadhavan.Venkataraman@Sun.COM
3104*8048SMadhavan.Venkataraman@Sun.COM return (1);
3105*8048SMadhavan.Venkataraman@Sun.COM }
3106*8048SMadhavan.Venkataraman@Sun.COM
31070Sstevel@tonic-gate hrtime_t
cyclic_getres()31080Sstevel@tonic-gate cyclic_getres()
31090Sstevel@tonic-gate {
31100Sstevel@tonic-gate return (cyclic_resolution);
31110Sstevel@tonic-gate }
31120Sstevel@tonic-gate
31130Sstevel@tonic-gate void
cyclic_init(cyc_backend_t * be,hrtime_t resolution)31140Sstevel@tonic-gate cyclic_init(cyc_backend_t *be, hrtime_t resolution)
31150Sstevel@tonic-gate {
31160Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
31170Sstevel@tonic-gate
31180Sstevel@tonic-gate CYC_PTRACE("init", be, resolution);
31190Sstevel@tonic-gate cyclic_resolution = resolution;
31200Sstevel@tonic-gate
31210Sstevel@tonic-gate /*
31220Sstevel@tonic-gate * Copy the passed cyc_backend into the backend template. This must
31230Sstevel@tonic-gate * be done before the CPU can be configured.
31240Sstevel@tonic-gate */
31250Sstevel@tonic-gate bcopy(be, &cyclic_backend, sizeof (cyc_backend_t));
31260Sstevel@tonic-gate
31270Sstevel@tonic-gate /*
31280Sstevel@tonic-gate * It's safe to look at the "CPU" pointer without disabling kernel
31290Sstevel@tonic-gate * preemption; cyclic_init() is called only during startup by the
31300Sstevel@tonic-gate * cyclic backend.
31310Sstevel@tonic-gate */
31320Sstevel@tonic-gate cyclic_configure(CPU);
31330Sstevel@tonic-gate cyclic_online(CPU);
31340Sstevel@tonic-gate }
31350Sstevel@tonic-gate
31360Sstevel@tonic-gate /*
31370Sstevel@tonic-gate * It is assumed that cyclic_mp_init() is called some time after cyclic
31380Sstevel@tonic-gate * init (and therefore, after cpu0 has been initialized). We grab cpu_lock,
31390Sstevel@tonic-gate * find the already initialized CPU, and initialize every other CPU with the
31400Sstevel@tonic-gate * same backend. Finally, we register a cpu_setup function.
31410Sstevel@tonic-gate */
31420Sstevel@tonic-gate void
cyclic_mp_init()31430Sstevel@tonic-gate cyclic_mp_init()
31440Sstevel@tonic-gate {
31450Sstevel@tonic-gate cpu_t *c;
31460Sstevel@tonic-gate
31470Sstevel@tonic-gate mutex_enter(&cpu_lock);
31480Sstevel@tonic-gate
31490Sstevel@tonic-gate c = cpu_list;
31500Sstevel@tonic-gate do {
31510Sstevel@tonic-gate if (c->cpu_cyclic == NULL) {
31520Sstevel@tonic-gate cyclic_configure(c);
31530Sstevel@tonic-gate cyclic_online(c);
31540Sstevel@tonic-gate }
31550Sstevel@tonic-gate } while ((c = c->cpu_next) != cpu_list);
31560Sstevel@tonic-gate
31570Sstevel@tonic-gate register_cpu_setup_func((cpu_setup_func_t *)cyclic_cpu_setup, NULL);
31580Sstevel@tonic-gate mutex_exit(&cpu_lock);
31590Sstevel@tonic-gate }
31600Sstevel@tonic-gate
31610Sstevel@tonic-gate /*
31620Sstevel@tonic-gate * int cyclic_juggle(cpu_t *)
31630Sstevel@tonic-gate *
31640Sstevel@tonic-gate * Overview
31650Sstevel@tonic-gate *
31660Sstevel@tonic-gate * cyclic_juggle() juggles as many cyclics as possible away from the
31670Sstevel@tonic-gate * specified CPU; all remaining cyclics on the CPU will either be CPU-
31680Sstevel@tonic-gate * or partition-bound.
31690Sstevel@tonic-gate *
31700Sstevel@tonic-gate * Arguments and notes
31710Sstevel@tonic-gate *
31720Sstevel@tonic-gate * The only argument to cyclic_juggle() is the CPU from which cyclics
31730Sstevel@tonic-gate * should be juggled. CPU-bound cyclics are never juggled; partition-bound
31740Sstevel@tonic-gate * cyclics are only juggled if the specified CPU is in the P_NOINTR state
31750Sstevel@tonic-gate * and there exists a P_ONLINE CPU in the partition. The cyclic subsystem
31760Sstevel@tonic-gate * assures that a cyclic will never fire late or spuriously, even while
31770Sstevel@tonic-gate * being juggled.
31780Sstevel@tonic-gate *
31790Sstevel@tonic-gate * Return value
31800Sstevel@tonic-gate *
31810Sstevel@tonic-gate * cyclic_juggle() returns a non-zero value if all cyclics were able to
31820Sstevel@tonic-gate * be juggled away from the CPU, and zero if one or more cyclics could
31830Sstevel@tonic-gate * not be juggled away.
31840Sstevel@tonic-gate *
31850Sstevel@tonic-gate * Caller's context
31860Sstevel@tonic-gate *
31870Sstevel@tonic-gate * cpu_lock must be held by the caller, and the caller must not be in
31880Sstevel@tonic-gate * interrupt context. The caller may not hold any locks which are also
31890Sstevel@tonic-gate * grabbed by any cyclic handler. While cyclic_juggle() _may_ be called
31900Sstevel@tonic-gate * in any context satisfying these constraints, it _must_ be called
31910Sstevel@tonic-gate * immediately after clearing CPU_ENABLE (i.e. before dropping cpu_lock).
31920Sstevel@tonic-gate * Failure to do so could result in an assertion failure in the cyclic
31930Sstevel@tonic-gate * subsystem.
31940Sstevel@tonic-gate */
31950Sstevel@tonic-gate int
cyclic_juggle(cpu_t * c)31960Sstevel@tonic-gate cyclic_juggle(cpu_t *c)
31970Sstevel@tonic-gate {
31980Sstevel@tonic-gate cyc_cpu_t *cpu = c->cpu_cyclic;
31990Sstevel@tonic-gate cyc_id_t *idp;
32000Sstevel@tonic-gate int all_juggled = 1;
32010Sstevel@tonic-gate
32020Sstevel@tonic-gate CYC_PTRACE1("juggle", c);
32030Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
32040Sstevel@tonic-gate
32050Sstevel@tonic-gate /*
32060Sstevel@tonic-gate * We'll go through each cyclic on the CPU, attempting to juggle
32070Sstevel@tonic-gate * each one elsewhere.
32080Sstevel@tonic-gate */
32090Sstevel@tonic-gate for (idp = cyclic_id_head; idp != NULL; idp = idp->cyi_next) {
32100Sstevel@tonic-gate if (idp->cyi_cpu != cpu)
32110Sstevel@tonic-gate continue;
32120Sstevel@tonic-gate
32130Sstevel@tonic-gate if (cyclic_juggle_one(idp) == 0) {
32140Sstevel@tonic-gate all_juggled = 0;
32150Sstevel@tonic-gate continue;
32160Sstevel@tonic-gate }
32170Sstevel@tonic-gate
32180Sstevel@tonic-gate ASSERT(idp->cyi_cpu != cpu);
32190Sstevel@tonic-gate }
32200Sstevel@tonic-gate
32210Sstevel@tonic-gate return (all_juggled);
32220Sstevel@tonic-gate }
32230Sstevel@tonic-gate
32240Sstevel@tonic-gate /*
32250Sstevel@tonic-gate * int cyclic_offline(cpu_t *)
32260Sstevel@tonic-gate *
32270Sstevel@tonic-gate * Overview
32280Sstevel@tonic-gate *
32290Sstevel@tonic-gate * cyclic_offline() offlines the cyclic subsystem on the specified CPU.
32300Sstevel@tonic-gate *
32310Sstevel@tonic-gate * Arguments and notes
32320Sstevel@tonic-gate *
32330Sstevel@tonic-gate * The only argument to cyclic_offline() is a CPU to offline.
32340Sstevel@tonic-gate * cyclic_offline() will attempt to juggle cyclics away from the specified
32350Sstevel@tonic-gate * CPU.
32360Sstevel@tonic-gate *
32370Sstevel@tonic-gate * Return value
32380Sstevel@tonic-gate *
32390Sstevel@tonic-gate * cyclic_offline() returns 1 if all cyclics on the CPU were juggled away
32400Sstevel@tonic-gate * and the cyclic subsystem on the CPU was successfully offlines.
32410Sstevel@tonic-gate * cyclic_offline returns 0 if some cyclics remain, blocking the cyclic
32420Sstevel@tonic-gate * offline operation. All remaining cyclics on the CPU will either be
32430Sstevel@tonic-gate * CPU- or partition-bound.
32440Sstevel@tonic-gate *
32450Sstevel@tonic-gate * See the "Arguments and notes" of cyclic_juggle(), below, for more detail
32460Sstevel@tonic-gate * on cyclic juggling.
32470Sstevel@tonic-gate *
32480Sstevel@tonic-gate * Caller's context
32490Sstevel@tonic-gate *
32500Sstevel@tonic-gate * The only caller of cyclic_offline() should be the processor management
32510Sstevel@tonic-gate * subsystem. It is expected that the caller of cyclic_offline() will
32520Sstevel@tonic-gate * offline the CPU immediately after cyclic_offline() returns success (i.e.
32530Sstevel@tonic-gate * before dropping cpu_lock). Moreover, it is expected that the caller will
32540Sstevel@tonic-gate * fail the CPU offline operation if cyclic_offline() returns failure.
32550Sstevel@tonic-gate */
32560Sstevel@tonic-gate int
cyclic_offline(cpu_t * c)32570Sstevel@tonic-gate cyclic_offline(cpu_t *c)
32580Sstevel@tonic-gate {
32590Sstevel@tonic-gate cyc_cpu_t *cpu = c->cpu_cyclic;
32600Sstevel@tonic-gate cyc_id_t *idp;
32610Sstevel@tonic-gate
32620Sstevel@tonic-gate CYC_PTRACE1("offline", cpu);
32630Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
32640Sstevel@tonic-gate
32650Sstevel@tonic-gate if (!cyclic_juggle(c))
32660Sstevel@tonic-gate return (0);
32670Sstevel@tonic-gate
32680Sstevel@tonic-gate /*
32690Sstevel@tonic-gate * This CPU is headed offline; we need to now stop omnipresent
32700Sstevel@tonic-gate * cyclic firing on this CPU.
32710Sstevel@tonic-gate */
32720Sstevel@tonic-gate for (idp = cyclic_id_head; idp != NULL; idp = idp->cyi_next) {
32730Sstevel@tonic-gate if (idp->cyi_cpu != NULL)
32740Sstevel@tonic-gate continue;
32750Sstevel@tonic-gate
32760Sstevel@tonic-gate /*
32770Sstevel@tonic-gate * We cannot possibly be offlining the last CPU; cyi_omni_list
32780Sstevel@tonic-gate * must be non-NULL.
32790Sstevel@tonic-gate */
32800Sstevel@tonic-gate ASSERT(idp->cyi_omni_list != NULL);
32810Sstevel@tonic-gate cyclic_omni_stop(idp, cpu);
32820Sstevel@tonic-gate }
32830Sstevel@tonic-gate
32840Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_ONLINE);
32850Sstevel@tonic-gate cpu->cyp_state = CYS_OFFLINE;
32860Sstevel@tonic-gate
32870Sstevel@tonic-gate return (1);
32880Sstevel@tonic-gate }
32890Sstevel@tonic-gate
32900Sstevel@tonic-gate /*
32910Sstevel@tonic-gate * void cyclic_online(cpu_t *)
32920Sstevel@tonic-gate *
32930Sstevel@tonic-gate * Overview
32940Sstevel@tonic-gate *
32950Sstevel@tonic-gate * cyclic_online() onlines a CPU previously offlined with cyclic_offline().
32960Sstevel@tonic-gate *
32970Sstevel@tonic-gate * Arguments and notes
32980Sstevel@tonic-gate *
32990Sstevel@tonic-gate * cyclic_online()'s only argument is a CPU to online. The specified
33000Sstevel@tonic-gate * CPU must have been previously offlined with cyclic_offline(). After
33010Sstevel@tonic-gate * cyclic_online() returns, the specified CPU will be eligible to execute
33020Sstevel@tonic-gate * cyclics.
33030Sstevel@tonic-gate *
33040Sstevel@tonic-gate * Return value
33050Sstevel@tonic-gate *
33060Sstevel@tonic-gate * None; cyclic_online() always succeeds.
33070Sstevel@tonic-gate *
33080Sstevel@tonic-gate * Caller's context
33090Sstevel@tonic-gate *
33100Sstevel@tonic-gate * cyclic_online() should only be called by the processor management
33110Sstevel@tonic-gate * subsystem; cpu_lock must be held.
33120Sstevel@tonic-gate */
33130Sstevel@tonic-gate void
cyclic_online(cpu_t * c)33140Sstevel@tonic-gate cyclic_online(cpu_t *c)
33150Sstevel@tonic-gate {
33160Sstevel@tonic-gate cyc_cpu_t *cpu = c->cpu_cyclic;
33170Sstevel@tonic-gate cyc_id_t *idp;
33180Sstevel@tonic-gate
33190Sstevel@tonic-gate CYC_PTRACE1("online", cpu);
33200Sstevel@tonic-gate ASSERT(c->cpu_flags & CPU_ENABLE);
33210Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
33220Sstevel@tonic-gate ASSERT(cpu->cyp_state == CYS_OFFLINE);
33230Sstevel@tonic-gate
33240Sstevel@tonic-gate cpu->cyp_state = CYS_ONLINE;
33250Sstevel@tonic-gate
33260Sstevel@tonic-gate /*
33270Sstevel@tonic-gate * Now that this CPU is open for business, we need to start firing
33280Sstevel@tonic-gate * all omnipresent cyclics on it.
33290Sstevel@tonic-gate */
33300Sstevel@tonic-gate for (idp = cyclic_id_head; idp != NULL; idp = idp->cyi_next) {
33310Sstevel@tonic-gate if (idp->cyi_cpu != NULL)
33320Sstevel@tonic-gate continue;
33330Sstevel@tonic-gate
33340Sstevel@tonic-gate cyclic_omni_start(idp, cpu);
33350Sstevel@tonic-gate }
33360Sstevel@tonic-gate }
33370Sstevel@tonic-gate
33380Sstevel@tonic-gate /*
33390Sstevel@tonic-gate * void cyclic_move_in(cpu_t *)
33400Sstevel@tonic-gate *
33410Sstevel@tonic-gate * Overview
33420Sstevel@tonic-gate *
33430Sstevel@tonic-gate * cyclic_move_in() is called by the CPU partition code immediately after
33440Sstevel@tonic-gate * the specified CPU has moved into a new partition.
33450Sstevel@tonic-gate *
33460Sstevel@tonic-gate * Arguments and notes
33470Sstevel@tonic-gate *
33480Sstevel@tonic-gate * The only argument to cyclic_move_in() is a CPU which has moved into a
33490Sstevel@tonic-gate * new partition. If the specified CPU is P_ONLINE, and every other
33500Sstevel@tonic-gate * CPU in the specified CPU's new partition is P_NOINTR, cyclic_move_in()
33510Sstevel@tonic-gate * will juggle all partition-bound, CPU-unbound cyclics to the specified
33520Sstevel@tonic-gate * CPU.
33530Sstevel@tonic-gate *
33540Sstevel@tonic-gate * Return value
33550Sstevel@tonic-gate *
33560Sstevel@tonic-gate * None; cyclic_move_in() always succeeds.
33570Sstevel@tonic-gate *
33580Sstevel@tonic-gate * Caller's context
33590Sstevel@tonic-gate *
33600Sstevel@tonic-gate * cyclic_move_in() should _only_ be called immediately after a CPU has
33610Sstevel@tonic-gate * moved into a new partition, with cpu_lock held. As with other calls
33620Sstevel@tonic-gate * into the cyclic subsystem, no lock may be held which is also grabbed
33630Sstevel@tonic-gate * by any cyclic handler.
33640Sstevel@tonic-gate */
33650Sstevel@tonic-gate void
cyclic_move_in(cpu_t * d)33660Sstevel@tonic-gate cyclic_move_in(cpu_t *d)
33670Sstevel@tonic-gate {
33680Sstevel@tonic-gate cyc_id_t *idp;
33690Sstevel@tonic-gate cyc_cpu_t *dest = d->cpu_cyclic;
33700Sstevel@tonic-gate cyclic_t *cyclic;
33710Sstevel@tonic-gate cpupart_t *part = d->cpu_part;
33720Sstevel@tonic-gate
33730Sstevel@tonic-gate CYC_PTRACE("move-in", dest, part);
33740Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
33750Sstevel@tonic-gate
33760Sstevel@tonic-gate /*
33770Sstevel@tonic-gate * Look for CYF_PART_BOUND cyclics in the new partition. If
33780Sstevel@tonic-gate * we find one, check to see if it is currently on a CPU which has
33790Sstevel@tonic-gate * interrupts disabled. If it is (and if this CPU currently has
33800Sstevel@tonic-gate * interrupts enabled), we'll juggle those cyclics over here.
33810Sstevel@tonic-gate */
33820Sstevel@tonic-gate if (!(d->cpu_flags & CPU_ENABLE)) {
33830Sstevel@tonic-gate CYC_PTRACE1("move-in-none", dest);
33840Sstevel@tonic-gate return;
33850Sstevel@tonic-gate }
33860Sstevel@tonic-gate
33870Sstevel@tonic-gate for (idp = cyclic_id_head; idp != NULL; idp = idp->cyi_next) {
33880Sstevel@tonic-gate cyc_cpu_t *cpu = idp->cyi_cpu;
33890Sstevel@tonic-gate cpu_t *c;
33900Sstevel@tonic-gate
33910Sstevel@tonic-gate /*
33920Sstevel@tonic-gate * Omnipresent cyclics are exempt from juggling.
33930Sstevel@tonic-gate */
33940Sstevel@tonic-gate if (cpu == NULL)
33950Sstevel@tonic-gate continue;
33960Sstevel@tonic-gate
33970Sstevel@tonic-gate c = cpu->cyp_cpu;
33980Sstevel@tonic-gate
33990Sstevel@tonic-gate if (c->cpu_part != part || (c->cpu_flags & CPU_ENABLE))
34000Sstevel@tonic-gate continue;
34010Sstevel@tonic-gate
34020Sstevel@tonic-gate cyclic = &cpu->cyp_cyclics[idp->cyi_ndx];
34030Sstevel@tonic-gate
34040Sstevel@tonic-gate if (cyclic->cy_flags & CYF_CPU_BOUND)
34050Sstevel@tonic-gate continue;
34060Sstevel@tonic-gate
34070Sstevel@tonic-gate /*
34080Sstevel@tonic-gate * We know that this cyclic is bound to its processor set
34090Sstevel@tonic-gate * (otherwise, it would not be on a CPU with interrupts
34100Sstevel@tonic-gate * disabled); juggle it to our CPU.
34110Sstevel@tonic-gate */
34120Sstevel@tonic-gate ASSERT(cyclic->cy_flags & CYF_PART_BOUND);
34130Sstevel@tonic-gate cyclic_juggle_one_to(idp, dest);
34140Sstevel@tonic-gate }
34150Sstevel@tonic-gate
34160Sstevel@tonic-gate CYC_PTRACE1("move-in-done", dest);
34170Sstevel@tonic-gate }
34180Sstevel@tonic-gate
34190Sstevel@tonic-gate /*
34200Sstevel@tonic-gate * int cyclic_move_out(cpu_t *)
34210Sstevel@tonic-gate *
34220Sstevel@tonic-gate * Overview
34230Sstevel@tonic-gate *
34240Sstevel@tonic-gate * cyclic_move_out() is called by the CPU partition code immediately before
34250Sstevel@tonic-gate * the specified CPU is to move out of its partition.
34260Sstevel@tonic-gate *
34270Sstevel@tonic-gate * Arguments and notes
34280Sstevel@tonic-gate *
34290Sstevel@tonic-gate * The only argument to cyclic_move_out() is a CPU which is to move out of
34300Sstevel@tonic-gate * its partition.
34310Sstevel@tonic-gate *
34320Sstevel@tonic-gate * cyclic_move_out() will attempt to juggle away all partition-bound
34330Sstevel@tonic-gate * cyclics. If the specified CPU is the last CPU in a partition with
34340Sstevel@tonic-gate * partition-bound cyclics, cyclic_move_out() will fail. If there exists
34350Sstevel@tonic-gate * a partition-bound cyclic which is CPU-bound to the specified CPU,
34360Sstevel@tonic-gate * cyclic_move_out() will fail.
34370Sstevel@tonic-gate *
34380Sstevel@tonic-gate * Note that cyclic_move_out() will _only_ attempt to juggle away
34390Sstevel@tonic-gate * partition-bound cyclics; CPU-bound cyclics which are not partition-bound
34400Sstevel@tonic-gate * and unbound cyclics are not affected by changing the partition
34410Sstevel@tonic-gate * affiliation of the CPU.
34420Sstevel@tonic-gate *
34430Sstevel@tonic-gate * Return value
34440Sstevel@tonic-gate *
34450Sstevel@tonic-gate * cyclic_move_out() returns 1 if all partition-bound cyclics on the CPU
34460Sstevel@tonic-gate * were juggled away; 0 if some cyclics remain.
34470Sstevel@tonic-gate *
34480Sstevel@tonic-gate * Caller's context
34490Sstevel@tonic-gate *
34500Sstevel@tonic-gate * cyclic_move_out() should _only_ be called immediately before a CPU has
34510Sstevel@tonic-gate * moved out of its partition, with cpu_lock held. It is expected that
34520Sstevel@tonic-gate * the caller of cyclic_move_out() will change the processor set affiliation
34530Sstevel@tonic-gate * of the specified CPU immediately after cyclic_move_out() returns
34540Sstevel@tonic-gate * success (i.e. before dropping cpu_lock). Moreover, it is expected that
34550Sstevel@tonic-gate * the caller will fail the CPU repartitioning operation if cyclic_move_out()
34560Sstevel@tonic-gate * returns failure. As with other calls into the cyclic subsystem, no lock
34570Sstevel@tonic-gate * may be held which is also grabbed by any cyclic handler.
34580Sstevel@tonic-gate */
34590Sstevel@tonic-gate int
cyclic_move_out(cpu_t * c)34600Sstevel@tonic-gate cyclic_move_out(cpu_t *c)
34610Sstevel@tonic-gate {
34620Sstevel@tonic-gate cyc_id_t *idp;
34630Sstevel@tonic-gate cyc_cpu_t *cpu = c->cpu_cyclic, *dest;
34640Sstevel@tonic-gate cyclic_t *cyclic, *cyclics = cpu->cyp_cyclics;
34650Sstevel@tonic-gate cpupart_t *part = c->cpu_part;
34660Sstevel@tonic-gate
34670Sstevel@tonic-gate CYC_PTRACE1("move-out", cpu);
34680Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
34690Sstevel@tonic-gate
34700Sstevel@tonic-gate /*
34710Sstevel@tonic-gate * If there are any CYF_PART_BOUND cyclics on this CPU, we need
34720Sstevel@tonic-gate * to try to juggle them away.
34730Sstevel@tonic-gate */
34740Sstevel@tonic-gate for (idp = cyclic_id_head; idp != NULL; idp = idp->cyi_next) {
34750Sstevel@tonic-gate
34760Sstevel@tonic-gate if (idp->cyi_cpu != cpu)
34770Sstevel@tonic-gate continue;
34780Sstevel@tonic-gate
34790Sstevel@tonic-gate cyclic = &cyclics[idp->cyi_ndx];
34800Sstevel@tonic-gate
34810Sstevel@tonic-gate if (!(cyclic->cy_flags & CYF_PART_BOUND))
34820Sstevel@tonic-gate continue;
34830Sstevel@tonic-gate
34840Sstevel@tonic-gate dest = cyclic_pick_cpu(part, c, c, cyclic->cy_flags);
34850Sstevel@tonic-gate
34860Sstevel@tonic-gate if (dest == NULL) {
34870Sstevel@tonic-gate /*
34880Sstevel@tonic-gate * We can't juggle this cyclic; we need to return
34890Sstevel@tonic-gate * failure (we won't bother trying to juggle away
34900Sstevel@tonic-gate * other cyclics).
34910Sstevel@tonic-gate */
34920Sstevel@tonic-gate CYC_PTRACE("move-out-fail", cpu, idp);
34930Sstevel@tonic-gate return (0);
34940Sstevel@tonic-gate }
34950Sstevel@tonic-gate cyclic_juggle_one_to(idp, dest);
34960Sstevel@tonic-gate }
34970Sstevel@tonic-gate
34980Sstevel@tonic-gate CYC_PTRACE1("move-out-done", cpu);
34990Sstevel@tonic-gate return (1);
35000Sstevel@tonic-gate }
35010Sstevel@tonic-gate
35020Sstevel@tonic-gate /*
35030Sstevel@tonic-gate * void cyclic_suspend()
35040Sstevel@tonic-gate *
35050Sstevel@tonic-gate * Overview
35060Sstevel@tonic-gate *
35070Sstevel@tonic-gate * cyclic_suspend() suspends all cyclic activity throughout the cyclic
35080Sstevel@tonic-gate * subsystem. It should be called only by subsystems which are attempting
35090Sstevel@tonic-gate * to suspend the entire system (e.g. checkpoint/resume, dynamic
35100Sstevel@tonic-gate * reconfiguration).
35110Sstevel@tonic-gate *
35120Sstevel@tonic-gate * Arguments and notes
35130Sstevel@tonic-gate *
35140Sstevel@tonic-gate * cyclic_suspend() takes no arguments. Each CPU with an active cyclic
35150Sstevel@tonic-gate * disables its backend (offline CPUs disable their backends as part of
35160Sstevel@tonic-gate * the cyclic_offline() operation), thereby disabling future CY_HIGH_LEVEL
35170Sstevel@tonic-gate * interrupts.
35180Sstevel@tonic-gate *
35190Sstevel@tonic-gate * Note that disabling CY_HIGH_LEVEL interrupts does not completely preclude
35200Sstevel@tonic-gate * cyclic handlers from being called after cyclic_suspend() returns: if a
35210Sstevel@tonic-gate * CY_LOCK_LEVEL or CY_LOW_LEVEL interrupt thread was blocked at the time
35220Sstevel@tonic-gate * of cyclic_suspend(), cyclic handlers at its level may continue to be
35230Sstevel@tonic-gate * called after the interrupt thread becomes unblocked. The
35240Sstevel@tonic-gate * post-cyclic_suspend() activity is bounded by the pend count on all
35250Sstevel@tonic-gate * cyclics at the time of cyclic_suspend(). Callers concerned with more
35260Sstevel@tonic-gate * than simply disabling future CY_HIGH_LEVEL interrupts must check for
35270Sstevel@tonic-gate * this condition.
35280Sstevel@tonic-gate *
35290Sstevel@tonic-gate * On most platforms, timestamps from gethrtime() and gethrestime() are not
35300Sstevel@tonic-gate * guaranteed to monotonically increase between cyclic_suspend() and
35310Sstevel@tonic-gate * cyclic_resume(). However, timestamps are guaranteed to monotonically
35320Sstevel@tonic-gate * increase across the entire cyclic_suspend()/cyclic_resume() operation.
35330Sstevel@tonic-gate * That is, every timestamp obtained before cyclic_suspend() will be less
35340Sstevel@tonic-gate * than every timestamp obtained after cyclic_resume().
35350Sstevel@tonic-gate *
35360Sstevel@tonic-gate * Return value
35370Sstevel@tonic-gate *
35380Sstevel@tonic-gate * None; cyclic_suspend() always succeeds.
35390Sstevel@tonic-gate *
35400Sstevel@tonic-gate * Caller's context
35410Sstevel@tonic-gate *
35420Sstevel@tonic-gate * The cyclic subsystem must be configured on every valid CPU;
35430Sstevel@tonic-gate * cyclic_suspend() may not be called during boot or during dynamic
35440Sstevel@tonic-gate * reconfiguration. Additionally, cpu_lock must be held, and the caller
35450Sstevel@tonic-gate * cannot be in high-level interrupt context. However, unlike most other
35460Sstevel@tonic-gate * cyclic entry points, cyclic_suspend() may be called with locks held
35470Sstevel@tonic-gate * which are also acquired by CY_LOCK_LEVEL or CY_LOW_LEVEL cyclic
35480Sstevel@tonic-gate * handlers.
35490Sstevel@tonic-gate */
35500Sstevel@tonic-gate void
cyclic_suspend()35510Sstevel@tonic-gate cyclic_suspend()
35520Sstevel@tonic-gate {
35530Sstevel@tonic-gate cpu_t *c;
35540Sstevel@tonic-gate cyc_cpu_t *cpu;
35550Sstevel@tonic-gate cyc_xcallarg_t arg;
35560Sstevel@tonic-gate cyc_backend_t *be;
35570Sstevel@tonic-gate
35580Sstevel@tonic-gate CYC_PTRACE0("suspend");
35590Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
35600Sstevel@tonic-gate c = cpu_list;
35610Sstevel@tonic-gate
35620Sstevel@tonic-gate do {
35630Sstevel@tonic-gate cpu = c->cpu_cyclic;
35640Sstevel@tonic-gate be = cpu->cyp_backend;
35650Sstevel@tonic-gate arg.cyx_cpu = cpu;
35660Sstevel@tonic-gate
35670Sstevel@tonic-gate be->cyb_xcall(be->cyb_arg, c,
35680Sstevel@tonic-gate (cyc_func_t)cyclic_suspend_xcall, &arg);
35690Sstevel@tonic-gate } while ((c = c->cpu_next) != cpu_list);
35700Sstevel@tonic-gate }
35710Sstevel@tonic-gate
35720Sstevel@tonic-gate /*
35730Sstevel@tonic-gate * void cyclic_resume()
35740Sstevel@tonic-gate *
35750Sstevel@tonic-gate * cyclic_resume() resumes all cyclic activity throughout the cyclic
35760Sstevel@tonic-gate * subsystem. It should be called only by system-suspending subsystems.
35770Sstevel@tonic-gate *
35780Sstevel@tonic-gate * Arguments and notes
35790Sstevel@tonic-gate *
35800Sstevel@tonic-gate * cyclic_resume() takes no arguments. Each CPU with an active cyclic
35810Sstevel@tonic-gate * reenables and reprograms its backend (offline CPUs are not reenabled).
35820Sstevel@tonic-gate * On most platforms, timestamps from gethrtime() and gethrestime() are not
35830Sstevel@tonic-gate * guaranteed to monotonically increase between cyclic_suspend() and
35840Sstevel@tonic-gate * cyclic_resume(). However, timestamps are guaranteed to monotonically
35850Sstevel@tonic-gate * increase across the entire cyclic_suspend()/cyclic_resume() operation.
35860Sstevel@tonic-gate * That is, every timestamp obtained before cyclic_suspend() will be less
35870Sstevel@tonic-gate * than every timestamp obtained after cyclic_resume().
35880Sstevel@tonic-gate *
35890Sstevel@tonic-gate * Return value
35900Sstevel@tonic-gate *
35910Sstevel@tonic-gate * None; cyclic_resume() always succeeds.
35920Sstevel@tonic-gate *
35930Sstevel@tonic-gate * Caller's context
35940Sstevel@tonic-gate *
35950Sstevel@tonic-gate * The cyclic subsystem must be configured on every valid CPU;
35960Sstevel@tonic-gate * cyclic_resume() may not be called during boot or during dynamic
35970Sstevel@tonic-gate * reconfiguration. Additionally, cpu_lock must be held, and the caller
35980Sstevel@tonic-gate * cannot be in high-level interrupt context. However, unlike most other
35990Sstevel@tonic-gate * cyclic entry points, cyclic_resume() may be called with locks held which
36000Sstevel@tonic-gate * are also acquired by CY_LOCK_LEVEL or CY_LOW_LEVEL cyclic handlers.
36010Sstevel@tonic-gate */
36020Sstevel@tonic-gate void
cyclic_resume()36030Sstevel@tonic-gate cyclic_resume()
36040Sstevel@tonic-gate {
36050Sstevel@tonic-gate cpu_t *c;
36060Sstevel@tonic-gate cyc_cpu_t *cpu;
36070Sstevel@tonic-gate cyc_xcallarg_t arg;
36080Sstevel@tonic-gate cyc_backend_t *be;
36090Sstevel@tonic-gate
36100Sstevel@tonic-gate CYC_PTRACE0("resume");
36110Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock));
36120Sstevel@tonic-gate
36130Sstevel@tonic-gate c = cpu_list;
36140Sstevel@tonic-gate
36150Sstevel@tonic-gate do {
36160Sstevel@tonic-gate cpu = c->cpu_cyclic;
36170Sstevel@tonic-gate be = cpu->cyp_backend;
36180Sstevel@tonic-gate arg.cyx_cpu = cpu;
36190Sstevel@tonic-gate
36200Sstevel@tonic-gate be->cyb_xcall(be->cyb_arg, c,
36210Sstevel@tonic-gate (cyc_func_t)cyclic_resume_xcall, &arg);
36220Sstevel@tonic-gate } while ((c = c->cpu_next) != cpu_list);
36230Sstevel@tonic-gate }
3624