1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright 2018 Joyent Inc. 28 */ 29 30 /* 31 * The Cyclic Subsystem 32 * -------------------- 33 * 34 * Prehistory 35 * 36 * Historically, most computer architectures have specified interval-based 37 * timer parts (e.g. SPARCstation's counter/timer; Intel's i8254). While 38 * these parts deal in relative (i.e. not absolute) time values, they are 39 * typically used by the operating system to implement the abstraction of 40 * absolute time. As a result, these parts cannot typically be reprogrammed 41 * without introducing error in the system's notion of time. 42 * 43 * Starting in about 1994, chip architectures began specifying high resolution 44 * timestamp registers. As of this writing (1999), all major chip families 45 * (UltraSPARC, PentiumPro, MIPS, PowerPC, Alpha) have high resolution 46 * timestamp registers, and two (UltraSPARC and MIPS) have added the capacity 47 * to interrupt based on timestamp values. These timestamp-compare registers 48 * present a time-based interrupt source which can be reprogrammed arbitrarily 49 * often without introducing error. Given the low cost of implementing such a 50 * timestamp-compare register (and the tangible benefit of eliminating 51 * discrete timer parts), it is reasonable to expect that future chip 52 * architectures will adopt this feature. 53 * 54 * The cyclic subsystem has been designed to take advantage of chip 55 * architectures with the capacity to interrupt based on absolute, high 56 * resolution values of time. 57 * 58 * Subsystem Overview 59 * 60 * The cyclic subsystem is a low-level kernel subsystem designed to provide 61 * arbitrarily high resolution, per-CPU interval timers (to avoid colliding 62 * with existing terms, we dub such an interval timer a "cyclic"). Cyclics 63 * can be specified to fire at high, lock or low interrupt level, and may be 64 * optionally bound to a CPU or a CPU partition. A cyclic's CPU or CPU 65 * partition binding may be changed dynamically; the cyclic will be "juggled" 66 * to a CPU which satisfies the new binding. Alternatively, a cyclic may 67 * be specified to be "omnipresent", denoting firing on all online CPUs. 68 * 69 * Cyclic Subsystem Interface Overview 70 * ----------------------------------- 71 * 72 * The cyclic subsystem has interfaces with the kernel at-large, with other 73 * kernel subsystems (e.g. the processor management subsystem, the checkpoint 74 * resume subsystem) and with the platform (the cyclic backend). Each 75 * of these interfaces is given a brief synopsis here, and is described 76 * in full above the interface's implementation. 77 * 78 * The following diagram displays the cyclic subsystem's interfaces to 79 * other kernel components. The arrows denote a "calls" relationship, with 80 * the large arrow indicating the cyclic subsystem's consumer interface. 81 * Each arrow is labeled with the section in which the corresponding 82 * interface is described. 83 * 84 * Kernel at-large consumers 85 * -----------++------------ 86 * || 87 * || 88 * _||_ 89 * \ / 90 * \/ 91 * +---------------------+ 92 * | | 93 * | Cyclic subsystem |<----------- Other kernel subsystems 94 * | | 95 * +---------------------+ 96 * ^ | 97 * | | 98 * | | 99 * | v 100 * +---------------------+ 101 * | | 102 * | Cyclic backend | 103 * | (platform specific) | 104 * | | 105 * +---------------------+ 106 * 107 * 108 * Kernel At-Large Interfaces 109 * 110 * cyclic_add() <-- Creates a cyclic 111 * cyclic_add_omni() <-- Creates an omnipresent cyclic 112 * cyclic_remove() <-- Removes a cyclic 113 * cyclic_bind() <-- Change a cyclic's CPU or partition binding 114 * cyclic_reprogram() <-- Reprogram a cyclic's expiration 115 * cyclic_move_here() <-- Shuffle cyclic to current CPU 116 * 117 * Inter-subsystem Interfaces 118 * 119 * cyclic_juggle() <-- Juggles cyclics away from a CPU 120 * cyclic_offline() <-- Offlines cyclic operation on a CPU 121 * cyclic_online() <-- Reenables operation on an offlined CPU 122 * cyclic_move_in() <-- Notifies subsystem of change in CPU partition 123 * cyclic_move_out() <-- Notifies subsystem of change in CPU partition 124 * cyclic_suspend() <-- Suspends the cyclic subsystem on all CPUs 125 * cyclic_resume() <-- Resumes the cyclic subsystem on all CPUs 126 * 127 * Backend Interfaces 128 * 129 * cyclic_init() <-- Initializes the cyclic subsystem 130 * cyclic_fire() <-- CY_HIGH_LEVEL interrupt entry point 131 * cyclic_softint() <-- CY_LOCK/LOW_LEVEL soft interrupt entry point 132 * 133 * The backend-supplied interfaces (through the cyc_backend structure) are 134 * documented in detail in <sys/cyclic_impl.h> 135 * 136 * 137 * Cyclic Subsystem Implementation Overview 138 * ---------------------------------------- 139 * 140 * The cyclic subsystem is designed to minimize interference between cyclics 141 * on different CPUs. Thus, all of the cyclic subsystem's data structures 142 * hang off of a per-CPU structure, cyc_cpu. 143 * 144 * Each cyc_cpu has a power-of-two sized array of cyclic structures (the 145 * cyp_cyclics member of the cyc_cpu structure). If cyclic_add() is called 146 * and there does not exist a free slot in the cyp_cyclics array, the size of 147 * the array will be doubled. The array will never shrink. Cyclics are 148 * referred to by their index in the cyp_cyclics array, which is of type 149 * cyc_index_t. 150 * 151 * The cyclics are kept sorted by expiration time in the cyc_cpu's heap. The 152 * heap is keyed by cyclic expiration time, with parents expiring earlier 153 * than their children. 154 * 155 * Heap Management 156 * 157 * The heap is managed primarily by cyclic_fire(). Upon entry, cyclic_fire() 158 * compares the root cyclic's expiration time to the current time. If the 159 * expiration time is in the past, cyclic_expire() is called on the root 160 * cyclic. Upon return from cyclic_expire(), the cyclic's new expiration time 161 * is derived by adding its interval to its old expiration time, and a 162 * downheap operation is performed. After the downheap, cyclic_fire() 163 * examines the (potentially changed) root cyclic, repeating the 164 * cyclic_expire()/add interval/cyclic_downheap() sequence until the root 165 * cyclic has an expiration time in the future. This expiration time 166 * (guaranteed to be the earliest in the heap) is then communicated to the 167 * backend via cyb_reprogram. Optimal backends will next call cyclic_fire() 168 * shortly after the root cyclic's expiration time. 169 * 170 * To allow efficient, deterministic downheap operations, we implement the 171 * heap as an array (the cyp_heap member of the cyc_cpu structure), with each 172 * element containing an index into the CPU's cyp_cyclics array. 173 * 174 * The heap is laid out in the array according to the following: 175 * 176 * 1. The root of the heap is always in the 0th element of the heap array 177 * 2. The left and right children of the nth element are element 178 * (((n + 1) << 1) - 1) and element ((n + 1) << 1), respectively. 179 * 180 * This layout is standard (see, e.g., Cormen's "Algorithms"); the proof 181 * that these constraints correctly lay out a heap (or indeed, any binary 182 * tree) is trivial and left to the reader. 183 * 184 * To see the heap by example, assume our cyclics array has the following 185 * members (at time t): 186 * 187 * cy_handler cy_level cy_expire 188 * --------------------------------------------- 189 * [ 0] clock() LOCK t+10000000 190 * [ 1] deadman() HIGH t+1000000000 191 * [ 2] clock_highres_fire() LOW t+100 192 * [ 3] clock_highres_fire() LOW t+1000 193 * [ 4] clock_highres_fire() LOW t+500 194 * [ 5] (free) -- -- 195 * [ 6] (free) -- -- 196 * [ 7] (free) -- -- 197 * 198 * The heap array could be: 199 * 200 * [0] [1] [2] [3] [4] [5] [6] [7] 201 * +-----+-----+-----+-----+-----+-----+-----+-----+ 202 * | | | | | | | | | 203 * | 2 | 3 | 4 | 0 | 1 | x | x | x | 204 * | | | | | | | | | 205 * +-----+-----+-----+-----+-----+-----+-----+-----+ 206 * 207 * Graphically, this array corresponds to the following (excuse the ASCII art): 208 * 209 * 2 210 * | 211 * +------------------+------------------+ 212 * 3 4 213 * | 214 * +---------+--------+ 215 * 0 1 216 * 217 * Note that the heap is laid out by layer: all nodes at a given depth are 218 * stored in consecutive elements of the array. Moreover, layers of 219 * consecutive depths are in adjacent element ranges. This property 220 * guarantees high locality of reference during downheap operations. 221 * Specifically, we are guaranteed that we can downheap to a depth of 222 * 223 * lg (cache_line_size / sizeof (cyc_index_t)) 224 * 225 * nodes with at most one cache miss. On UltraSPARC (64 byte e-cache line 226 * size), this corresponds to a depth of four nodes. Thus, if there are 227 * fewer than sixteen cyclics in the heap, downheaps on UltraSPARC miss at 228 * most once in the e-cache. 229 * 230 * Downheaps are required to compare siblings as they proceed down the 231 * heap. For downheaps proceeding beyond the one-cache-miss depth, every 232 * access to a left child could potentially miss in the cache. However, 233 * if we assume 234 * 235 * (cache_line_size / sizeof (cyc_index_t)) > 2, 236 * 237 * then all siblings are guaranteed to be on the same cache line. Thus, the 238 * miss on the left child will guarantee a hit on the right child; downheaps 239 * will incur at most one cache miss per layer beyond the one-cache-miss 240 * depth. The total number of cache misses for heap management during a 241 * downheap operation is thus bounded by 242 * 243 * lg (n) - lg (cache_line_size / sizeof (cyc_index_t)) 244 * 245 * Traditional pointer-based heaps are implemented without regard to 246 * locality. Downheaps can thus incur two cache misses per layer (one for 247 * each child), but at most one cache miss at the root. This yields a bound 248 * of 249 * 250 * 2 * lg (n) - 1 251 * 252 * on the total cache misses. 253 * 254 * This difference may seem theoretically trivial (the difference is, after 255 * all, constant), but can become substantial in practice -- especially for 256 * caches with very large cache lines and high miss penalties (e.g. TLBs). 257 * 258 * Heaps must always be full, balanced trees. Heap management must therefore 259 * track the next point-of-insertion into the heap. In pointer-based heaps, 260 * recomputing this point takes O(lg (n)). Given the layout of the 261 * array-based implementation, however, the next point-of-insertion is 262 * always: 263 * 264 * heap[number_of_elements] 265 * 266 * We exploit this property by implementing the free-list in the usused 267 * heap elements. Heap insertion, therefore, consists only of filling in 268 * the cyclic at cyp_cyclics[cyp_heap[number_of_elements]], incrementing 269 * the number of elements, and performing an upheap. Heap deletion consists 270 * of decrementing the number of elements, swapping the to-be-deleted element 271 * with the element at cyp_heap[number_of_elements], and downheaping. 272 * 273 * Filling in more details in our earlier example: 274 * 275 * +--- free list head 276 * | 277 * V 278 * 279 * [0] [1] [2] [3] [4] [5] [6] [7] 280 * +-----+-----+-----+-----+-----+-----+-----+-----+ 281 * | | | | | | | | | 282 * | 2 | 3 | 4 | 0 | 1 | 5 | 6 | 7 | 283 * | | | | | | | | | 284 * +-----+-----+-----+-----+-----+-----+-----+-----+ 285 * 286 * To insert into this heap, we would just need to fill in the cyclic at 287 * cyp_cyclics[5], bump the number of elements (from 5 to 6) and perform 288 * an upheap. 289 * 290 * If we wanted to remove, say, cyp_cyclics[3], we would first scan for it 291 * in the cyp_heap, and discover it at cyp_heap[1]. We would then decrement 292 * the number of elements (from 5 to 4), swap cyp_heap[1] with cyp_heap[4], 293 * and perform a downheap from cyp_heap[1]. The linear scan is required 294 * because the cyclic does not keep a backpointer into the heap. This makes 295 * heap manipulation (e.g. downheaps) faster at the expense of removal 296 * operations. 297 * 298 * Expiry processing 299 * 300 * As alluded to above, cyclic_expire() is called by cyclic_fire() at 301 * CY_HIGH_LEVEL to expire a cyclic. Cyclic subsystem consumers are 302 * guaranteed that for an arbitrary time t in the future, their cyclic 303 * handler will have been called (t - cyt_when) / cyt_interval times. Thus, 304 * there must be a one-to-one mapping between a cyclic's expiration at 305 * CY_HIGH_LEVEL and its execution at the desired level (either CY_HIGH_LEVEL, 306 * CY_LOCK_LEVEL or CY_LOW_LEVEL). 307 * 308 * For CY_HIGH_LEVEL cyclics, this is trivial; cyclic_expire() simply needs 309 * to call the handler. 310 * 311 * For CY_LOCK_LEVEL and CY_LOW_LEVEL cyclics, however, there exists a 312 * potential disconnect: if the CPU is at an interrupt level less than 313 * CY_HIGH_LEVEL but greater than the level of a cyclic for a period of 314 * time longer than twice the cyclic's interval, the cyclic will be expired 315 * twice before it can be handled. 316 * 317 * To maintain the one-to-one mapping, we track the difference between the 318 * number of times a cyclic has been expired and the number of times it's 319 * been handled in a "pending count" (the cy_pend field of the cyclic 320 * structure). cyclic_expire() thus increments the cy_pend count for the 321 * expired cyclic and posts a soft interrupt at the desired level. In the 322 * cyclic subsystem's soft interrupt handler, cyclic_softint(), we repeatedly 323 * call the cyclic handler and decrement cy_pend until we have decremented 324 * cy_pend to zero. 325 * 326 * The Producer/Consumer Buffer 327 * 328 * If we wish to avoid a linear scan of the cyclics array at soft interrupt 329 * level, cyclic_softint() must be able to quickly determine which cyclics 330 * have a non-zero cy_pend count. We thus introduce a per-soft interrupt 331 * level producer/consumer buffer shared with CY_HIGH_LEVEL. These buffers 332 * are encapsulated in the cyc_pcbuffer structure, and, like cyp_heap, are 333 * implemented as cyc_index_t arrays (the cypc_buf member of the cyc_pcbuffer 334 * structure). 335 * 336 * The producer (cyclic_expire() running at CY_HIGH_LEVEL) enqueues a cyclic 337 * by storing the cyclic's index to cypc_buf[cypc_prodndx] and incrementing 338 * cypc_prodndx. The consumer (cyclic_softint() running at either 339 * CY_LOCK_LEVEL or CY_LOW_LEVEL) dequeues a cyclic by loading from 340 * cypc_buf[cypc_consndx] and bumping cypc_consndx. The buffer is empty when 341 * cypc_prodndx == cypc_consndx. 342 * 343 * To bound the size of the producer/consumer buffer, cyclic_expire() only 344 * enqueues a cyclic if its cy_pend was zero (if the cyclic's cy_pend is 345 * non-zero, cyclic_expire() only bumps cy_pend). Symmetrically, 346 * cyclic_softint() only consumes a cyclic after it has decremented the 347 * cy_pend count to zero. 348 * 349 * Returning to our example, here is what the CY_LOW_LEVEL producer/consumer 350 * buffer might look like: 351 * 352 * cypc_consndx ---+ +--- cypc_prodndx 353 * | | 354 * V V 355 * 356 * [0] [1] [2] [3] [4] [5] [6] [7] 357 * +-----+-----+-----+-----+-----+-----+-----+-----+ 358 * | | | | | | | | | 359 * | x | x | 3 | 2 | 4 | x | x | x | <== cypc_buf 360 * | | | . | . | . | | | | 361 * +-----+-----+- | -+- | -+- | -+-----+-----+-----+ 362 * | | | 363 * | | | cy_pend cy_handler 364 * | | | ------------------------- 365 * | | | [ 0] 1 clock() 366 * | | | [ 1] 0 deadman() 367 * | +---- | -------> [ 2] 3 clock_highres_fire() 368 * +---------- | -------> [ 3] 1 clock_highres_fire() 369 * +--------> [ 4] 1 clock_highres_fire() 370 * [ 5] - (free) 371 * [ 6] - (free) 372 * [ 7] - (free) 373 * 374 * In particular, note that clock()'s cy_pend is 1 but that it is _not_ in 375 * this producer/consumer buffer; it would be enqueued in the CY_LOCK_LEVEL 376 * producer/consumer buffer. 377 * 378 * Locking 379 * 380 * Traditionally, access to per-CPU data structures shared between 381 * interrupt levels is serialized by manipulating programmable interrupt 382 * level: readers and writers are required to raise their interrupt level 383 * to that of the highest level writer. 384 * 385 * For the producer/consumer buffers (shared between cyclic_fire()/ 386 * cyclic_expire() executing at CY_HIGH_LEVEL and cyclic_softint() executing 387 * at one of CY_LOCK_LEVEL or CY_LOW_LEVEL), forcing cyclic_softint() to raise 388 * programmable interrupt level is undesirable: aside from the additional 389 * latency incurred by manipulating interrupt level in the hot cy_pend 390 * processing path, this would create the potential for soft level cy_pend 391 * processing to delay CY_HIGH_LEVEL firing and expiry processing. 392 * CY_LOCK/LOW_LEVEL cyclics could thereby induce jitter in CY_HIGH_LEVEL 393 * cyclics. 394 * 395 * To minimize jitter, then, we would like the cyclic_fire()/cyclic_expire() 396 * and cyclic_softint() code paths to be lock-free. 397 * 398 * For cyclic_fire()/cyclic_expire(), lock-free execution is straightforward: 399 * because these routines execute at a higher interrupt level than 400 * cyclic_softint(), their actions on the producer/consumer buffer appear 401 * atomic. In particular, the increment of cy_pend appears to occur 402 * atomically with the increment of cypc_prodndx. 403 * 404 * For cyclic_softint(), however, lock-free execution requires more delicacy. 405 * When cyclic_softint() discovers a cyclic in the producer/consumer buffer, 406 * it calls the cyclic's handler and attempts to atomically decrement the 407 * cy_pend count with a compare&swap operation. 408 * 409 * If the compare&swap operation succeeds, cyclic_softint() behaves 410 * conditionally based on the value it atomically wrote to cy_pend: 411 * 412 * - If the cy_pend was decremented to 0, the cyclic has been consumed; 413 * cyclic_softint() increments the cypc_consndx and checks for more 414 * enqueued work. 415 * 416 * - If the count was decremented to a non-zero value, there is more work 417 * to be done on the cyclic; cyclic_softint() calls the cyclic handler 418 * and repeats the atomic decrement process. 419 * 420 * If the compare&swap operation fails, cyclic_softint() knows that 421 * cyclic_expire() has intervened and bumped the cy_pend count (resizes 422 * and removals complicate this, however -- see the sections on their 423 * operation, below). cyclic_softint() thus reloads cy_pend, and re-attempts 424 * the atomic decrement. 425 * 426 * Recall that we bound the size of the producer/consumer buffer by 427 * having cyclic_expire() only enqueue the specified cyclic if its 428 * cy_pend count is zero; this assures that each cyclic is enqueued at 429 * most once. This leads to a critical constraint on cyclic_softint(), 430 * however: after the compare&swap operation which successfully decrements 431 * cy_pend to zero, cyclic_softint() must _not_ re-examine the consumed 432 * cyclic. In part to obey this constraint, cyclic_softint() calls the 433 * cyclic handler before decrementing cy_pend. 434 * 435 * Resizing 436 * 437 * All of the discussion thus far has assumed a static number of cyclics. 438 * Obviously, static limitations are not practical; we need the capacity 439 * to resize our data structures dynamically. 440 * 441 * We resize our data structures lazily, and only on a per-CPU basis. 442 * The size of the data structures always doubles and never shrinks. We 443 * serialize adds (and thus resizes) on cpu_lock; we never need to deal 444 * with concurrent resizes. Resizes should be rare; they may induce jitter 445 * on the CPU being resized, but should not affect cyclic operation on other 446 * CPUs. Pending cyclics may not be dropped during a resize operation. 447 * 448 * Three key cyc_cpu data structures need to be resized: the cyclics array, 449 * the heap array and the producer/consumer buffers. Resizing the first two 450 * is relatively straightforward: 451 * 452 * 1. The new, larger arrays are allocated in cyclic_expand() (called 453 * from cyclic_add()). 454 * 2. cyclic_expand() cross calls cyclic_expand_xcall() on the CPU 455 * undergoing the resize. 456 * 3. cyclic_expand_xcall() raises interrupt level to CY_HIGH_LEVEL 457 * 4. The contents of the old arrays are copied into the new arrays. 458 * 5. The old cyclics array is bzero()'d 459 * 6. The pointers are updated. 460 * 461 * The producer/consumer buffer is dicier: cyclic_expand_xcall() may have 462 * interrupted cyclic_softint() in the middle of consumption. To resize the 463 * producer/consumer buffer, we implement up to two buffers per soft interrupt 464 * level: a hard buffer (the buffer being produced into by cyclic_expire()) 465 * and a soft buffer (the buffer from which cyclic_softint() is consuming). 466 * During normal operation, the hard buffer and soft buffer point to the 467 * same underlying producer/consumer buffer. 468 * 469 * During a resize, however, cyclic_expand_xcall() changes the hard buffer 470 * to point to the new, larger producer/consumer buffer; all future 471 * cyclic_expire()'s will produce into the new buffer. cyclic_expand_xcall() 472 * then posts a CY_LOCK_LEVEL soft interrupt, landing in cyclic_softint(). 473 * 474 * As under normal operation, cyclic_softint() will consume cyclics from 475 * its soft buffer. After the soft buffer is drained, however, 476 * cyclic_softint() will see that the hard buffer has changed. At that time, 477 * cyclic_softint() will change its soft buffer to point to the hard buffer, 478 * and repeat the producer/consumer buffer draining procedure. 479 * 480 * After the new buffer is drained, cyclic_softint() will determine if both 481 * soft levels have seen their new producer/consumer buffer. If both have, 482 * cyclic_softint() will post on the semaphore cyp_modify_wait. If not, a 483 * soft interrupt will be generated for the remaining level. 484 * 485 * cyclic_expand() blocks on the cyp_modify_wait semaphore (a semaphore is 486 * used instead of a condition variable because of the race between the 487 * sema_p() in cyclic_expand() and the sema_v() in cyclic_softint()). This 488 * allows cyclic_expand() to know when the resize operation is complete; 489 * all of the old buffers (the heap, the cyclics array and the producer/ 490 * consumer buffers) can be freed. 491 * 492 * A final caveat on resizing: we described step (5) in the 493 * cyclic_expand_xcall() procedure without providing any motivation. This 494 * step addresses the problem of a cyclic_softint() attempting to decrement 495 * a cy_pend count while interrupted by a cyclic_expand_xcall(). Because 496 * cyclic_softint() has already called the handler by the time cy_pend is 497 * decremented, we want to assure that it doesn't decrement a cy_pend 498 * count in the old cyclics array. By zeroing the old cyclics array in 499 * cyclic_expand_xcall(), we are zeroing out every cy_pend count; when 500 * cyclic_softint() attempts to compare&swap on the cy_pend count, it will 501 * fail and recognize that the count has been zeroed. cyclic_softint() will 502 * update its stale copy of the cyp_cyclics pointer, re-read the cy_pend 503 * count from the new cyclics array, and re-attempt the compare&swap. 504 * 505 * Removals 506 * 507 * Cyclic removals should be rare. To simplify the implementation (and to 508 * allow optimization for the cyclic_fire()/cyclic_expire()/cyclic_softint() 509 * path), we force removals and adds to serialize on cpu_lock. 510 * 511 * Cyclic removal is complicated by a guarantee made to the consumer of 512 * the cyclic subsystem: after cyclic_remove() returns, the cyclic handler 513 * has returned and will never again be called. 514 * 515 * Here is the procedure for cyclic removal: 516 * 517 * 1. cyclic_remove() calls cyclic_remove_xcall() on the CPU undergoing 518 * the removal. 519 * 2. cyclic_remove_xcall() raises interrupt level to CY_HIGH_LEVEL 520 * 3. The current expiration time for the removed cyclic is recorded. 521 * 4. If the cy_pend count on the removed cyclic is non-zero, it 522 * is copied into cyp_rpend and subsequently zeroed. 523 * 5. The cyclic is removed from the heap 524 * 6. If the root of the heap has changed, the backend is reprogrammed. 525 * 7. If the cy_pend count was non-zero cyclic_remove() blocks on the 526 * cyp_modify_wait semaphore. 527 * 528 * The motivation for step (3) is explained in "Juggling", below. 529 * 530 * The cy_pend count is decremented in cyclic_softint() after the cyclic 531 * handler returns. Thus, if we find a cy_pend count of zero in step 532 * (4), we know that cyclic_remove() doesn't need to block. 533 * 534 * If the cy_pend count is non-zero, however, we must block in cyclic_remove() 535 * until cyclic_softint() has finished calling the cyclic handler. To let 536 * cyclic_softint() know that this cyclic has been removed, we zero the 537 * cy_pend count. This will cause cyclic_softint()'s compare&swap to fail. 538 * When cyclic_softint() sees the zero cy_pend count, it knows that it's been 539 * caught during a resize (see "Resizing", above) or that the cyclic has been 540 * removed. In the latter case, it calls cyclic_remove_pend() to call the 541 * cyclic handler cyp_rpend - 1 times, and posts on cyp_modify_wait. 542 * 543 * Juggling 544 * 545 * At first glance, cyclic juggling seems to be a difficult problem. The 546 * subsystem must guarantee that a cyclic doesn't execute simultaneously on 547 * different CPUs, while also assuring that a cyclic fires exactly once 548 * per interval. We solve this problem by leveraging a property of the 549 * platform: gethrtime() is required to increase in lock-step across 550 * multiple CPUs. Therefore, to juggle a cyclic, we remove it from its 551 * CPU, recording its expiration time in the remove cross call (step (3) 552 * in "Removing", above). We then add the cyclic to the new CPU, explicitly 553 * setting its expiration time to the time recorded in the removal. This 554 * leverages the existing cyclic expiry processing, which will compensate 555 * for any time lost while juggling. 556 * 557 * Reprogramming 558 * 559 * Normally, after a cyclic fires, its next expiration is computed from 560 * the current time and the cyclic interval. But there are situations when 561 * the next expiration needs to be reprogrammed by the kernel subsystem that 562 * is using the cyclic. cyclic_reprogram() allows this to be done. This, 563 * unlike the other kernel at-large cyclic API functions, is permitted to 564 * be called from the cyclic handler. This is because it does not use the 565 * cpu_lock to serialize access. 566 * 567 * When cyclic_reprogram() is called for an omni-cyclic, the operation is 568 * applied to the omni-cyclic's component on the current CPU. 569 * 570 * If a high-level cyclic handler reprograms its own cyclic, then 571 * cyclic_fire() detects that and does not recompute the cyclic's next 572 * expiration. However, for a lock-level or a low-level cyclic, the 573 * actual cyclic handler will execute at the lower PIL only after 574 * cyclic_fire() is done with all expired cyclics. To deal with this, such 575 * cyclics can be specified with a special interval of CY_INFINITY (INT64_MAX). 576 * cyclic_fire() recognizes this special value and recomputes the next 577 * expiration to CY_INFINITY. This effectively moves the cyclic to the 578 * bottom of the heap and prevents it from going off until its handler has 579 * had a chance to reprogram it. Infact, this is the way to create and reuse 580 * "one-shot" timers in the context of the cyclic subsystem without using 581 * cyclic_remove(). 582 * 583 * Here is the procedure for cyclic reprogramming: 584 * 585 * 1. cyclic_reprogram() calls cyclic_reprogram_xcall() on the CPU 586 * that houses the cyclic. 587 * 2. cyclic_reprogram_xcall() raises interrupt level to CY_HIGH_LEVEL 588 * 3. The cyclic is located in the cyclic heap. The search for this is 589 * done from the bottom of the heap to the top as reprogrammable cyclics 590 * would be located closer to the bottom than the top. 591 * 4. The cyclic expiration is set and the cyclic is moved to its 592 * correct position in the heap (up or down depending on whether the 593 * new expiration is less than or greater than the old one). 594 * 5. If the cyclic move modified the root of the heap, the backend is 595 * reprogrammed. 596 * 597 * Reprogramming can be a frequent event (see the callout subsystem). So, 598 * the serialization used has to be efficient. As with all other cyclic 599 * operations, the interrupt level is raised during reprogramming. Plus, 600 * during reprogramming, the cyclic must not be juggled (regular cyclic) 601 * or stopped (omni-cyclic). The implementation defines a per-cyclic 602 * reader-writer lock to accomplish this. This lock is acquired in the 603 * reader mode by cyclic_reprogram() and writer mode by cyclic_juggle() and 604 * cyclic_omni_stop(). The reader-writer lock makes it efficient if 605 * an omni-cyclic is reprogrammed on different CPUs frequently. 606 * 607 * Note that since the cpu_lock is not used during reprogramming, it is 608 * the responsibility of the user of the reprogrammable cyclic to make sure 609 * that the cyclic is not removed via cyclic_remove() during reprogramming. 610 * This is not an unreasonable requirement as the user will typically have 611 * some sort of synchronization for its cyclic-related activities. This 612 * little caveat exists because the cyclic ID is not really an ID. It is 613 * implemented as a pointer to a structure. 614 */ 615 #include <sys/cyclic_impl.h> 616 #include <sys/sysmacros.h> 617 #include <sys/systm.h> 618 #include <sys/atomic.h> 619 #include <sys/kmem.h> 620 #include <sys/cmn_err.h> 621 #include <sys/ddi.h> 622 #include <sys/sdt.h> 623 624 #ifdef CYCLIC_TRACE 625 626 /* 627 * cyc_trace_enabled is for the benefit of kernel debuggers. 628 */ 629 int cyc_trace_enabled = 1; 630 static cyc_tracebuf_t cyc_ptrace; 631 static cyc_coverage_t cyc_coverage[CY_NCOVERAGE]; 632 633 /* 634 * Seen this anywhere? 635 */ 636 static uint_t 637 cyclic_coverage_hash(char *p) 638 { 639 unsigned int g; 640 uint_t hval; 641 642 hval = 0; 643 while (*p) { 644 hval = (hval << 4) + *p++; 645 if ((g = (hval & 0xf0000000)) != 0) 646 hval ^= g >> 24; 647 hval &= ~g; 648 } 649 return (hval); 650 } 651 652 static void 653 cyclic_coverage(char *why, int level, uint64_t arg0, uint64_t arg1) 654 { 655 uint_t ndx, orig; 656 657 for (ndx = orig = cyclic_coverage_hash(why) % CY_NCOVERAGE; ; ) { 658 if (cyc_coverage[ndx].cyv_why == why) 659 break; 660 661 if (cyc_coverage[ndx].cyv_why != NULL || 662 atomic_cas_ptr(&cyc_coverage[ndx].cyv_why, NULL, why) != 663 NULL) { 664 665 if (++ndx == CY_NCOVERAGE) 666 ndx = 0; 667 668 if (ndx == orig) 669 panic("too many cyclic coverage points"); 670 continue; 671 } 672 673 /* 674 * If we're here, we have successfully swung our guy into 675 * the position at "ndx". 676 */ 677 break; 678 } 679 680 if (level == CY_PASSIVE_LEVEL) 681 cyc_coverage[ndx].cyv_passive_count++; 682 else 683 cyc_coverage[ndx].cyv_count[level]++; 684 685 cyc_coverage[ndx].cyv_arg0 = arg0; 686 cyc_coverage[ndx].cyv_arg1 = arg1; 687 } 688 689 #define CYC_TRACE(cpu, level, why, arg0, arg1) \ 690 CYC_TRACE_IMPL(&cpu->cyp_trace[level], level, why, arg0, arg1) 691 692 #define CYC_PTRACE(why, arg0, arg1) \ 693 CYC_TRACE_IMPL(&cyc_ptrace, CY_PASSIVE_LEVEL, why, arg0, arg1) 694 695 #define CYC_TRACE_IMPL(buf, level, why, a0, a1) { \ 696 if (panicstr == NULL) { \ 697 int _ndx = (buf)->cyt_ndx; \ 698 cyc_tracerec_t *_rec = &(buf)->cyt_buf[_ndx]; \ 699 (buf)->cyt_ndx = (++_ndx == CY_NTRACEREC) ? 0 : _ndx; \ 700 _rec->cyt_tstamp = gethrtime_unscaled(); \ 701 _rec->cyt_why = (why); \ 702 _rec->cyt_arg0 = (uint64_t)(uintptr_t)(a0); \ 703 _rec->cyt_arg1 = (uint64_t)(uintptr_t)(a1); \ 704 cyclic_coverage(why, level, \ 705 (uint64_t)(uintptr_t)(a0), (uint64_t)(uintptr_t)(a1)); \ 706 } \ 707 } 708 709 #else 710 711 static int cyc_trace_enabled = 0; 712 713 #define CYC_TRACE(cpu, level, why, arg0, arg1) 714 #define CYC_PTRACE(why, arg0, arg1) 715 716 #endif 717 718 #define CYC_TRACE0(cpu, level, why) CYC_TRACE(cpu, level, why, 0, 0) 719 #define CYC_TRACE1(cpu, level, why, arg0) CYC_TRACE(cpu, level, why, arg0, 0) 720 721 #define CYC_PTRACE0(why) CYC_PTRACE(why, 0, 0) 722 #define CYC_PTRACE1(why, arg0) CYC_PTRACE(why, arg0, 0) 723 724 static kmem_cache_t *cyclic_id_cache; 725 static cyc_id_t *cyclic_id_head; 726 static hrtime_t cyclic_resolution; 727 static cyc_backend_t cyclic_backend; 728 729 /* 730 * Returns 1 if the upheap propagated to the root, 0 if it did not. This 731 * allows the caller to reprogram the backend only when the root has been 732 * modified. 733 */ 734 static int 735 cyclic_upheap(cyc_cpu_t *cpu, cyc_index_t ndx) 736 { 737 cyclic_t *cyclics; 738 cyc_index_t *heap; 739 cyc_index_t heap_parent, heap_current = ndx; 740 cyc_index_t parent, current; 741 742 if (heap_current == 0) 743 return (1); 744 745 heap = cpu->cyp_heap; 746 cyclics = cpu->cyp_cyclics; 747 heap_parent = CYC_HEAP_PARENT(heap_current); 748 749 for (;;) { 750 current = heap[heap_current]; 751 parent = heap[heap_parent]; 752 753 /* 754 * We have an expiration time later than our parent; we're 755 * done. 756 */ 757 if (cyclics[current].cy_expire >= cyclics[parent].cy_expire) 758 return (0); 759 760 /* 761 * We need to swap with our parent, and continue up the heap. 762 */ 763 heap[heap_parent] = current; 764 heap[heap_current] = parent; 765 766 /* 767 * If we just reached the root, we're done. 768 */ 769 if (heap_parent == 0) 770 return (1); 771 772 heap_current = heap_parent; 773 heap_parent = CYC_HEAP_PARENT(heap_current); 774 } 775 } 776 777 static void 778 cyclic_downheap(cyc_cpu_t *cpu, cyc_index_t ndx) 779 { 780 cyclic_t *cyclics = cpu->cyp_cyclics; 781 cyc_index_t *heap = cpu->cyp_heap; 782 783 cyc_index_t heap_left, heap_right, heap_me = ndx; 784 cyc_index_t left, right, me; 785 cyc_index_t nelems = cpu->cyp_nelems; 786 787 for (;;) { 788 /* 789 * If we don't have a left child (i.e., we're a leaf), we're 790 * done. 791 */ 792 if ((heap_left = CYC_HEAP_LEFT(heap_me)) >= nelems) 793 return; 794 795 left = heap[heap_left]; 796 me = heap[heap_me]; 797 798 heap_right = CYC_HEAP_RIGHT(heap_me); 799 800 /* 801 * Even if we don't have a right child, we still need to compare 802 * our expiration time against that of our left child. 803 */ 804 if (heap_right >= nelems) 805 goto comp_left; 806 807 right = heap[heap_right]; 808 809 /* 810 * We have both a left and a right child. We need to compare 811 * the expiration times of the children to determine which 812 * expires earlier. 813 */ 814 if (cyclics[right].cy_expire < cyclics[left].cy_expire) { 815 /* 816 * Our right child is the earlier of our children. 817 * We'll now compare our expiration time to its; if 818 * ours is the earlier, we're done. 819 */ 820 if (cyclics[me].cy_expire <= cyclics[right].cy_expire) 821 return; 822 823 /* 824 * Our right child expires earlier than we do; swap 825 * with our right child, and descend right. 826 */ 827 heap[heap_right] = me; 828 heap[heap_me] = right; 829 heap_me = heap_right; 830 continue; 831 } 832 833 comp_left: 834 /* 835 * Our left child is the earlier of our children (or we have 836 * no right child). We'll now compare our expiration time 837 * to its; if ours is the earlier, we're done. 838 */ 839 if (cyclics[me].cy_expire <= cyclics[left].cy_expire) 840 return; 841 842 /* 843 * Our left child expires earlier than we do; swap with our 844 * left child, and descend left. 845 */ 846 heap[heap_left] = me; 847 heap[heap_me] = left; 848 heap_me = heap_left; 849 } 850 } 851 852 static void 853 cyclic_expire(cyc_cpu_t *cpu, cyc_index_t ndx, cyclic_t *cyclic) 854 { 855 cyc_backend_t *be = cpu->cyp_backend; 856 cyc_level_t level = cyclic->cy_level; 857 858 /* 859 * If this is a CY_HIGH_LEVEL cyclic, just call the handler; we don't 860 * need to worry about the pend count for CY_HIGH_LEVEL cyclics. 861 */ 862 if (level == CY_HIGH_LEVEL) { 863 cyc_func_t handler = cyclic->cy_handler; 864 void *arg = cyclic->cy_arg; 865 866 CYC_TRACE(cpu, CY_HIGH_LEVEL, "handler-in", handler, arg); 867 DTRACE_PROBE1(cyclic__start, cyclic_t *, cyclic); 868 869 (*handler)(arg); 870 871 DTRACE_PROBE1(cyclic__end, cyclic_t *, cyclic); 872 CYC_TRACE(cpu, CY_HIGH_LEVEL, "handler-out", handler, arg); 873 874 return; 875 } 876 877 /* 878 * We're at CY_HIGH_LEVEL; this modification to cy_pend need not 879 * be atomic (the high interrupt level assures that it will appear 880 * atomic to any softint currently running). 881 */ 882 if (cyclic->cy_pend++ == 0) { 883 cyc_softbuf_t *softbuf = &cpu->cyp_softbuf[level]; 884 cyc_pcbuffer_t *pc = &softbuf->cys_buf[softbuf->cys_hard]; 885 886 /* 887 * We need to enqueue this cyclic in the soft buffer. 888 */ 889 CYC_TRACE(cpu, CY_HIGH_LEVEL, "expire-enq", cyclic, 890 pc->cypc_prodndx); 891 pc->cypc_buf[pc->cypc_prodndx++ & pc->cypc_sizemask] = ndx; 892 893 ASSERT(pc->cypc_prodndx != pc->cypc_consndx); 894 } else { 895 /* 896 * If the pend count is zero after we incremented it, then 897 * we've wrapped (i.e. we had a cy_pend count of over four 898 * billion. In this case, we clamp the pend count at 899 * UINT32_MAX. Yes, cyclics can be lost in this case. 900 */ 901 if (cyclic->cy_pend == 0) { 902 CYC_TRACE1(cpu, CY_HIGH_LEVEL, "expire-wrap", cyclic); 903 cyclic->cy_pend = UINT32_MAX; 904 } 905 906 CYC_TRACE(cpu, CY_HIGH_LEVEL, "expire-bump", cyclic, 0); 907 } 908 909 be->cyb_softint(be->cyb_arg, cyclic->cy_level); 910 } 911 912 /* 913 * cyclic_fire(cpu_t *) 914 * 915 * Overview 916 * 917 * cyclic_fire() is the cyclic subsystem's CY_HIGH_LEVEL interrupt handler. 918 * Called by the cyclic backend. 919 * 920 * Arguments and notes 921 * 922 * The only argument is the CPU on which the interrupt is executing; 923 * backends must call into cyclic_fire() on the specified CPU. 924 * 925 * cyclic_fire() may be called spuriously without ill effect. Optimal 926 * backends will call into cyclic_fire() at or shortly after the time 927 * requested via cyb_reprogram(). However, calling cyclic_fire() 928 * arbitrarily late will only manifest latency bubbles; the correctness 929 * of the cyclic subsystem does not rely on the timeliness of the backend. 930 * 931 * cyclic_fire() is wait-free; it will not block or spin. 932 * 933 * Return values 934 * 935 * None. 936 * 937 * Caller's context 938 * 939 * cyclic_fire() must be called from CY_HIGH_LEVEL interrupt context. 940 */ 941 void 942 cyclic_fire(cpu_t *c) 943 { 944 cyc_cpu_t *cpu = c->cpu_cyclic; 945 cyc_backend_t *be = cpu->cyp_backend; 946 cyc_index_t *heap = cpu->cyp_heap; 947 cyclic_t *cyclic, *cyclics = cpu->cyp_cyclics; 948 void *arg = be->cyb_arg; 949 hrtime_t now = gethrtime(); 950 hrtime_t exp; 951 952 CYC_TRACE(cpu, CY_HIGH_LEVEL, "fire", now, 0); 953 954 if (cpu->cyp_nelems == 0) { 955 /* 956 * This is a spurious fire. Count it as such, and blow 957 * out of here. 958 */ 959 CYC_TRACE0(cpu, CY_HIGH_LEVEL, "fire-spurious"); 960 return; 961 } 962 963 for (;;) { 964 cyc_index_t ndx = heap[0]; 965 966 cyclic = &cyclics[ndx]; 967 968 ASSERT(!(cyclic->cy_flags & CYF_FREE)); 969 970 CYC_TRACE(cpu, CY_HIGH_LEVEL, "fire-check", cyclic, 971 cyclic->cy_expire); 972 973 if ((exp = cyclic->cy_expire) > now) 974 break; 975 976 cyclic_expire(cpu, ndx, cyclic); 977 978 /* 979 * If the handler reprogrammed the cyclic, then don't 980 * recompute the expiration. Then, if the interval is 981 * infinity, set the expiration to infinity. This can 982 * be used to create one-shot timers. 983 */ 984 if (exp != cyclic->cy_expire) { 985 /* 986 * If a hi level cyclic reprograms itself, 987 * the heap adjustment and reprogramming of the 988 * clock source have already been done at this 989 * point. So, we can continue. 990 */ 991 continue; 992 } 993 994 if (cyclic->cy_interval == CY_INFINITY) 995 exp = CY_INFINITY; 996 else 997 exp += cyclic->cy_interval; 998 999 /* 1000 * If this cyclic will be set to next expire in the distant 1001 * past, we have one of two situations: 1002 * 1003 * a) This is the first firing of a cyclic which had 1004 * cy_expire set to 0. 1005 * 1006 * b) We are tragically late for a cyclic -- most likely 1007 * due to being in the debugger. 1008 * 1009 * In either case, we set the new expiration time to be the 1010 * the next interval boundary. This assures that the 1011 * expiration time modulo the interval is invariant. 1012 * 1013 * We arbitrarily define "distant" to be one second (one second 1014 * is chosen because it's shorter than any foray to the 1015 * debugger while still being longer than any legitimate 1016 * stretch at CY_HIGH_LEVEL). 1017 */ 1018 1019 if (now - exp > NANOSEC) { 1020 hrtime_t interval = cyclic->cy_interval; 1021 1022 CYC_TRACE(cpu, CY_HIGH_LEVEL, exp == interval ? 1023 "fire-first" : "fire-swing", now, exp); 1024 1025 exp += ((now - exp) / interval + 1) * interval; 1026 } 1027 1028 cyclic->cy_expire = exp; 1029 cyclic_downheap(cpu, 0); 1030 } 1031 1032 /* 1033 * Now we have a cyclic in the root slot which isn't in the past; 1034 * reprogram the interrupt source. 1035 */ 1036 be->cyb_reprogram(arg, exp); 1037 } 1038 1039 static void 1040 cyclic_remove_pend(cyc_cpu_t *cpu, cyc_level_t level, cyclic_t *cyclic) 1041 { 1042 cyc_func_t handler = cyclic->cy_handler; 1043 void *arg = cyclic->cy_arg; 1044 uint32_t i, rpend = cpu->cyp_rpend - 1; 1045 1046 ASSERT(cyclic->cy_flags & CYF_FREE); 1047 ASSERT(cyclic->cy_pend == 0); 1048 ASSERT(cpu->cyp_state == CYS_REMOVING); 1049 ASSERT(cpu->cyp_rpend > 0); 1050 1051 CYC_TRACE(cpu, level, "remove-rpend", cyclic, cpu->cyp_rpend); 1052 1053 /* 1054 * Note that we only call the handler cyp_rpend - 1 times; this is 1055 * to account for the handler call in cyclic_softint(). 1056 */ 1057 for (i = 0; i < rpend; i++) { 1058 CYC_TRACE(cpu, level, "rpend-in", handler, arg); 1059 DTRACE_PROBE1(cyclic__start, cyclic_t *, cyclic); 1060 1061 (*handler)(arg); 1062 1063 DTRACE_PROBE1(cyclic__end, cyclic_t *, cyclic); 1064 CYC_TRACE(cpu, level, "rpend-out", handler, arg); 1065 } 1066 1067 /* 1068 * We can now let the remove operation complete. 1069 */ 1070 sema_v(&cpu->cyp_modify_wait); 1071 } 1072 1073 /* 1074 * cyclic_softint(cpu_t *cpu, cyc_level_t level) 1075 * 1076 * Overview 1077 * 1078 * cyclic_softint() is the cyclic subsystem's CY_LOCK_LEVEL and CY_LOW_LEVEL 1079 * soft interrupt handler. Called by the cyclic backend. 1080 * 1081 * Arguments and notes 1082 * 1083 * The first argument to cyclic_softint() is the CPU on which the interrupt 1084 * is executing; backends must call into cyclic_softint() on the specified 1085 * CPU. The second argument is the level of the soft interrupt; it must 1086 * be one of CY_LOCK_LEVEL or CY_LOW_LEVEL. 1087 * 1088 * cyclic_softint() will call the handlers for cyclics pending at the 1089 * specified level. cyclic_softint() will not return until all pending 1090 * cyclics at the specified level have been dealt with; intervening 1091 * CY_HIGH_LEVEL interrupts which enqueue cyclics at the specified level 1092 * may therefore prolong cyclic_softint(). 1093 * 1094 * cyclic_softint() never disables interrupts, and, if neither a 1095 * cyclic_add() nor a cyclic_remove() is pending on the specified CPU, is 1096 * lock-free. This assures that in the common case, cyclic_softint() 1097 * completes without blocking, and never starves cyclic_fire(). If either 1098 * cyclic_add() or cyclic_remove() is pending, cyclic_softint() may grab 1099 * a dispatcher lock. 1100 * 1101 * While cyclic_softint() is designed for bounded latency, it is obviously 1102 * at the mercy of its cyclic handlers. Because cyclic handlers may block 1103 * arbitrarily, callers of cyclic_softint() should not rely upon 1104 * deterministic completion. 1105 * 1106 * cyclic_softint() may be called spuriously without ill effect. 1107 * 1108 * Return value 1109 * 1110 * None. 1111 * 1112 * Caller's context 1113 * 1114 * The caller must be executing in soft interrupt context at either 1115 * CY_LOCK_LEVEL or CY_LOW_LEVEL. The level passed to cyclic_softint() 1116 * must match the level at which it is executing. On optimal backends, 1117 * the caller will hold no locks. In any case, the caller may not hold 1118 * cpu_lock or any lock acquired by any cyclic handler or held across 1119 * any of cyclic_add(), cyclic_remove(), cyclic_bind() or cyclic_juggle(). 1120 */ 1121 void 1122 cyclic_softint(cpu_t *c, cyc_level_t level) 1123 { 1124 cyc_cpu_t *cpu = c->cpu_cyclic; 1125 cyc_softbuf_t *softbuf; 1126 int soft, *buf, consndx, resized = 0, intr_resized = 0; 1127 cyc_pcbuffer_t *pc; 1128 cyclic_t *cyclics = cpu->cyp_cyclics; 1129 int sizemask; 1130 1131 CYC_TRACE(cpu, level, "softint", cyclics, 0); 1132 1133 ASSERT(level < CY_LOW_LEVEL + CY_SOFT_LEVELS); 1134 1135 softbuf = &cpu->cyp_softbuf[level]; 1136 top: 1137 soft = softbuf->cys_soft; 1138 ASSERT(soft == 0 || soft == 1); 1139 1140 pc = &softbuf->cys_buf[soft]; 1141 buf = pc->cypc_buf; 1142 consndx = pc->cypc_consndx; 1143 sizemask = pc->cypc_sizemask; 1144 1145 CYC_TRACE(cpu, level, "softint-top", cyclics, pc); 1146 1147 while (consndx != pc->cypc_prodndx) { 1148 uint32_t pend, npend, opend; 1149 int consmasked = consndx & sizemask; 1150 cyclic_t *cyclic = &cyclics[buf[consmasked]]; 1151 cyc_func_t handler = cyclic->cy_handler; 1152 void *arg = cyclic->cy_arg; 1153 1154 ASSERT(buf[consmasked] < cpu->cyp_size); 1155 CYC_TRACE(cpu, level, "consuming", consndx, cyclic); 1156 1157 /* 1158 * We have found this cyclic in the pcbuffer. We know that 1159 * one of the following is true: 1160 * 1161 * (a) The pend is non-zero. We need to execute the handler 1162 * at least once. 1163 * 1164 * (b) The pend _was_ non-zero, but it's now zero due to a 1165 * resize. We will call the handler once, see that we 1166 * are in this case, and read the new cyclics buffer 1167 * (and hence the old non-zero pend). 1168 * 1169 * (c) The pend _was_ non-zero, but it's now zero due to a 1170 * removal. We will call the handler once, see that we 1171 * are in this case, and call into cyclic_remove_pend() 1172 * to call the cyclic rpend times. We will take into 1173 * account that we have already called the handler once. 1174 * 1175 * Point is: it's safe to call the handler without first 1176 * checking the pend. 1177 */ 1178 do { 1179 CYC_TRACE(cpu, level, "handler-in", handler, arg); 1180 DTRACE_PROBE1(cyclic__start, cyclic_t *, cyclic); 1181 1182 (*handler)(arg); 1183 1184 DTRACE_PROBE1(cyclic__end, cyclic_t *, cyclic); 1185 CYC_TRACE(cpu, level, "handler-out", handler, arg); 1186 reread: 1187 pend = cyclic->cy_pend; 1188 npend = pend - 1; 1189 1190 if (pend == 0) { 1191 if (cpu->cyp_state == CYS_REMOVING) { 1192 /* 1193 * This cyclic has been removed while 1194 * it had a non-zero pend count (we 1195 * know it was non-zero because we 1196 * found this cyclic in the pcbuffer). 1197 * There must be a non-zero rpend for 1198 * this CPU, and there must be a remove 1199 * operation blocking; we'll call into 1200 * cyclic_remove_pend() to clean this 1201 * up, and break out of the pend loop. 1202 */ 1203 cyclic_remove_pend(cpu, level, cyclic); 1204 break; 1205 } 1206 1207 /* 1208 * We must have had a resize interrupt us. 1209 */ 1210 CYC_TRACE(cpu, level, "resize-int", cyclics, 0); 1211 ASSERT(cpu->cyp_state == CYS_EXPANDING); 1212 ASSERT(cyclics != cpu->cyp_cyclics); 1213 ASSERT(resized == 0); 1214 ASSERT(intr_resized == 0); 1215 intr_resized = 1; 1216 cyclics = cpu->cyp_cyclics; 1217 cyclic = &cyclics[buf[consmasked]]; 1218 ASSERT(cyclic->cy_handler == handler); 1219 ASSERT(cyclic->cy_arg == arg); 1220 goto reread; 1221 } 1222 1223 if ((opend = 1224 atomic_cas_32(&cyclic->cy_pend, pend, npend)) != 1225 pend) { 1226 /* 1227 * Our atomic_cas_32 can fail for one of several 1228 * reasons: 1229 * 1230 * (a) An intervening high level bumped up the 1231 * pend count on this cyclic. In this 1232 * case, we will see a higher pend. 1233 * 1234 * (b) The cyclics array has been yanked out 1235 * from underneath us by a resize 1236 * operation. In this case, pend is 0 and 1237 * cyp_state is CYS_EXPANDING. 1238 * 1239 * (c) The cyclic has been removed by an 1240 * intervening remove-xcall. In this case, 1241 * pend will be 0, the cyp_state will be 1242 * CYS_REMOVING, and the cyclic will be 1243 * marked CYF_FREE. 1244 * 1245 * The assertion below checks that we are 1246 * in one of the above situations. The 1247 * action under all three is to return to 1248 * the top of the loop. 1249 */ 1250 CYC_TRACE(cpu, level, "cas-fail", opend, pend); 1251 ASSERT(opend > pend || (opend == 0 && 1252 ((cyclics != cpu->cyp_cyclics && 1253 cpu->cyp_state == CYS_EXPANDING) || 1254 (cpu->cyp_state == CYS_REMOVING && 1255 (cyclic->cy_flags & CYF_FREE))))); 1256 goto reread; 1257 } 1258 1259 /* 1260 * Okay, so we've managed to successfully decrement 1261 * pend. If we just decremented the pend to 0, we're 1262 * done. 1263 */ 1264 } while (npend > 0); 1265 1266 pc->cypc_consndx = ++consndx; 1267 } 1268 1269 /* 1270 * If the high level handler is no longer writing to the same 1271 * buffer, then we've had a resize. We need to switch our soft 1272 * index, and goto top. 1273 */ 1274 if (soft != softbuf->cys_hard) { 1275 /* 1276 * We can assert that the other buffer has grown by exactly 1277 * one factor of two. 1278 */ 1279 CYC_TRACE(cpu, level, "buffer-grow", 0, 0); 1280 ASSERT(cpu->cyp_state == CYS_EXPANDING); 1281 ASSERT(softbuf->cys_buf[softbuf->cys_hard].cypc_sizemask == 1282 (softbuf->cys_buf[soft].cypc_sizemask << 1) + 1 || 1283 softbuf->cys_buf[soft].cypc_sizemask == 0); 1284 ASSERT(softbuf->cys_hard == (softbuf->cys_soft ^ 1)); 1285 1286 /* 1287 * If our cached cyclics pointer doesn't match cyp_cyclics, 1288 * then we took a resize between our last iteration of the 1289 * pend loop and the check against softbuf->cys_hard. 1290 */ 1291 if (cpu->cyp_cyclics != cyclics) { 1292 CYC_TRACE1(cpu, level, "resize-int-int", consndx); 1293 cyclics = cpu->cyp_cyclics; 1294 } 1295 1296 softbuf->cys_soft = softbuf->cys_hard; 1297 1298 ASSERT(resized == 0); 1299 resized = 1; 1300 goto top; 1301 } 1302 1303 /* 1304 * If we were interrupted by a resize operation, then we must have 1305 * seen the hard index change. 1306 */ 1307 ASSERT(!(intr_resized == 1 && resized == 0)); 1308 1309 if (resized) { 1310 uint32_t lev, nlev; 1311 1312 ASSERT(cpu->cyp_state == CYS_EXPANDING); 1313 1314 do { 1315 lev = cpu->cyp_modify_levels; 1316 nlev = lev + 1; 1317 } while (atomic_cas_32(&cpu->cyp_modify_levels, lev, nlev) != 1318 lev); 1319 1320 /* 1321 * If we are the last soft level to see the modification, 1322 * post on cyp_modify_wait. Otherwise, (if we're not 1323 * already at low level), post down to the next soft level. 1324 */ 1325 if (nlev == CY_SOFT_LEVELS) { 1326 CYC_TRACE0(cpu, level, "resize-kick"); 1327 sema_v(&cpu->cyp_modify_wait); 1328 } else { 1329 ASSERT(nlev < CY_SOFT_LEVELS); 1330 if (level != CY_LOW_LEVEL) { 1331 cyc_backend_t *be = cpu->cyp_backend; 1332 1333 CYC_TRACE0(cpu, level, "resize-post"); 1334 be->cyb_softint(be->cyb_arg, level - 1); 1335 } 1336 } 1337 } 1338 } 1339 1340 static void 1341 cyclic_expand_xcall(cyc_xcallarg_t *arg) 1342 { 1343 cyc_cpu_t *cpu = arg->cyx_cpu; 1344 cyc_backend_t *be = cpu->cyp_backend; 1345 cyb_arg_t bar = be->cyb_arg; 1346 cyc_cookie_t cookie; 1347 cyc_index_t new_size = arg->cyx_size, size = cpu->cyp_size, i; 1348 cyc_index_t *new_heap = arg->cyx_heap; 1349 cyclic_t *cyclics = cpu->cyp_cyclics, *new_cyclics = arg->cyx_cyclics; 1350 1351 ASSERT(cpu->cyp_state == CYS_EXPANDING); 1352 1353 /* 1354 * This is a little dicey. First, we'll raise our interrupt level 1355 * to CY_HIGH_LEVEL. This CPU already has a new heap, cyclic array, 1356 * etc.; we just need to bcopy them across. As for the softint 1357 * buffers, we'll switch the active buffers. The actual softints will 1358 * take care of consuming any pending cyclics in the old buffer. 1359 */ 1360 cookie = be->cyb_set_level(bar, CY_HIGH_LEVEL); 1361 1362 CYC_TRACE(cpu, CY_HIGH_LEVEL, "expand", new_size, 0); 1363 1364 /* 1365 * Assert that the new size is a power of 2. 1366 */ 1367 ASSERT((new_size & new_size - 1) == 0); 1368 ASSERT(new_size == (size << 1)); 1369 ASSERT(cpu->cyp_heap != NULL && cpu->cyp_cyclics != NULL); 1370 1371 bcopy(cpu->cyp_heap, new_heap, sizeof (cyc_index_t) * size); 1372 bcopy(cyclics, new_cyclics, sizeof (cyclic_t) * size); 1373 1374 /* 1375 * Now run through the old cyclics array, setting pend to 0. To 1376 * softints (which are executing at a lower priority level), the 1377 * pends dropping to 0 will appear atomic with the cyp_cyclics 1378 * pointer changing. 1379 */ 1380 for (i = 0; i < size; i++) 1381 cyclics[i].cy_pend = 0; 1382 1383 /* 1384 * Set up the free list, and set all of the new cyclics to be CYF_FREE. 1385 */ 1386 for (i = size; i < new_size; i++) { 1387 new_heap[i] = i; 1388 new_cyclics[i].cy_flags = CYF_FREE; 1389 } 1390 1391 /* 1392 * We can go ahead and plow the value of cyp_heap and cyp_cyclics; 1393 * cyclic_expand() has kept a copy. 1394 */ 1395 cpu->cyp_heap = new_heap; 1396 cpu->cyp_cyclics = new_cyclics; 1397 cpu->cyp_size = new_size; 1398 1399 /* 1400 * We've switched over the heap and the cyclics array. Now we need 1401 * to switch over our active softint buffer pointers. 1402 */ 1403 for (i = CY_LOW_LEVEL; i < CY_LOW_LEVEL + CY_SOFT_LEVELS; i++) { 1404 cyc_softbuf_t *softbuf = &cpu->cyp_softbuf[i]; 1405 uchar_t hard = softbuf->cys_hard; 1406 1407 /* 1408 * Assert that we're not in the middle of a resize operation. 1409 */ 1410 ASSERT(hard == softbuf->cys_soft); 1411 ASSERT(hard == 0 || hard == 1); 1412 ASSERT(softbuf->cys_buf[hard].cypc_buf != NULL); 1413 1414 softbuf->cys_hard = hard ^ 1; 1415 1416 /* 1417 * The caller (cyclic_expand()) is responsible for setting 1418 * up the new producer-consumer buffer; assert that it's 1419 * been done correctly. 1420 */ 1421 ASSERT(softbuf->cys_buf[hard ^ 1].cypc_buf != NULL); 1422 ASSERT(softbuf->cys_buf[hard ^ 1].cypc_prodndx == 0); 1423 ASSERT(softbuf->cys_buf[hard ^ 1].cypc_consndx == 0); 1424 } 1425 1426 /* 1427 * That's all there is to it; now we just need to postdown to 1428 * get the softint chain going. 1429 */ 1430 be->cyb_softint(bar, CY_HIGH_LEVEL - 1); 1431 be->cyb_restore_level(bar, cookie); 1432 } 1433 1434 /* 1435 * cyclic_expand() will cross call onto the CPU to perform the actual 1436 * expand operation. 1437 */ 1438 static void 1439 cyclic_expand(cyc_cpu_t *cpu) 1440 { 1441 cyc_index_t new_size, old_size; 1442 cyc_index_t *new_heap, *old_heap; 1443 cyclic_t *new_cyclics, *old_cyclics; 1444 cyc_xcallarg_t arg; 1445 cyc_backend_t *be = cpu->cyp_backend; 1446 char old_hard; 1447 int i; 1448 1449 ASSERT(MUTEX_HELD(&cpu_lock)); 1450 ASSERT(cpu->cyp_state == CYS_ONLINE); 1451 1452 cpu->cyp_state = CYS_EXPANDING; 1453 1454 old_heap = cpu->cyp_heap; 1455 old_cyclics = cpu->cyp_cyclics; 1456 1457 if ((new_size = ((old_size = cpu->cyp_size) << 1)) == 0) { 1458 new_size = CY_DEFAULT_PERCPU; 1459 ASSERT(old_heap == NULL && old_cyclics == NULL); 1460 } 1461 1462 /* 1463 * Check that the new_size is a power of 2. 1464 */ 1465 ASSERT((new_size - 1 & new_size) == 0); 1466 1467 new_heap = kmem_alloc(sizeof (cyc_index_t) * new_size, KM_SLEEP); 1468 new_cyclics = kmem_zalloc(sizeof (cyclic_t) * new_size, KM_SLEEP); 1469 1470 /* 1471 * We know that no other expansions are in progress (they serialize 1472 * on cpu_lock), so we can safely read the softbuf metadata. 1473 */ 1474 old_hard = cpu->cyp_softbuf[0].cys_hard; 1475 1476 for (i = CY_LOW_LEVEL; i < CY_LOW_LEVEL + CY_SOFT_LEVELS; i++) { 1477 cyc_softbuf_t *softbuf = &cpu->cyp_softbuf[i]; 1478 char hard = softbuf->cys_hard; 1479 cyc_pcbuffer_t *pc = &softbuf->cys_buf[hard ^ 1]; 1480 1481 ASSERT(hard == old_hard); 1482 ASSERT(hard == softbuf->cys_soft); 1483 ASSERT(pc->cypc_buf == NULL); 1484 1485 pc->cypc_buf = 1486 kmem_alloc(sizeof (cyc_index_t) * new_size, KM_SLEEP); 1487 pc->cypc_prodndx = pc->cypc_consndx = 0; 1488 pc->cypc_sizemask = new_size - 1; 1489 } 1490 1491 arg.cyx_cpu = cpu; 1492 arg.cyx_heap = new_heap; 1493 arg.cyx_cyclics = new_cyclics; 1494 arg.cyx_size = new_size; 1495 1496 cpu->cyp_modify_levels = 0; 1497 1498 be->cyb_xcall(be->cyb_arg, cpu->cyp_cpu, 1499 (cyc_func_t)cyclic_expand_xcall, &arg); 1500 1501 /* 1502 * Now block, waiting for the resize operation to complete. 1503 */ 1504 sema_p(&cpu->cyp_modify_wait); 1505 ASSERT(cpu->cyp_modify_levels == CY_SOFT_LEVELS); 1506 1507 /* 1508 * The operation is complete; we can now free the old buffers. 1509 */ 1510 for (i = CY_LOW_LEVEL; i < CY_LOW_LEVEL + CY_SOFT_LEVELS; i++) { 1511 cyc_softbuf_t *softbuf = &cpu->cyp_softbuf[i]; 1512 char hard = softbuf->cys_hard; 1513 cyc_pcbuffer_t *pc = &softbuf->cys_buf[hard ^ 1]; 1514 1515 ASSERT(hard == (old_hard ^ 1)); 1516 ASSERT(hard == softbuf->cys_soft); 1517 1518 if (pc->cypc_buf == NULL) 1519 continue; 1520 1521 ASSERT(pc->cypc_sizemask == ((new_size - 1) >> 1)); 1522 1523 kmem_free(pc->cypc_buf, 1524 sizeof (cyc_index_t) * (pc->cypc_sizemask + 1)); 1525 pc->cypc_buf = NULL; 1526 } 1527 1528 if (old_cyclics != NULL) { 1529 ASSERT(old_heap != NULL); 1530 ASSERT(old_size != 0); 1531 kmem_free(old_cyclics, sizeof (cyclic_t) * old_size); 1532 kmem_free(old_heap, sizeof (cyc_index_t) * old_size); 1533 } 1534 1535 ASSERT(cpu->cyp_state == CYS_EXPANDING); 1536 cpu->cyp_state = CYS_ONLINE; 1537 } 1538 1539 /* 1540 * cyclic_pick_cpu will attempt to pick a CPU according to the constraints 1541 * specified by the partition, bound CPU, and flags. Additionally, 1542 * cyclic_pick_cpu() will not pick the avoid CPU; it will return NULL if 1543 * the avoid CPU is the only CPU which satisfies the constraints. 1544 * 1545 * If CYF_CPU_BOUND is set in flags, the specified CPU must be non-NULL. 1546 * If CYF_PART_BOUND is set in flags, the specified partition must be non-NULL. 1547 * If both CYF_CPU_BOUND and CYF_PART_BOUND are set, the specified CPU must 1548 * be in the specified partition. 1549 */ 1550 static cyc_cpu_t * 1551 cyclic_pick_cpu(cpupart_t *part, cpu_t *bound, cpu_t *avoid, uint16_t flags) 1552 { 1553 cpu_t *c, *start = (part != NULL) ? part->cp_cpulist : CPU; 1554 cpu_t *online = NULL; 1555 uintptr_t offset; 1556 1557 CYC_PTRACE("pick-cpu", part, bound); 1558 1559 ASSERT(!(flags & CYF_CPU_BOUND) || bound != NULL); 1560 ASSERT(!(flags & CYF_PART_BOUND) || part != NULL); 1561 1562 /* 1563 * If we're bound to our CPU, there isn't much choice involved. We 1564 * need to check that the CPU passed as bound is in the cpupart, and 1565 * that the CPU that we're binding to has been configured. 1566 */ 1567 if (flags & CYF_CPU_BOUND) { 1568 CYC_PTRACE("pick-cpu-bound", bound, avoid); 1569 1570 if ((flags & CYF_PART_BOUND) && bound->cpu_part != part) 1571 panic("cyclic_pick_cpu: " 1572 "CPU binding contradicts partition binding"); 1573 1574 if (bound == avoid) 1575 return (NULL); 1576 1577 if (bound->cpu_cyclic == NULL) 1578 panic("cyclic_pick_cpu: " 1579 "attempt to bind to non-configured CPU"); 1580 1581 return (bound->cpu_cyclic); 1582 } 1583 1584 if (flags & CYF_PART_BOUND) { 1585 CYC_PTRACE("pick-part-bound", bound, avoid); 1586 offset = offsetof(cpu_t, cpu_next_part); 1587 } else { 1588 offset = offsetof(cpu_t, cpu_next_onln); 1589 } 1590 1591 c = start; 1592 do { 1593 if (c->cpu_cyclic == NULL) 1594 continue; 1595 1596 if (c->cpu_cyclic->cyp_state == CYS_OFFLINE) 1597 continue; 1598 1599 if (c == avoid) 1600 continue; 1601 1602 if (c->cpu_flags & CPU_ENABLE) 1603 goto found; 1604 1605 if (online == NULL) 1606 online = c; 1607 } while ((c = *(cpu_t **)((uintptr_t)c + offset)) != start); 1608 1609 /* 1610 * If we're here, we're in one of two situations: 1611 * 1612 * (a) We have a partition-bound cyclic, and there is no CPU in 1613 * our partition which is CPU_ENABLE'd. If we saw another 1614 * non-CYS_OFFLINE CPU in our partition, we'll go with it. 1615 * If not, the avoid CPU must be the only non-CYS_OFFLINE 1616 * CPU in the partition; we're forced to return NULL. 1617 * 1618 * (b) We have a partition-unbound cyclic, in which case there 1619 * must only be one CPU CPU_ENABLE'd, and it must be the one 1620 * we're trying to avoid. If cyclic_juggle()/cyclic_offline() 1621 * are called appropriately, this generally shouldn't happen 1622 * (the offline should fail before getting to this code). 1623 * At any rate: we can't avoid the avoid CPU, so we return 1624 * NULL. 1625 */ 1626 if (!(flags & CYF_PART_BOUND)) { 1627 ASSERT(avoid->cpu_flags & CPU_ENABLE); 1628 return (NULL); 1629 } 1630 1631 CYC_PTRACE("pick-no-intr", part, avoid); 1632 1633 if ((c = online) != NULL) 1634 goto found; 1635 1636 CYC_PTRACE("pick-fail", part, avoid); 1637 ASSERT(avoid->cpu_part == start->cpu_part); 1638 return (NULL); 1639 1640 found: 1641 CYC_PTRACE("pick-cpu-found", c, avoid); 1642 ASSERT(c != avoid); 1643 ASSERT(c->cpu_cyclic != NULL); 1644 1645 return (c->cpu_cyclic); 1646 } 1647 1648 static void 1649 cyclic_add_xcall(cyc_xcallarg_t *arg) 1650 { 1651 cyc_cpu_t *cpu = arg->cyx_cpu; 1652 cyc_handler_t *hdlr = arg->cyx_hdlr; 1653 cyc_time_t *when = arg->cyx_when; 1654 cyc_backend_t *be = cpu->cyp_backend; 1655 cyc_index_t ndx, nelems; 1656 cyc_cookie_t cookie; 1657 cyb_arg_t bar = be->cyb_arg; 1658 cyclic_t *cyclic; 1659 1660 ASSERT(cpu->cyp_nelems < cpu->cyp_size); 1661 1662 cookie = be->cyb_set_level(bar, CY_HIGH_LEVEL); 1663 1664 CYC_TRACE(cpu, CY_HIGH_LEVEL, 1665 "add-xcall", when->cyt_when, when->cyt_interval); 1666 1667 nelems = cpu->cyp_nelems++; 1668 1669 if (nelems == 0) { 1670 /* 1671 * If this is the first element, we need to enable the 1672 * backend on this CPU. 1673 */ 1674 CYC_TRACE0(cpu, CY_HIGH_LEVEL, "enabled"); 1675 be->cyb_enable(bar); 1676 } 1677 1678 ndx = cpu->cyp_heap[nelems]; 1679 cyclic = &cpu->cyp_cyclics[ndx]; 1680 1681 ASSERT(cyclic->cy_flags == CYF_FREE); 1682 cyclic->cy_interval = when->cyt_interval; 1683 1684 if (when->cyt_when == 0) { 1685 /* 1686 * If a start time hasn't been explicitly specified, we'll 1687 * start on the next interval boundary. 1688 */ 1689 cyclic->cy_expire = (gethrtime() / cyclic->cy_interval + 1) * 1690 cyclic->cy_interval; 1691 } else { 1692 cyclic->cy_expire = when->cyt_when; 1693 } 1694 1695 cyclic->cy_handler = hdlr->cyh_func; 1696 cyclic->cy_arg = hdlr->cyh_arg; 1697 cyclic->cy_level = hdlr->cyh_level; 1698 cyclic->cy_flags = arg->cyx_flags; 1699 1700 if (cyclic_upheap(cpu, nelems)) { 1701 hrtime_t exp = cyclic->cy_expire; 1702 1703 CYC_TRACE(cpu, CY_HIGH_LEVEL, "add-reprog", cyclic, exp); 1704 1705 /* 1706 * If our upheap propagated to the root, we need to 1707 * reprogram the interrupt source. 1708 */ 1709 be->cyb_reprogram(bar, exp); 1710 } 1711 be->cyb_restore_level(bar, cookie); 1712 1713 arg->cyx_ndx = ndx; 1714 } 1715 1716 static cyc_index_t 1717 cyclic_add_here(cyc_cpu_t *cpu, cyc_handler_t *hdlr, 1718 cyc_time_t *when, uint16_t flags) 1719 { 1720 cyc_backend_t *be = cpu->cyp_backend; 1721 cyb_arg_t bar = be->cyb_arg; 1722 cyc_xcallarg_t arg; 1723 1724 CYC_PTRACE("add-cpu", cpu, hdlr->cyh_func); 1725 ASSERT(MUTEX_HELD(&cpu_lock)); 1726 ASSERT(cpu->cyp_state == CYS_ONLINE); 1727 ASSERT(!(cpu->cyp_cpu->cpu_flags & CPU_OFFLINE)); 1728 ASSERT(when->cyt_when >= 0 && when->cyt_interval > 0); 1729 1730 if (cpu->cyp_nelems == cpu->cyp_size) { 1731 /* 1732 * This is expensive; it will cross call onto the other 1733 * CPU to perform the expansion. 1734 */ 1735 cyclic_expand(cpu); 1736 ASSERT(cpu->cyp_nelems < cpu->cyp_size); 1737 } 1738 1739 /* 1740 * By now, we know that we're going to be able to successfully 1741 * perform the add. Now cross call over to the CPU of interest to 1742 * actually add our cyclic. 1743 */ 1744 arg.cyx_cpu = cpu; 1745 arg.cyx_hdlr = hdlr; 1746 arg.cyx_when = when; 1747 arg.cyx_flags = flags; 1748 1749 be->cyb_xcall(bar, cpu->cyp_cpu, (cyc_func_t)cyclic_add_xcall, &arg); 1750 1751 CYC_PTRACE("add-cpu-done", cpu, arg.cyx_ndx); 1752 1753 return (arg.cyx_ndx); 1754 } 1755 1756 static void 1757 cyclic_remove_xcall(cyc_xcallarg_t *arg) 1758 { 1759 cyc_cpu_t *cpu = arg->cyx_cpu; 1760 cyc_backend_t *be = cpu->cyp_backend; 1761 cyb_arg_t bar = be->cyb_arg; 1762 cyc_cookie_t cookie; 1763 cyc_index_t ndx = arg->cyx_ndx, nelems, i; 1764 cyc_index_t *heap, last; 1765 cyclic_t *cyclic; 1766 #ifdef DEBUG 1767 cyc_index_t root; 1768 #endif 1769 1770 ASSERT(cpu->cyp_state == CYS_REMOVING); 1771 1772 cookie = be->cyb_set_level(bar, CY_HIGH_LEVEL); 1773 1774 CYC_TRACE1(cpu, CY_HIGH_LEVEL, "remove-xcall", ndx); 1775 1776 heap = cpu->cyp_heap; 1777 nelems = cpu->cyp_nelems; 1778 ASSERT(nelems > 0); 1779 cyclic = &cpu->cyp_cyclics[ndx]; 1780 1781 /* 1782 * Grab the current expiration time. If this cyclic is being 1783 * removed as part of a juggling operation, the expiration time 1784 * will be used when the cyclic is added to the new CPU. 1785 */ 1786 if (arg->cyx_when != NULL) { 1787 arg->cyx_when->cyt_when = cyclic->cy_expire; 1788 arg->cyx_when->cyt_interval = cyclic->cy_interval; 1789 } 1790 1791 if (cyclic->cy_pend != 0) { 1792 /* 1793 * The pend is non-zero; this cyclic is currently being 1794 * executed (or will be executed shortly). If the caller 1795 * refuses to wait, we must return (doing nothing). Otherwise, 1796 * we will stash the pend value * in this CPU's rpend, and 1797 * then zero it out. The softint in the pend loop will see 1798 * that we have zeroed out pend, and will call the cyclic 1799 * handler rpend times. The caller will wait until the 1800 * softint has completed calling the cyclic handler. 1801 */ 1802 if (arg->cyx_wait == CY_NOWAIT) { 1803 arg->cyx_wait = CY_WAIT; 1804 goto out; 1805 } 1806 1807 ASSERT(cyclic->cy_level != CY_HIGH_LEVEL); 1808 CYC_TRACE1(cpu, CY_HIGH_LEVEL, "remove-pend", cyclic->cy_pend); 1809 cpu->cyp_rpend = cyclic->cy_pend; 1810 cyclic->cy_pend = 0; 1811 } 1812 1813 /* 1814 * Now set the flags to CYF_FREE. We don't need a membar_enter() 1815 * between zeroing pend and setting the flags because we're at 1816 * CY_HIGH_LEVEL (that is, the zeroing of pend and the setting 1817 * of cy_flags appear atomic to softints). 1818 */ 1819 cyclic->cy_flags = CYF_FREE; 1820 1821 for (i = 0; i < nelems; i++) { 1822 if (heap[i] == ndx) 1823 break; 1824 } 1825 1826 if (i == nelems) 1827 panic("attempt to remove non-existent cyclic"); 1828 1829 cpu->cyp_nelems = --nelems; 1830 1831 if (nelems == 0) { 1832 /* 1833 * If we just removed the last element, then we need to 1834 * disable the backend on this CPU. 1835 */ 1836 CYC_TRACE0(cpu, CY_HIGH_LEVEL, "disabled"); 1837 be->cyb_disable(bar); 1838 } 1839 1840 if (i == nelems) { 1841 /* 1842 * If we just removed the last element of the heap, then 1843 * we don't have to downheap. 1844 */ 1845 CYC_TRACE0(cpu, CY_HIGH_LEVEL, "remove-bottom"); 1846 goto out; 1847 } 1848 1849 #ifdef DEBUG 1850 root = heap[0]; 1851 #endif 1852 1853 /* 1854 * Swap the last element of the heap with the one we want to 1855 * remove, and downheap (this has the implicit effect of putting 1856 * the newly freed element on the free list). 1857 */ 1858 heap[i] = (last = heap[nelems]); 1859 heap[nelems] = ndx; 1860 1861 if (i == 0) { 1862 CYC_TRACE0(cpu, CY_HIGH_LEVEL, "remove-root"); 1863 cyclic_downheap(cpu, 0); 1864 } else { 1865 if (cyclic_upheap(cpu, i) == 0) { 1866 /* 1867 * The upheap didn't propagate to the root; if it 1868 * didn't propagate at all, we need to downheap. 1869 */ 1870 CYC_TRACE0(cpu, CY_HIGH_LEVEL, "remove-no-root"); 1871 if (heap[i] == last) { 1872 CYC_TRACE0(cpu, CY_HIGH_LEVEL, "remove-no-up"); 1873 cyclic_downheap(cpu, i); 1874 } 1875 ASSERT(heap[0] == root); 1876 goto out; 1877 } 1878 } 1879 1880 /* 1881 * We're here because we changed the root; we need to reprogram 1882 * the clock source. 1883 */ 1884 cyclic = &cpu->cyp_cyclics[heap[0]]; 1885 1886 CYC_TRACE0(cpu, CY_HIGH_LEVEL, "remove-reprog"); 1887 1888 ASSERT(nelems != 0); 1889 be->cyb_reprogram(bar, cyclic->cy_expire); 1890 out: 1891 be->cyb_restore_level(bar, cookie); 1892 } 1893 1894 static int 1895 cyclic_remove_here(cyc_cpu_t *cpu, cyc_index_t ndx, cyc_time_t *when, int wait) 1896 { 1897 cyc_backend_t *be = cpu->cyp_backend; 1898 cyc_xcallarg_t arg; 1899 cyclic_t *cyclic = &cpu->cyp_cyclics[ndx]; 1900 cyc_level_t level = cyclic->cy_level; 1901 1902 ASSERT(MUTEX_HELD(&cpu_lock)); 1903 ASSERT(cpu->cyp_rpend == 0); 1904 ASSERT(wait == CY_WAIT || wait == CY_NOWAIT); 1905 1906 arg.cyx_ndx = ndx; 1907 arg.cyx_cpu = cpu; 1908 arg.cyx_when = when; 1909 arg.cyx_wait = wait; 1910 1911 ASSERT(cpu->cyp_state == CYS_ONLINE); 1912 cpu->cyp_state = CYS_REMOVING; 1913 1914 be->cyb_xcall(be->cyb_arg, cpu->cyp_cpu, 1915 (cyc_func_t)cyclic_remove_xcall, &arg); 1916 1917 /* 1918 * If the cyclic we removed wasn't at CY_HIGH_LEVEL, then we need to 1919 * check the cyp_rpend. If it's non-zero, then we need to wait here 1920 * for all pending cyclic handlers to run. 1921 */ 1922 ASSERT(!(level == CY_HIGH_LEVEL && cpu->cyp_rpend != 0)); 1923 ASSERT(!(wait == CY_NOWAIT && cpu->cyp_rpend != 0)); 1924 ASSERT(!(arg.cyx_wait == CY_NOWAIT && cpu->cyp_rpend != 0)); 1925 1926 if (wait != arg.cyx_wait) { 1927 /* 1928 * We are being told that we must wait if we want to 1929 * remove this cyclic; put the CPU back in the CYS_ONLINE 1930 * state and return failure. 1931 */ 1932 ASSERT(wait == CY_NOWAIT && arg.cyx_wait == CY_WAIT); 1933 ASSERT(cpu->cyp_state == CYS_REMOVING); 1934 cpu->cyp_state = CYS_ONLINE; 1935 1936 return (0); 1937 } 1938 1939 if (cpu->cyp_rpend != 0) 1940 sema_p(&cpu->cyp_modify_wait); 1941 1942 ASSERT(cpu->cyp_state == CYS_REMOVING); 1943 1944 cpu->cyp_rpend = 0; 1945 cpu->cyp_state = CYS_ONLINE; 1946 1947 return (1); 1948 } 1949 1950 /* 1951 * If cyclic_reprogram() is called on the same CPU as the cyclic's CPU, then 1952 * it calls this function directly. Else, it invokes this function through 1953 * an X-call to the cyclic's CPU. 1954 */ 1955 static void 1956 cyclic_reprogram_cyclic(cyc_cpu_t *cpu, cyc_index_t ndx, hrtime_t expire) 1957 { 1958 cyc_backend_t *be = cpu->cyp_backend; 1959 cyb_arg_t bar = be->cyb_arg; 1960 cyc_cookie_t cookie; 1961 cyc_index_t nelems, i; 1962 cyc_index_t *heap; 1963 cyclic_t *cyclic; 1964 hrtime_t oexpire; 1965 int reprog; 1966 1967 cookie = be->cyb_set_level(bar, CY_HIGH_LEVEL); 1968 1969 CYC_TRACE1(cpu, CY_HIGH_LEVEL, "reprog-xcall", ndx); 1970 1971 nelems = cpu->cyp_nelems; 1972 ASSERT(nelems > 0); 1973 heap = cpu->cyp_heap; 1974 1975 /* 1976 * Reprogrammed cyclics are typically one-shot ones that get 1977 * set to infinity on every expiration. We shorten the search by 1978 * searching from the bottom of the heap to the top instead of the 1979 * other way around. 1980 */ 1981 for (i = nelems - 1; i >= 0; i--) { 1982 if (heap[i] == ndx) 1983 break; 1984 } 1985 if (i < 0) 1986 panic("attempt to reprogram non-existent cyclic"); 1987 1988 cyclic = &cpu->cyp_cyclics[ndx]; 1989 oexpire = cyclic->cy_expire; 1990 cyclic->cy_expire = expire; 1991 1992 reprog = (i == 0); 1993 if (expire > oexpire) { 1994 CYC_TRACE1(cpu, CY_HIGH_LEVEL, "reprog-down", i); 1995 cyclic_downheap(cpu, i); 1996 } else if (i > 0) { 1997 CYC_TRACE1(cpu, CY_HIGH_LEVEL, "reprog-up", i); 1998 reprog = cyclic_upheap(cpu, i); 1999 } 2000 2001 if (reprog && (cpu->cyp_state != CYS_SUSPENDED)) { 2002 /* 2003 * The root changed. Reprogram the clock source. 2004 */ 2005 CYC_TRACE0(cpu, CY_HIGH_LEVEL, "reprog-root"); 2006 cyclic = &cpu->cyp_cyclics[heap[0]]; 2007 be->cyb_reprogram(bar, cyclic->cy_expire); 2008 } 2009 2010 be->cyb_restore_level(bar, cookie); 2011 } 2012 2013 static void 2014 cyclic_reprogram_xcall(cyc_xcallarg_t *arg) 2015 { 2016 cyclic_reprogram_cyclic(arg->cyx_cpu, arg->cyx_ndx, 2017 arg->cyx_when->cyt_when); 2018 } 2019 2020 static void 2021 cyclic_reprogram_here(cyc_cpu_t *cpu, cyc_index_t ndx, hrtime_t expiration) 2022 { 2023 cyc_backend_t *be = cpu->cyp_backend; 2024 cyc_xcallarg_t arg; 2025 cyc_time_t when; 2026 2027 ASSERT(expiration > 0); 2028 2029 arg.cyx_ndx = ndx; 2030 arg.cyx_cpu = cpu; 2031 arg.cyx_when = &when; 2032 when.cyt_when = expiration; 2033 2034 be->cyb_xcall(be->cyb_arg, cpu->cyp_cpu, 2035 (cyc_func_t)cyclic_reprogram_xcall, &arg); 2036 } 2037 2038 /* 2039 * cyclic_juggle_one_to() should only be called when the source cyclic 2040 * can be juggled and the destination CPU is known to be able to accept 2041 * it. 2042 */ 2043 static void 2044 cyclic_juggle_one_to(cyc_id_t *idp, cyc_cpu_t *dest) 2045 { 2046 cyc_cpu_t *src = idp->cyi_cpu; 2047 cyc_index_t ndx = idp->cyi_ndx; 2048 cyc_time_t when; 2049 cyc_handler_t hdlr; 2050 cyclic_t *cyclic; 2051 uint16_t flags; 2052 hrtime_t delay; 2053 2054 ASSERT(MUTEX_HELD(&cpu_lock)); 2055 ASSERT(src != NULL && idp->cyi_omni_list == NULL); 2056 ASSERT(!(dest->cyp_cpu->cpu_flags & (CPU_QUIESCED | CPU_OFFLINE))); 2057 CYC_PTRACE("juggle-one-to", idp, dest); 2058 2059 cyclic = &src->cyp_cyclics[ndx]; 2060 2061 flags = cyclic->cy_flags; 2062 ASSERT(!(flags & CYF_CPU_BOUND) && !(flags & CYF_FREE)); 2063 2064 hdlr.cyh_func = cyclic->cy_handler; 2065 hdlr.cyh_level = cyclic->cy_level; 2066 hdlr.cyh_arg = cyclic->cy_arg; 2067 2068 /* 2069 * Before we begin the juggling process, see if the destination 2070 * CPU requires an expansion. If it does, we'll perform the 2071 * expansion before removing the cyclic. This is to prevent us 2072 * from blocking while a system-critical cyclic (notably, the clock 2073 * cyclic) isn't on a CPU. 2074 */ 2075 if (dest->cyp_nelems == dest->cyp_size) { 2076 CYC_PTRACE("remove-expand", idp, dest); 2077 cyclic_expand(dest); 2078 ASSERT(dest->cyp_nelems < dest->cyp_size); 2079 } 2080 2081 /* 2082 * Prevent a reprogram of this cyclic while we are relocating it. 2083 * Otherwise, cyclic_reprogram_here() will end up sending an X-call 2084 * to the wrong CPU. 2085 */ 2086 rw_enter(&idp->cyi_lock, RW_WRITER); 2087 2088 /* 2089 * Remove the cyclic from the source. As mentioned above, we cannot 2090 * block during this operation; if we cannot remove the cyclic 2091 * without waiting, we spin for a time shorter than the interval, and 2092 * reattempt the (non-blocking) removal. If we continue to fail, 2093 * we will exponentially back off (up to half of the interval). 2094 * Note that the removal will ultimately succeed -- even if the 2095 * cyclic handler is blocked on a resource held by a thread which we 2096 * have preempted, priority inheritance assures that the preempted 2097 * thread will preempt us and continue to progress. 2098 */ 2099 for (delay = NANOSEC / MICROSEC; ; delay <<= 1) { 2100 /* 2101 * Before we begin this operation, disable kernel preemption. 2102 */ 2103 kpreempt_disable(); 2104 if (cyclic_remove_here(src, ndx, &when, CY_NOWAIT)) 2105 break; 2106 2107 /* 2108 * The operation failed; enable kernel preemption while 2109 * spinning. 2110 */ 2111 kpreempt_enable(); 2112 2113 CYC_PTRACE("remove-retry", idp, src); 2114 2115 if (delay > (cyclic->cy_interval >> 1)) 2116 delay = cyclic->cy_interval >> 1; 2117 2118 /* 2119 * Drop the RW lock to avoid a deadlock with the cyclic 2120 * handler (because it can potentially call cyclic_reprogram(). 2121 */ 2122 rw_exit(&idp->cyi_lock); 2123 drv_usecwait((clock_t)(delay / (NANOSEC / MICROSEC))); 2124 rw_enter(&idp->cyi_lock, RW_WRITER); 2125 } 2126 2127 /* 2128 * Now add the cyclic to the destination. This won't block; we 2129 * performed any necessary (blocking) expansion of the destination 2130 * CPU before removing the cyclic from the source CPU. 2131 */ 2132 idp->cyi_ndx = cyclic_add_here(dest, &hdlr, &when, flags); 2133 idp->cyi_cpu = dest; 2134 kpreempt_enable(); 2135 2136 /* 2137 * Now that we have successfully relocated the cyclic, allow 2138 * it to be reprogrammed. 2139 */ 2140 rw_exit(&idp->cyi_lock); 2141 } 2142 2143 static int 2144 cyclic_juggle_one(cyc_id_t *idp) 2145 { 2146 cyc_index_t ndx = idp->cyi_ndx; 2147 cyc_cpu_t *cpu = idp->cyi_cpu, *dest; 2148 cyclic_t *cyclic = &cpu->cyp_cyclics[ndx]; 2149 cpu_t *c = cpu->cyp_cpu; 2150 cpupart_t *part = c->cpu_part; 2151 2152 CYC_PTRACE("juggle-one", idp, cpu); 2153 ASSERT(MUTEX_HELD(&cpu_lock)); 2154 ASSERT(!(c->cpu_flags & CPU_OFFLINE)); 2155 ASSERT(cpu->cyp_state == CYS_ONLINE); 2156 ASSERT(!(cyclic->cy_flags & CYF_FREE)); 2157 2158 if ((dest = cyclic_pick_cpu(part, c, c, cyclic->cy_flags)) == NULL) { 2159 /* 2160 * Bad news: this cyclic can't be juggled. 2161 */ 2162 CYC_PTRACE("juggle-fail", idp, cpu) 2163 return (0); 2164 } 2165 2166 cyclic_juggle_one_to(idp, dest); 2167 2168 return (1); 2169 } 2170 2171 static void 2172 cyclic_unbind_cpu(cyclic_id_t id) 2173 { 2174 cyc_id_t *idp = (cyc_id_t *)id; 2175 cyc_cpu_t *cpu = idp->cyi_cpu; 2176 cpu_t *c = cpu->cyp_cpu; 2177 cyclic_t *cyclic = &cpu->cyp_cyclics[idp->cyi_ndx]; 2178 2179 CYC_PTRACE("unbind-cpu", id, cpu); 2180 ASSERT(MUTEX_HELD(&cpu_lock)); 2181 ASSERT(cpu->cyp_state == CYS_ONLINE); 2182 ASSERT(!(cyclic->cy_flags & CYF_FREE)); 2183 ASSERT(cyclic->cy_flags & CYF_CPU_BOUND); 2184 2185 cyclic->cy_flags &= ~CYF_CPU_BOUND; 2186 2187 /* 2188 * If we were bound to CPU which has interrupts disabled, we need 2189 * to juggle away. This can only fail if we are bound to a 2190 * processor set, and if every CPU in the processor set has 2191 * interrupts disabled. 2192 */ 2193 if (!(c->cpu_flags & CPU_ENABLE)) { 2194 int res = cyclic_juggle_one(idp); 2195 2196 ASSERT((res && idp->cyi_cpu != cpu) || 2197 (!res && (cyclic->cy_flags & CYF_PART_BOUND))); 2198 } 2199 } 2200 2201 static void 2202 cyclic_bind_cpu(cyclic_id_t id, cpu_t *d) 2203 { 2204 cyc_id_t *idp = (cyc_id_t *)id; 2205 cyc_cpu_t *dest = d->cpu_cyclic, *cpu = idp->cyi_cpu; 2206 cpu_t *c = cpu->cyp_cpu; 2207 cyclic_t *cyclic = &cpu->cyp_cyclics[idp->cyi_ndx]; 2208 cpupart_t *part = c->cpu_part; 2209 2210 CYC_PTRACE("bind-cpu", id, dest); 2211 ASSERT(MUTEX_HELD(&cpu_lock)); 2212 ASSERT(!(d->cpu_flags & CPU_OFFLINE)); 2213 ASSERT(!(c->cpu_flags & CPU_OFFLINE)); 2214 ASSERT(cpu->cyp_state == CYS_ONLINE); 2215 ASSERT(dest != NULL); 2216 ASSERT(dest->cyp_state == CYS_ONLINE); 2217 ASSERT(!(cyclic->cy_flags & CYF_FREE)); 2218 ASSERT(!(cyclic->cy_flags & CYF_CPU_BOUND)); 2219 2220 dest = cyclic_pick_cpu(part, d, NULL, cyclic->cy_flags | CYF_CPU_BOUND); 2221 2222 if (dest != cpu) { 2223 cyclic_juggle_one_to(idp, dest); 2224 cyclic = &dest->cyp_cyclics[idp->cyi_ndx]; 2225 } 2226 2227 cyclic->cy_flags |= CYF_CPU_BOUND; 2228 } 2229 2230 static void 2231 cyclic_unbind_cpupart(cyclic_id_t id) 2232 { 2233 cyc_id_t *idp = (cyc_id_t *)id; 2234 cyc_cpu_t *cpu = idp->cyi_cpu; 2235 cpu_t *c = cpu->cyp_cpu; 2236 cyclic_t *cyc = &cpu->cyp_cyclics[idp->cyi_ndx]; 2237 2238 CYC_PTRACE("unbind-part", idp, c->cpu_part); 2239 ASSERT(MUTEX_HELD(&cpu_lock)); 2240 ASSERT(cpu->cyp_state == CYS_ONLINE); 2241 ASSERT(!(cyc->cy_flags & CYF_FREE)); 2242 ASSERT(cyc->cy_flags & CYF_PART_BOUND); 2243 2244 cyc->cy_flags &= ~CYF_PART_BOUND; 2245 2246 /* 2247 * If we're on a CPU which has interrupts disabled (and if this cyclic 2248 * isn't bound to the CPU), we need to juggle away. 2249 */ 2250 if (!(c->cpu_flags & CPU_ENABLE) && !(cyc->cy_flags & CYF_CPU_BOUND)) { 2251 int res = cyclic_juggle_one(idp); 2252 2253 ASSERT(res && idp->cyi_cpu != cpu); 2254 } 2255 } 2256 2257 static void 2258 cyclic_bind_cpupart(cyclic_id_t id, cpupart_t *part) 2259 { 2260 cyc_id_t *idp = (cyc_id_t *)id; 2261 cyc_cpu_t *cpu = idp->cyi_cpu, *dest; 2262 cpu_t *c = cpu->cyp_cpu; 2263 cyclic_t *cyc = &cpu->cyp_cyclics[idp->cyi_ndx]; 2264 2265 CYC_PTRACE("bind-part", idp, part); 2266 ASSERT(MUTEX_HELD(&cpu_lock)); 2267 ASSERT(!(c->cpu_flags & CPU_OFFLINE)); 2268 ASSERT(cpu->cyp_state == CYS_ONLINE); 2269 ASSERT(!(cyc->cy_flags & CYF_FREE)); 2270 ASSERT(!(cyc->cy_flags & CYF_PART_BOUND)); 2271 ASSERT(part->cp_ncpus > 0); 2272 2273 dest = cyclic_pick_cpu(part, c, NULL, cyc->cy_flags | CYF_PART_BOUND); 2274 2275 if (dest != cpu) { 2276 cyclic_juggle_one_to(idp, dest); 2277 cyc = &dest->cyp_cyclics[idp->cyi_ndx]; 2278 } 2279 2280 cyc->cy_flags |= CYF_PART_BOUND; 2281 } 2282 2283 static void 2284 cyclic_configure(cpu_t *c) 2285 { 2286 cyc_cpu_t *cpu = kmem_zalloc(sizeof (cyc_cpu_t), KM_SLEEP); 2287 cyc_backend_t *nbe = kmem_zalloc(sizeof (cyc_backend_t), KM_SLEEP); 2288 int i; 2289 2290 CYC_PTRACE1("configure", cpu); 2291 ASSERT(MUTEX_HELD(&cpu_lock)); 2292 2293 if (cyclic_id_cache == NULL) 2294 cyclic_id_cache = kmem_cache_create("cyclic_id_cache", 2295 sizeof (cyc_id_t), 0, NULL, NULL, NULL, NULL, NULL, 0); 2296 2297 cpu->cyp_cpu = c; 2298 2299 sema_init(&cpu->cyp_modify_wait, 0, NULL, SEMA_DEFAULT, NULL); 2300 2301 cpu->cyp_size = 1; 2302 cpu->cyp_heap = kmem_zalloc(sizeof (cyc_index_t), KM_SLEEP); 2303 cpu->cyp_cyclics = kmem_zalloc(sizeof (cyclic_t), KM_SLEEP); 2304 cpu->cyp_cyclics->cy_flags = CYF_FREE; 2305 2306 for (i = CY_LOW_LEVEL; i < CY_LOW_LEVEL + CY_SOFT_LEVELS; i++) { 2307 /* 2308 * We don't need to set the sizemask; it's already zero 2309 * (which is the appropriate sizemask for a size of 1). 2310 */ 2311 cpu->cyp_softbuf[i].cys_buf[0].cypc_buf = 2312 kmem_alloc(sizeof (cyc_index_t), KM_SLEEP); 2313 } 2314 2315 cpu->cyp_state = CYS_OFFLINE; 2316 2317 /* 2318 * Setup the backend for this CPU. 2319 */ 2320 bcopy(&cyclic_backend, nbe, sizeof (cyc_backend_t)); 2321 nbe->cyb_arg = nbe->cyb_configure(c); 2322 cpu->cyp_backend = nbe; 2323 2324 /* 2325 * On platforms where stray interrupts may be taken during startup, 2326 * the CPU's cpu_cyclic pointer serves as an indicator that the 2327 * cyclic subsystem for this CPU is prepared to field interrupts. 2328 */ 2329 membar_producer(); 2330 2331 c->cpu_cyclic = cpu; 2332 } 2333 2334 static void 2335 cyclic_unconfigure(cpu_t *c) 2336 { 2337 cyc_cpu_t *cpu = c->cpu_cyclic; 2338 cyc_backend_t *be = cpu->cyp_backend; 2339 cyb_arg_t bar = be->cyb_arg; 2340 int i; 2341 2342 CYC_PTRACE1("unconfigure", cpu); 2343 ASSERT(MUTEX_HELD(&cpu_lock)); 2344 ASSERT(cpu->cyp_state == CYS_OFFLINE); 2345 ASSERT(cpu->cyp_nelems == 0); 2346 2347 /* 2348 * Let the backend know that the CPU is being yanked, and free up 2349 * the backend structure. 2350 */ 2351 be->cyb_unconfigure(bar); 2352 kmem_free(be, sizeof (cyc_backend_t)); 2353 cpu->cyp_backend = NULL; 2354 2355 /* 2356 * Free up the producer/consumer buffers at each of the soft levels. 2357 */ 2358 for (i = CY_LOW_LEVEL; i < CY_LOW_LEVEL + CY_SOFT_LEVELS; i++) { 2359 cyc_softbuf_t *softbuf = &cpu->cyp_softbuf[i]; 2360 uchar_t hard = softbuf->cys_hard; 2361 cyc_pcbuffer_t *pc = &softbuf->cys_buf[hard]; 2362 size_t bufsize = sizeof (cyc_index_t) * (pc->cypc_sizemask + 1); 2363 2364 /* 2365 * Assert that we're not in the middle of a resize operation. 2366 */ 2367 ASSERT(hard == softbuf->cys_soft); 2368 ASSERT(hard == 0 || hard == 1); 2369 ASSERT(pc->cypc_buf != NULL); 2370 ASSERT(softbuf->cys_buf[hard ^ 1].cypc_buf == NULL); 2371 2372 kmem_free(pc->cypc_buf, bufsize); 2373 pc->cypc_buf = NULL; 2374 } 2375 2376 /* 2377 * Finally, clean up our remaining dynamic structures and NULL out 2378 * the cpu_cyclic pointer. 2379 */ 2380 kmem_free(cpu->cyp_cyclics, cpu->cyp_size * sizeof (cyclic_t)); 2381 kmem_free(cpu->cyp_heap, cpu->cyp_size * sizeof (cyc_index_t)); 2382 kmem_free(cpu, sizeof (cyc_cpu_t)); 2383 2384 c->cpu_cyclic = NULL; 2385 } 2386 2387 static int 2388 cyclic_cpu_setup(cpu_setup_t what, int id, void *arg __unused) 2389 { 2390 /* 2391 * We are guaranteed that there is still/already an entry in the 2392 * cpu array for this CPU. 2393 */ 2394 cpu_t *c = cpu[id]; 2395 cyc_cpu_t *cyp = c->cpu_cyclic; 2396 2397 ASSERT(MUTEX_HELD(&cpu_lock)); 2398 2399 switch (what) { 2400 case CPU_CONFIG: 2401 ASSERT(cyp == NULL); 2402 cyclic_configure(c); 2403 break; 2404 2405 case CPU_UNCONFIG: 2406 ASSERT(cyp != NULL && cyp->cyp_state == CYS_OFFLINE); 2407 cyclic_unconfigure(c); 2408 break; 2409 2410 default: 2411 break; 2412 } 2413 2414 return (0); 2415 } 2416 2417 static void 2418 cyclic_suspend_xcall(cyc_xcallarg_t *arg) 2419 { 2420 cyc_cpu_t *cpu = arg->cyx_cpu; 2421 cyc_backend_t *be = cpu->cyp_backend; 2422 cyc_cookie_t cookie; 2423 cyb_arg_t bar = be->cyb_arg; 2424 2425 cookie = be->cyb_set_level(bar, CY_HIGH_LEVEL); 2426 2427 CYC_TRACE1(cpu, CY_HIGH_LEVEL, "suspend-xcall", cpu->cyp_nelems); 2428 ASSERT(cpu->cyp_state == CYS_ONLINE || cpu->cyp_state == CYS_OFFLINE); 2429 2430 /* 2431 * We won't disable this CPU unless it has a non-zero number of 2432 * elements (cpu_lock assures that no one else may be attempting 2433 * to disable this CPU). 2434 */ 2435 if (cpu->cyp_nelems > 0) { 2436 ASSERT(cpu->cyp_state == CYS_ONLINE); 2437 be->cyb_disable(bar); 2438 } 2439 2440 if (cpu->cyp_state == CYS_ONLINE) 2441 cpu->cyp_state = CYS_SUSPENDED; 2442 2443 be->cyb_suspend(bar); 2444 be->cyb_restore_level(bar, cookie); 2445 } 2446 2447 static void 2448 cyclic_resume_xcall(cyc_xcallarg_t *arg) 2449 { 2450 cyc_cpu_t *cpu = arg->cyx_cpu; 2451 cyc_backend_t *be = cpu->cyp_backend; 2452 cyc_cookie_t cookie; 2453 cyb_arg_t bar = be->cyb_arg; 2454 cyc_state_t state = cpu->cyp_state; 2455 2456 cookie = be->cyb_set_level(bar, CY_HIGH_LEVEL); 2457 2458 CYC_TRACE1(cpu, CY_HIGH_LEVEL, "resume-xcall", cpu->cyp_nelems); 2459 ASSERT(state == CYS_SUSPENDED || state == CYS_OFFLINE); 2460 2461 be->cyb_resume(bar); 2462 2463 /* 2464 * We won't enable this CPU unless it has a non-zero number of 2465 * elements. 2466 */ 2467 if (cpu->cyp_nelems > 0) { 2468 cyclic_t *cyclic = &cpu->cyp_cyclics[cpu->cyp_heap[0]]; 2469 hrtime_t exp = cyclic->cy_expire; 2470 2471 CYC_TRACE(cpu, CY_HIGH_LEVEL, "resume-reprog", cyclic, exp); 2472 ASSERT(state == CYS_SUSPENDED); 2473 be->cyb_enable(bar); 2474 be->cyb_reprogram(bar, exp); 2475 } 2476 2477 if (state == CYS_SUSPENDED) 2478 cpu->cyp_state = CYS_ONLINE; 2479 2480 CYC_TRACE1(cpu, CY_HIGH_LEVEL, "resume-done", cpu->cyp_nelems); 2481 be->cyb_restore_level(bar, cookie); 2482 } 2483 2484 static void 2485 cyclic_omni_start(cyc_id_t *idp, cyc_cpu_t *cpu) 2486 { 2487 cyc_omni_handler_t *omni = &idp->cyi_omni_hdlr; 2488 cyc_omni_cpu_t *ocpu = kmem_alloc(sizeof (cyc_omni_cpu_t), KM_SLEEP); 2489 cyc_handler_t hdlr; 2490 cyc_time_t when; 2491 2492 CYC_PTRACE("omni-start", cpu, idp); 2493 ASSERT(MUTEX_HELD(&cpu_lock)); 2494 ASSERT(cpu->cyp_state == CYS_ONLINE); 2495 ASSERT(idp->cyi_cpu == NULL); 2496 2497 hdlr.cyh_func = NULL; 2498 hdlr.cyh_arg = NULL; 2499 hdlr.cyh_level = CY_LEVELS; 2500 2501 when.cyt_when = 0; 2502 when.cyt_interval = 0; 2503 2504 omni->cyo_online(omni->cyo_arg, cpu->cyp_cpu, &hdlr, &when); 2505 2506 ASSERT(hdlr.cyh_func != NULL); 2507 ASSERT(hdlr.cyh_level < CY_LEVELS); 2508 ASSERT(when.cyt_when >= 0 && when.cyt_interval > 0); 2509 2510 ocpu->cyo_cpu = cpu; 2511 ocpu->cyo_arg = hdlr.cyh_arg; 2512 ocpu->cyo_ndx = cyclic_add_here(cpu, &hdlr, &when, 0); 2513 ocpu->cyo_next = idp->cyi_omni_list; 2514 idp->cyi_omni_list = ocpu; 2515 } 2516 2517 static void 2518 cyclic_omni_stop(cyc_id_t *idp, cyc_cpu_t *cpu) 2519 { 2520 cyc_omni_handler_t *omni = &idp->cyi_omni_hdlr; 2521 cyc_omni_cpu_t *ocpu = idp->cyi_omni_list, *prev = NULL; 2522 clock_t delay; 2523 int ret; 2524 2525 CYC_PTRACE("omni-stop", cpu, idp); 2526 ASSERT(MUTEX_HELD(&cpu_lock)); 2527 ASSERT(cpu->cyp_state == CYS_ONLINE); 2528 ASSERT(idp->cyi_cpu == NULL); 2529 ASSERT(ocpu != NULL); 2530 2531 /* 2532 * Prevent a reprogram of this cyclic while we are removing it. 2533 * Otherwise, cyclic_reprogram_here() will end up sending an X-call 2534 * to the offlined CPU. 2535 */ 2536 rw_enter(&idp->cyi_lock, RW_WRITER); 2537 2538 while (ocpu != NULL && ocpu->cyo_cpu != cpu) { 2539 prev = ocpu; 2540 ocpu = ocpu->cyo_next; 2541 } 2542 2543 /* 2544 * We _must_ have found an cyc_omni_cpu which corresponds to this 2545 * CPU -- the definition of an omnipresent cyclic is that it runs 2546 * on all online CPUs. 2547 */ 2548 ASSERT(ocpu != NULL); 2549 2550 if (prev == NULL) { 2551 idp->cyi_omni_list = ocpu->cyo_next; 2552 } else { 2553 prev->cyo_next = ocpu->cyo_next; 2554 } 2555 2556 /* 2557 * Remove the cyclic from the source. We cannot block during this 2558 * operation because we are holding the cyi_lock which can be held 2559 * by the cyclic handler via cyclic_reprogram(). 2560 * 2561 * If we cannot remove the cyclic without waiting, we spin for a time, 2562 * and reattempt the (non-blocking) removal. If the handler is blocked 2563 * on the cyi_lock, then we let go of it in the spin loop to give 2564 * the handler a chance to run. Note that the removal will ultimately 2565 * succeed -- even if the cyclic handler is blocked on a resource 2566 * held by a thread which we have preempted, priority inheritance 2567 * assures that the preempted thread will preempt us and continue 2568 * to progress. 2569 */ 2570 for (delay = 1; ; delay <<= 1) { 2571 /* 2572 * Before we begin this operation, disable kernel preemption. 2573 */ 2574 kpreempt_disable(); 2575 ret = cyclic_remove_here(ocpu->cyo_cpu, ocpu->cyo_ndx, NULL, 2576 CY_NOWAIT); 2577 /* 2578 * Enable kernel preemption while spinning. 2579 */ 2580 kpreempt_enable(); 2581 2582 if (ret) 2583 break; 2584 2585 CYC_PTRACE("remove-omni-retry", idp, ocpu->cyo_cpu); 2586 2587 /* 2588 * Drop the RW lock to avoid a deadlock with the cyclic 2589 * handler (because it can potentially call cyclic_reprogram(). 2590 */ 2591 rw_exit(&idp->cyi_lock); 2592 drv_usecwait(delay); 2593 rw_enter(&idp->cyi_lock, RW_WRITER); 2594 } 2595 2596 /* 2597 * Now that we have successfully removed the cyclic, allow the omni 2598 * cyclic to be reprogrammed on other CPUs. 2599 */ 2600 rw_exit(&idp->cyi_lock); 2601 2602 /* 2603 * The cyclic has been removed from this CPU; time to call the 2604 * omnipresent offline handler. 2605 */ 2606 if (omni->cyo_offline != NULL) 2607 omni->cyo_offline(omni->cyo_arg, cpu->cyp_cpu, ocpu->cyo_arg); 2608 2609 kmem_free(ocpu, sizeof (cyc_omni_cpu_t)); 2610 } 2611 2612 static cyc_id_t * 2613 cyclic_new_id() 2614 { 2615 cyc_id_t *idp; 2616 2617 ASSERT(MUTEX_HELD(&cpu_lock)); 2618 2619 idp = kmem_cache_alloc(cyclic_id_cache, KM_SLEEP); 2620 2621 /* 2622 * The cyi_cpu field of the cyc_id_t structure tracks the CPU 2623 * associated with the cyclic. If and only if this field is NULL, the 2624 * cyc_id_t is an omnipresent cyclic. Note that cyi_omni_list may be 2625 * NULL for an omnipresent cyclic while the cyclic is being created 2626 * or destroyed. 2627 */ 2628 idp->cyi_cpu = NULL; 2629 idp->cyi_ndx = 0; 2630 rw_init(&idp->cyi_lock, NULL, RW_DEFAULT, NULL); 2631 2632 idp->cyi_next = cyclic_id_head; 2633 idp->cyi_prev = NULL; 2634 idp->cyi_omni_list = NULL; 2635 2636 if (cyclic_id_head != NULL) { 2637 ASSERT(cyclic_id_head->cyi_prev == NULL); 2638 cyclic_id_head->cyi_prev = idp; 2639 } 2640 2641 cyclic_id_head = idp; 2642 2643 return (idp); 2644 } 2645 2646 /* 2647 * cyclic_id_t cyclic_add(cyc_handler_t *, cyc_time_t *) 2648 * 2649 * Overview 2650 * 2651 * cyclic_add() will create an unbound cyclic with the specified handler and 2652 * interval. The cyclic will run on a CPU which both has interrupts enabled 2653 * and is in the system CPU partition. 2654 * 2655 * Arguments and notes 2656 * 2657 * As its first argument, cyclic_add() takes a cyc_handler, which has the 2658 * following members: 2659 * 2660 * cyc_func_t cyh_func <-- Cyclic handler 2661 * void *cyh_arg <-- Argument to cyclic handler 2662 * cyc_level_t cyh_level <-- Level at which to fire; must be one of 2663 * CY_LOW_LEVEL, CY_LOCK_LEVEL or CY_HIGH_LEVEL 2664 * 2665 * Note that cyh_level is _not_ an ipl or spl; it must be one the 2666 * CY_*_LEVELs. This layer of abstraction allows the platform to define 2667 * the precise interrupt priority levels, within the following constraints: 2668 * 2669 * CY_LOCK_LEVEL must map to LOCK_LEVEL 2670 * CY_HIGH_LEVEL must map to an ipl greater than LOCK_LEVEL 2671 * CY_LOW_LEVEL must map to an ipl below LOCK_LEVEL 2672 * 2673 * In addition to a cyc_handler, cyclic_add() takes a cyc_time, which 2674 * has the following members: 2675 * 2676 * hrtime_t cyt_when <-- Absolute time, in nanoseconds since boot, at 2677 * which to start firing 2678 * hrtime_t cyt_interval <-- Length of interval, in nanoseconds 2679 * 2680 * gethrtime() is the time source for nanoseconds since boot. If cyt_when 2681 * is set to 0, the cyclic will start to fire when cyt_interval next 2682 * divides the number of nanoseconds since boot. 2683 * 2684 * The cyt_interval field _must_ be filled in by the caller; one-shots are 2685 * _not_ explicitly supported by the cyclic subsystem (cyclic_add() will 2686 * assert that cyt_interval is non-zero). The maximum value for either 2687 * field is INT64_MAX; the caller is responsible for assuring that 2688 * cyt_when + cyt_interval <= INT64_MAX. Neither field may be negative. 2689 * 2690 * For an arbitrary time t in the future, the cyclic handler is guaranteed 2691 * to have been called (t - cyt_when) / cyt_interval times. This will 2692 * be true even if interrupts have been disabled for periods greater than 2693 * cyt_interval nanoseconds. In order to compensate for such periods, 2694 * the cyclic handler may be called a finite number of times with an 2695 * arbitrarily small interval. 2696 * 2697 * The cyclic subsystem will not enforce any lower bound on the interval; 2698 * if the interval is less than the time required to process an interrupt, 2699 * the CPU will wedge. It's the responsibility of the caller to assure that 2700 * either the value of the interval is sane, or that its caller has 2701 * sufficient privilege to deny service (i.e. its caller is root). 2702 * 2703 * The cyclic handler is guaranteed to be single threaded, even while the 2704 * cyclic is being juggled between CPUs (see cyclic_juggle(), below). 2705 * That is, a given cyclic handler will never be executed simultaneously 2706 * on different CPUs. 2707 * 2708 * Return value 2709 * 2710 * cyclic_add() returns a cyclic_id_t, which is guaranteed to be a value 2711 * other than CYCLIC_NONE. cyclic_add() cannot fail. 2712 * 2713 * Caller's context 2714 * 2715 * cpu_lock must be held by the caller, and the caller must not be in 2716 * interrupt context. cyclic_add() will perform a KM_SLEEP kernel 2717 * memory allocation, so the usual rules (e.g. p_lock cannot be held) 2718 * apply. A cyclic may be added even in the presence of CPUs that have 2719 * not been configured with respect to the cyclic subsystem, but only 2720 * configured CPUs will be eligible to run the new cyclic. 2721 * 2722 * Cyclic handler's context 2723 * 2724 * Cyclic handlers will be executed in the interrupt context corresponding 2725 * to the specified level (i.e. either high, lock or low level). The 2726 * usual context rules apply. 2727 * 2728 * A cyclic handler may not grab ANY locks held by the caller of any of 2729 * cyclic_add(), cyclic_remove() or cyclic_bind(); the implementation of 2730 * these functions may require blocking on cyclic handler completion. 2731 * Moreover, cyclic handlers may not make any call back into the cyclic 2732 * subsystem. 2733 */ 2734 cyclic_id_t 2735 cyclic_add(cyc_handler_t *hdlr, cyc_time_t *when) 2736 { 2737 cyc_id_t *idp = cyclic_new_id(); 2738 2739 ASSERT(MUTEX_HELD(&cpu_lock)); 2740 ASSERT(when->cyt_when >= 0 && when->cyt_interval > 0); 2741 2742 idp->cyi_cpu = cyclic_pick_cpu(NULL, NULL, NULL, 0); 2743 idp->cyi_ndx = cyclic_add_here(idp->cyi_cpu, hdlr, when, 0); 2744 2745 return ((uintptr_t)idp); 2746 } 2747 2748 /* 2749 * cyclic_id_t cyclic_add_omni(cyc_omni_handler_t *) 2750 * 2751 * Overview 2752 * 2753 * cyclic_add_omni() will create an omnipresent cyclic with the specified 2754 * online and offline handlers. Omnipresent cyclics run on all online 2755 * CPUs, including CPUs which have unbound interrupts disabled. 2756 * 2757 * Arguments 2758 * 2759 * As its only argument, cyclic_add_omni() takes a cyc_omni_handler, which 2760 * has the following members: 2761 * 2762 * void (*cyo_online)() <-- Online handler 2763 * void (*cyo_offline)() <-- Offline handler 2764 * void *cyo_arg <-- Argument to be passed to on/offline handlers 2765 * 2766 * Online handler 2767 * 2768 * The cyo_online member is a pointer to a function which has the following 2769 * four arguments: 2770 * 2771 * void * <-- Argument (cyo_arg) 2772 * cpu_t * <-- Pointer to CPU about to be onlined 2773 * cyc_handler_t * <-- Pointer to cyc_handler_t; must be filled in 2774 * by omni online handler 2775 * cyc_time_t * <-- Pointer to cyc_time_t; must be filled in by 2776 * omni online handler 2777 * 2778 * The omni cyclic online handler is always called _before_ the omni 2779 * cyclic begins to fire on the specified CPU. As the above argument 2780 * description implies, the online handler must fill in the two structures 2781 * passed to it: the cyc_handler_t and the cyc_time_t. These are the 2782 * same two structures passed to cyclic_add(), outlined above. This 2783 * allows the omni cyclic to have maximum flexibility; different CPUs may 2784 * optionally 2785 * 2786 * (a) have different intervals 2787 * (b) be explicitly in or out of phase with one another 2788 * (c) have different handlers 2789 * (d) have different handler arguments 2790 * (e) fire at different levels 2791 * 2792 * Of these, (e) seems somewhat dubious, but is nonetheless allowed. 2793 * 2794 * The omni online handler is called in the same context as cyclic_add(), 2795 * and has the same liberties: omni online handlers may perform KM_SLEEP 2796 * kernel memory allocations, and may grab locks which are also acquired 2797 * by cyclic handlers. However, omni cyclic online handlers may _not_ 2798 * call back into the cyclic subsystem, and should be generally careful 2799 * about calling into arbitrary kernel subsystems. 2800 * 2801 * Offline handler 2802 * 2803 * The cyo_offline member is a pointer to a function which has the following 2804 * three arguments: 2805 * 2806 * void * <-- Argument (cyo_arg) 2807 * cpu_t * <-- Pointer to CPU about to be offlined 2808 * void * <-- CPU's cyclic argument (that is, value 2809 * to which cyh_arg member of the cyc_handler_t 2810 * was set in the omni online handler) 2811 * 2812 * The omni cyclic offline handler is always called _after_ the omni 2813 * cyclic has ceased firing on the specified CPU. Its purpose is to 2814 * allow cleanup of any resources dynamically allocated in the omni cyclic 2815 * online handler. The context of the offline handler is identical to 2816 * that of the online handler; the same constraints and liberties apply. 2817 * 2818 * The offline handler is optional; it may be NULL. 2819 * 2820 * Return value 2821 * 2822 * cyclic_add_omni() returns a cyclic_id_t, which is guaranteed to be a 2823 * value other than CYCLIC_NONE. cyclic_add_omni() cannot fail. 2824 * 2825 * Caller's context 2826 * 2827 * The caller's context is identical to that of cyclic_add(), specified 2828 * above. 2829 */ 2830 cyclic_id_t 2831 cyclic_add_omni(cyc_omni_handler_t *omni) 2832 { 2833 cyc_id_t *idp = cyclic_new_id(); 2834 cyc_cpu_t *cpu; 2835 cpu_t *c; 2836 2837 ASSERT(MUTEX_HELD(&cpu_lock)); 2838 ASSERT(omni != NULL && omni->cyo_online != NULL); 2839 2840 idp->cyi_omni_hdlr = *omni; 2841 2842 c = cpu_list; 2843 do { 2844 if ((cpu = c->cpu_cyclic) == NULL) 2845 continue; 2846 2847 if (cpu->cyp_state != CYS_ONLINE) { 2848 ASSERT(cpu->cyp_state == CYS_OFFLINE); 2849 continue; 2850 } 2851 2852 cyclic_omni_start(idp, cpu); 2853 } while ((c = c->cpu_next) != cpu_list); 2854 2855 /* 2856 * We must have found at least one online CPU on which to run 2857 * this cyclic. 2858 */ 2859 ASSERT(idp->cyi_omni_list != NULL); 2860 ASSERT(idp->cyi_cpu == NULL); 2861 2862 return ((uintptr_t)idp); 2863 } 2864 2865 /* 2866 * void cyclic_remove(cyclic_id_t) 2867 * 2868 * Overview 2869 * 2870 * cyclic_remove() will remove the specified cyclic from the system. 2871 * 2872 * Arguments and notes 2873 * 2874 * The only argument is a cyclic_id returned from either cyclic_add() or 2875 * cyclic_add_omni(). 2876 * 2877 * By the time cyclic_remove() returns, the caller is guaranteed that the 2878 * removed cyclic handler has completed execution (this is the same 2879 * semantic that untimeout() provides). As a result, cyclic_remove() may 2880 * need to block, waiting for the removed cyclic to complete execution. 2881 * This leads to an important constraint on the caller: no lock may be 2882 * held across cyclic_remove() that also may be acquired by a cyclic 2883 * handler. 2884 * 2885 * Return value 2886 * 2887 * None; cyclic_remove() always succeeds. 2888 * 2889 * Caller's context 2890 * 2891 * cpu_lock must be held by the caller, and the caller must not be in 2892 * interrupt context. The caller may not hold any locks which are also 2893 * grabbed by any cyclic handler. See "Arguments and notes", above. 2894 */ 2895 void 2896 cyclic_remove(cyclic_id_t id) 2897 { 2898 cyc_id_t *idp = (cyc_id_t *)id; 2899 cyc_id_t *prev = idp->cyi_prev, *next = idp->cyi_next; 2900 cyc_cpu_t *cpu = idp->cyi_cpu; 2901 2902 CYC_PTRACE("remove", idp, idp->cyi_cpu); 2903 ASSERT(MUTEX_HELD(&cpu_lock)); 2904 2905 if (cpu != NULL) { 2906 (void) cyclic_remove_here(cpu, idp->cyi_ndx, NULL, CY_WAIT); 2907 } else { 2908 ASSERT(idp->cyi_omni_list != NULL); 2909 while (idp->cyi_omni_list != NULL) 2910 cyclic_omni_stop(idp, idp->cyi_omni_list->cyo_cpu); 2911 } 2912 2913 if (prev != NULL) { 2914 ASSERT(cyclic_id_head != idp); 2915 prev->cyi_next = next; 2916 } else { 2917 ASSERT(cyclic_id_head == idp); 2918 cyclic_id_head = next; 2919 } 2920 2921 if (next != NULL) 2922 next->cyi_prev = prev; 2923 2924 kmem_cache_free(cyclic_id_cache, idp); 2925 } 2926 2927 /* 2928 * void cyclic_bind(cyclic_id_t, cpu_t *, cpupart_t *) 2929 * 2930 * Overview 2931 * 2932 * cyclic_bind() atomically changes the CPU and CPU partition bindings 2933 * of a cyclic. 2934 * 2935 * Arguments and notes 2936 * 2937 * The first argument is a cyclic_id retuned from cyclic_add(). 2938 * cyclic_bind() may _not_ be called on a cyclic_id returned from 2939 * cyclic_add_omni(). 2940 * 2941 * The second argument specifies the CPU to which to bind the specified 2942 * cyclic. If the specified cyclic is bound to a CPU other than the one 2943 * specified, it will be unbound from its bound CPU. Unbinding the cyclic 2944 * from its CPU may cause it to be juggled to another CPU. If the specified 2945 * CPU is non-NULL, the cyclic will be subsequently rebound to the specified 2946 * CPU. 2947 * 2948 * If a CPU with bound cyclics is transitioned into the P_NOINTR state, 2949 * only cyclics not bound to the CPU can be juggled away; CPU-bound cyclics 2950 * will continue to fire on the P_NOINTR CPU. A CPU with bound cyclics 2951 * cannot be offlined (attempts to offline the CPU will return EBUSY). 2952 * Likewise, cyclics may not be bound to an offline CPU; if the caller 2953 * attempts to bind a cyclic to an offline CPU, the cyclic subsystem will 2954 * panic. 2955 * 2956 * The third argument specifies the CPU partition to which to bind the 2957 * specified cyclic. If the specified cyclic is bound to a CPU partition 2958 * other than the one specified, it will be unbound from its bound 2959 * partition. Unbinding the cyclic from its CPU partition may cause it 2960 * to be juggled to another CPU. If the specified CPU partition is 2961 * non-NULL, the cyclic will be subsequently rebound to the specified CPU 2962 * partition. 2963 * 2964 * It is the caller's responsibility to assure that the specified CPU 2965 * partition contains a CPU. If it does not, the cyclic subsystem will 2966 * panic. A CPU partition with bound cyclics cannot be destroyed (attempts 2967 * to destroy the partition will return EBUSY). If a CPU with 2968 * partition-bound cyclics is transitioned into the P_NOINTR state, cyclics 2969 * bound to the CPU's partition (but not bound to the CPU) will be juggled 2970 * away only if there exists another CPU in the partition in the P_ONLINE 2971 * state. 2972 * 2973 * It is the caller's responsibility to assure that the specified CPU and 2974 * CPU partition are self-consistent. If both parameters are non-NULL, 2975 * and the specified CPU partition does not contain the specified CPU, the 2976 * cyclic subsystem will panic. 2977 * 2978 * It is the caller's responsibility to assure that the specified CPU has 2979 * been configured with respect to the cyclic subsystem. Generally, this 2980 * is always true for valid, on-line CPUs. The only periods of time during 2981 * which this may not be true are during MP boot (i.e. after cyclic_init() 2982 * is called but before cyclic_mp_init() is called) or during dynamic 2983 * reconfiguration; cyclic_bind() should only be called with great care 2984 * from these contexts. 2985 * 2986 * Return value 2987 * 2988 * None; cyclic_bind() always succeeds. 2989 * 2990 * Caller's context 2991 * 2992 * cpu_lock must be held by the caller, and the caller must not be in 2993 * interrupt context. The caller may not hold any locks which are also 2994 * grabbed by any cyclic handler. 2995 */ 2996 void 2997 cyclic_bind(cyclic_id_t id, cpu_t *d, cpupart_t *part) 2998 { 2999 cyc_id_t *idp = (cyc_id_t *)id; 3000 cyc_cpu_t *cpu = idp->cyi_cpu; 3001 cpu_t *c; 3002 uint16_t flags; 3003 3004 CYC_PTRACE("bind", d, part); 3005 ASSERT(MUTEX_HELD(&cpu_lock)); 3006 ASSERT(part == NULL || d == NULL || d->cpu_part == part); 3007 3008 if (cpu == NULL) { 3009 ASSERT(idp->cyi_omni_list != NULL); 3010 panic("attempt to change binding of omnipresent cyclic"); 3011 } 3012 3013 c = cpu->cyp_cpu; 3014 flags = cpu->cyp_cyclics[idp->cyi_ndx].cy_flags; 3015 3016 if (c != d && (flags & CYF_CPU_BOUND)) 3017 cyclic_unbind_cpu(id); 3018 3019 /* 3020 * Reload our cpu (we may have migrated). We don't have to reload 3021 * the flags field here; if we were CYF_PART_BOUND on entry, we are 3022 * CYF_PART_BOUND now. 3023 */ 3024 cpu = idp->cyi_cpu; 3025 c = cpu->cyp_cpu; 3026 3027 if (part != c->cpu_part && (flags & CYF_PART_BOUND)) 3028 cyclic_unbind_cpupart(id); 3029 3030 /* 3031 * Now reload the flags field, asserting that if we are CPU bound, 3032 * the CPU was specified (and likewise, if we are partition bound, 3033 * the partition was specified). 3034 */ 3035 cpu = idp->cyi_cpu; 3036 c = cpu->cyp_cpu; 3037 flags = cpu->cyp_cyclics[idp->cyi_ndx].cy_flags; 3038 ASSERT(!(flags & CYF_CPU_BOUND) || c == d); 3039 ASSERT(!(flags & CYF_PART_BOUND) || c->cpu_part == part); 3040 3041 if (!(flags & CYF_CPU_BOUND) && d != NULL) 3042 cyclic_bind_cpu(id, d); 3043 3044 if (!(flags & CYF_PART_BOUND) && part != NULL) 3045 cyclic_bind_cpupart(id, part); 3046 } 3047 3048 int 3049 cyclic_reprogram(cyclic_id_t id, hrtime_t expiration) 3050 { 3051 cyc_id_t *idp = (cyc_id_t *)id; 3052 cyc_cpu_t *cpu; 3053 cyc_omni_cpu_t *ocpu; 3054 cyc_index_t ndx; 3055 3056 ASSERT(expiration > 0); 3057 3058 CYC_PTRACE("reprog", idp, idp->cyi_cpu); 3059 3060 kpreempt_disable(); 3061 3062 /* 3063 * Prevent the cyclic from moving or disappearing while we reprogram. 3064 */ 3065 rw_enter(&idp->cyi_lock, RW_READER); 3066 3067 if (idp->cyi_cpu == NULL) { 3068 ASSERT(curthread->t_preempt > 0); 3069 cpu = CPU->cpu_cyclic; 3070 3071 /* 3072 * For an omni cyclic, we reprogram the cyclic corresponding 3073 * to the current CPU. Look for it in the list. 3074 */ 3075 ocpu = idp->cyi_omni_list; 3076 while (ocpu != NULL) { 3077 if (ocpu->cyo_cpu == cpu) 3078 break; 3079 ocpu = ocpu->cyo_next; 3080 } 3081 3082 if (ocpu == NULL) { 3083 /* 3084 * Didn't find it. This means that CPU offline 3085 * must have removed it racing with us. So, 3086 * nothing to do. 3087 */ 3088 rw_exit(&idp->cyi_lock); 3089 3090 kpreempt_enable(); 3091 3092 return (0); 3093 } 3094 ndx = ocpu->cyo_ndx; 3095 } else { 3096 cpu = idp->cyi_cpu; 3097 ndx = idp->cyi_ndx; 3098 } 3099 3100 if (cpu->cyp_cpu == CPU) 3101 cyclic_reprogram_cyclic(cpu, ndx, expiration); 3102 else 3103 cyclic_reprogram_here(cpu, ndx, expiration); 3104 3105 /* 3106 * Allow the cyclic to be moved or removed. 3107 */ 3108 rw_exit(&idp->cyi_lock); 3109 3110 kpreempt_enable(); 3111 3112 return (1); 3113 } 3114 3115 /* 3116 * void cyclic_move_here(cyclic_id_t) 3117 * 3118 * Overview 3119 * 3120 * cyclic_move_here() attempts to shuffle a cyclic onto the current CPU. 3121 * 3122 * Arguments and notes 3123 * 3124 * The first argument is a cyclic_id returned from cyclic_add(). 3125 * cyclic_move_here() may _not_ be called on a cyclic_id returned from 3126 * cyclic_add_omni() or one bound to a CPU or partition via cyclic_bind(). 3127 * 3128 * This cyclic shuffling is performed on a best-effort basis. If for some 3129 * reason the current CPU is unsuitable or the thread migrates between CPUs 3130 * during the call, the function may return with the cyclic residing on some 3131 * other CPU. 3132 * 3133 * Return value 3134 * 3135 * None; cyclic_move_here() always reports success. 3136 * 3137 * Caller's context 3138 * 3139 * cpu_lock must be held by the caller, and the caller must not be in 3140 * interrupt context. The caller may not hold any locks which are also 3141 * grabbed by any cyclic handler. 3142 */ 3143 void 3144 cyclic_move_here(cyclic_id_t id) 3145 { 3146 cyc_id_t *idp = (cyc_id_t *)id; 3147 cyc_cpu_t *cc = idp->cyi_cpu; 3148 cpu_t *dest = CPU; 3149 3150 ASSERT(MUTEX_HELD(&cpu_lock)); 3151 CYC_PTRACE("move_here", idp, dest); 3152 VERIFY3P(cc, !=, NULL); 3153 VERIFY3U(cc->cyp_cyclics[idp->cyi_ndx].cy_flags & 3154 (CYF_CPU_BOUND|CYF_PART_BOUND), ==, 0); 3155 3156 if (cc->cyp_cpu == dest) { 3157 return; 3158 } 3159 3160 /* Is the destination CPU suitable for a migration target? */ 3161 if (dest->cpu_cyclic == NULL || 3162 dest->cpu_cyclic->cyp_state == CYS_OFFLINE || 3163 (dest->cpu_flags & CPU_ENABLE) == 0) { 3164 return; 3165 } 3166 3167 cyclic_juggle_one_to(idp, dest->cpu_cyclic); 3168 } 3169 3170 hrtime_t 3171 cyclic_getres() 3172 { 3173 return (cyclic_resolution); 3174 } 3175 3176 void 3177 cyclic_init(cyc_backend_t *be, hrtime_t resolution) 3178 { 3179 ASSERT(MUTEX_HELD(&cpu_lock)); 3180 3181 CYC_PTRACE("init", be, resolution); 3182 cyclic_resolution = resolution; 3183 3184 /* 3185 * Copy the passed cyc_backend into the backend template. This must 3186 * be done before the CPU can be configured. 3187 */ 3188 bcopy(be, &cyclic_backend, sizeof (cyc_backend_t)); 3189 3190 /* 3191 * It's safe to look at the "CPU" pointer without disabling kernel 3192 * preemption; cyclic_init() is called only during startup by the 3193 * cyclic backend. 3194 */ 3195 cyclic_configure(CPU); 3196 cyclic_online(CPU); 3197 } 3198 3199 /* 3200 * It is assumed that cyclic_mp_init() is called some time after cyclic 3201 * init (and therefore, after cpu0 has been initialized). We grab cpu_lock, 3202 * find the already initialized CPU, and initialize every other CPU with the 3203 * same backend. Finally, we register a cpu_setup function. 3204 */ 3205 void 3206 cyclic_mp_init() 3207 { 3208 cpu_t *c; 3209 3210 mutex_enter(&cpu_lock); 3211 3212 c = cpu_list; 3213 do { 3214 if (c->cpu_cyclic == NULL) { 3215 cyclic_configure(c); 3216 cyclic_online(c); 3217 } 3218 } while ((c = c->cpu_next) != cpu_list); 3219 3220 register_cpu_setup_func(cyclic_cpu_setup, NULL); 3221 mutex_exit(&cpu_lock); 3222 } 3223 3224 /* 3225 * int cyclic_juggle(cpu_t *) 3226 * 3227 * Overview 3228 * 3229 * cyclic_juggle() juggles as many cyclics as possible away from the 3230 * specified CPU; all remaining cyclics on the CPU will either be CPU- 3231 * or partition-bound. 3232 * 3233 * Arguments and notes 3234 * 3235 * The only argument to cyclic_juggle() is the CPU from which cyclics 3236 * should be juggled. CPU-bound cyclics are never juggled; partition-bound 3237 * cyclics are only juggled if the specified CPU is in the P_NOINTR state 3238 * and there exists a P_ONLINE CPU in the partition. The cyclic subsystem 3239 * assures that a cyclic will never fire late or spuriously, even while 3240 * being juggled. 3241 * 3242 * Return value 3243 * 3244 * cyclic_juggle() returns a non-zero value if all cyclics were able to 3245 * be juggled away from the CPU, and zero if one or more cyclics could 3246 * not be juggled away. 3247 * 3248 * Caller's context 3249 * 3250 * cpu_lock must be held by the caller, and the caller must not be in 3251 * interrupt context. The caller may not hold any locks which are also 3252 * grabbed by any cyclic handler. While cyclic_juggle() _may_ be called 3253 * in any context satisfying these constraints, it _must_ be called 3254 * immediately after clearing CPU_ENABLE (i.e. before dropping cpu_lock). 3255 * Failure to do so could result in an assertion failure in the cyclic 3256 * subsystem. 3257 */ 3258 int 3259 cyclic_juggle(cpu_t *c) 3260 { 3261 cyc_cpu_t *cpu = c->cpu_cyclic; 3262 cyc_id_t *idp; 3263 int all_juggled = 1; 3264 3265 CYC_PTRACE1("juggle", c); 3266 ASSERT(MUTEX_HELD(&cpu_lock)); 3267 3268 /* 3269 * We'll go through each cyclic on the CPU, attempting to juggle 3270 * each one elsewhere. 3271 */ 3272 for (idp = cyclic_id_head; idp != NULL; idp = idp->cyi_next) { 3273 if (idp->cyi_cpu != cpu) 3274 continue; 3275 3276 if (cyclic_juggle_one(idp) == 0) { 3277 all_juggled = 0; 3278 continue; 3279 } 3280 3281 ASSERT(idp->cyi_cpu != cpu); 3282 } 3283 3284 return (all_juggled); 3285 } 3286 3287 /* 3288 * int cyclic_offline(cpu_t *) 3289 * 3290 * Overview 3291 * 3292 * cyclic_offline() offlines the cyclic subsystem on the specified CPU. 3293 * 3294 * Arguments and notes 3295 * 3296 * The only argument to cyclic_offline() is a CPU to offline. 3297 * cyclic_offline() will attempt to juggle cyclics away from the specified 3298 * CPU. 3299 * 3300 * Return value 3301 * 3302 * cyclic_offline() returns 1 if all cyclics on the CPU were juggled away 3303 * and the cyclic subsystem on the CPU was successfully offlines. 3304 * cyclic_offline returns 0 if some cyclics remain, blocking the cyclic 3305 * offline operation. All remaining cyclics on the CPU will either be 3306 * CPU- or partition-bound. 3307 * 3308 * See the "Arguments and notes" of cyclic_juggle(), below, for more detail 3309 * on cyclic juggling. 3310 * 3311 * Caller's context 3312 * 3313 * The only caller of cyclic_offline() should be the processor management 3314 * subsystem. It is expected that the caller of cyclic_offline() will 3315 * offline the CPU immediately after cyclic_offline() returns success (i.e. 3316 * before dropping cpu_lock). Moreover, it is expected that the caller will 3317 * fail the CPU offline operation if cyclic_offline() returns failure. 3318 */ 3319 int 3320 cyclic_offline(cpu_t *c) 3321 { 3322 cyc_cpu_t *cpu = c->cpu_cyclic; 3323 cyc_id_t *idp; 3324 3325 CYC_PTRACE1("offline", cpu); 3326 ASSERT(MUTEX_HELD(&cpu_lock)); 3327 3328 if (!cyclic_juggle(c)) 3329 return (0); 3330 3331 /* 3332 * This CPU is headed offline; we need to now stop omnipresent 3333 * cyclic firing on this CPU. 3334 */ 3335 for (idp = cyclic_id_head; idp != NULL; idp = idp->cyi_next) { 3336 if (idp->cyi_cpu != NULL) 3337 continue; 3338 3339 /* 3340 * We cannot possibly be offlining the last CPU; cyi_omni_list 3341 * must be non-NULL. 3342 */ 3343 ASSERT(idp->cyi_omni_list != NULL); 3344 cyclic_omni_stop(idp, cpu); 3345 } 3346 3347 ASSERT(cpu->cyp_state == CYS_ONLINE); 3348 cpu->cyp_state = CYS_OFFLINE; 3349 3350 return (1); 3351 } 3352 3353 /* 3354 * void cyclic_online(cpu_t *) 3355 * 3356 * Overview 3357 * 3358 * cyclic_online() onlines a CPU previously offlined with cyclic_offline(). 3359 * 3360 * Arguments and notes 3361 * 3362 * cyclic_online()'s only argument is a CPU to online. The specified 3363 * CPU must have been previously offlined with cyclic_offline(). After 3364 * cyclic_online() returns, the specified CPU will be eligible to execute 3365 * cyclics. 3366 * 3367 * Return value 3368 * 3369 * None; cyclic_online() always succeeds. 3370 * 3371 * Caller's context 3372 * 3373 * cyclic_online() should only be called by the processor management 3374 * subsystem; cpu_lock must be held. 3375 */ 3376 void 3377 cyclic_online(cpu_t *c) 3378 { 3379 cyc_cpu_t *cpu = c->cpu_cyclic; 3380 cyc_id_t *idp; 3381 3382 CYC_PTRACE1("online", cpu); 3383 ASSERT(c->cpu_flags & CPU_ENABLE); 3384 ASSERT(MUTEX_HELD(&cpu_lock)); 3385 ASSERT(cpu->cyp_state == CYS_OFFLINE); 3386 3387 cpu->cyp_state = CYS_ONLINE; 3388 3389 /* 3390 * Now that this CPU is open for business, we need to start firing 3391 * all omnipresent cyclics on it. 3392 */ 3393 for (idp = cyclic_id_head; idp != NULL; idp = idp->cyi_next) { 3394 if (idp->cyi_cpu != NULL) 3395 continue; 3396 3397 cyclic_omni_start(idp, cpu); 3398 } 3399 } 3400 3401 /* 3402 * void cyclic_move_in(cpu_t *) 3403 * 3404 * Overview 3405 * 3406 * cyclic_move_in() is called by the CPU partition code immediately after 3407 * the specified CPU has moved into a new partition. 3408 * 3409 * Arguments and notes 3410 * 3411 * The only argument to cyclic_move_in() is a CPU which has moved into a 3412 * new partition. If the specified CPU is P_ONLINE, and every other 3413 * CPU in the specified CPU's new partition is P_NOINTR, cyclic_move_in() 3414 * will juggle all partition-bound, CPU-unbound cyclics to the specified 3415 * CPU. 3416 * 3417 * Return value 3418 * 3419 * None; cyclic_move_in() always succeeds. 3420 * 3421 * Caller's context 3422 * 3423 * cyclic_move_in() should _only_ be called immediately after a CPU has 3424 * moved into a new partition, with cpu_lock held. As with other calls 3425 * into the cyclic subsystem, no lock may be held which is also grabbed 3426 * by any cyclic handler. 3427 */ 3428 void 3429 cyclic_move_in(cpu_t *d) 3430 { 3431 cyc_id_t *idp; 3432 cyc_cpu_t *dest = d->cpu_cyclic; 3433 cyclic_t *cyclic; 3434 cpupart_t *part = d->cpu_part; 3435 3436 CYC_PTRACE("move-in", dest, part); 3437 ASSERT(MUTEX_HELD(&cpu_lock)); 3438 3439 /* 3440 * Look for CYF_PART_BOUND cyclics in the new partition. If 3441 * we find one, check to see if it is currently on a CPU which has 3442 * interrupts disabled. If it is (and if this CPU currently has 3443 * interrupts enabled), we'll juggle those cyclics over here. 3444 */ 3445 if (!(d->cpu_flags & CPU_ENABLE)) { 3446 CYC_PTRACE1("move-in-none", dest); 3447 return; 3448 } 3449 3450 for (idp = cyclic_id_head; idp != NULL; idp = idp->cyi_next) { 3451 cyc_cpu_t *cpu = idp->cyi_cpu; 3452 cpu_t *c; 3453 3454 /* 3455 * Omnipresent cyclics are exempt from juggling. 3456 */ 3457 if (cpu == NULL) 3458 continue; 3459 3460 c = cpu->cyp_cpu; 3461 3462 if (c->cpu_part != part || (c->cpu_flags & CPU_ENABLE)) 3463 continue; 3464 3465 cyclic = &cpu->cyp_cyclics[idp->cyi_ndx]; 3466 3467 if (cyclic->cy_flags & CYF_CPU_BOUND) 3468 continue; 3469 3470 /* 3471 * We know that this cyclic is bound to its processor set 3472 * (otherwise, it would not be on a CPU with interrupts 3473 * disabled); juggle it to our CPU. 3474 */ 3475 ASSERT(cyclic->cy_flags & CYF_PART_BOUND); 3476 cyclic_juggle_one_to(idp, dest); 3477 } 3478 3479 CYC_PTRACE1("move-in-done", dest); 3480 } 3481 3482 /* 3483 * int cyclic_move_out(cpu_t *) 3484 * 3485 * Overview 3486 * 3487 * cyclic_move_out() is called by the CPU partition code immediately before 3488 * the specified CPU is to move out of its partition. 3489 * 3490 * Arguments and notes 3491 * 3492 * The only argument to cyclic_move_out() is a CPU which is to move out of 3493 * its partition. 3494 * 3495 * cyclic_move_out() will attempt to juggle away all partition-bound 3496 * cyclics. If the specified CPU is the last CPU in a partition with 3497 * partition-bound cyclics, cyclic_move_out() will fail. If there exists 3498 * a partition-bound cyclic which is CPU-bound to the specified CPU, 3499 * cyclic_move_out() will fail. 3500 * 3501 * Note that cyclic_move_out() will _only_ attempt to juggle away 3502 * partition-bound cyclics; CPU-bound cyclics which are not partition-bound 3503 * and unbound cyclics are not affected by changing the partition 3504 * affiliation of the CPU. 3505 * 3506 * Return value 3507 * 3508 * cyclic_move_out() returns 1 if all partition-bound cyclics on the CPU 3509 * were juggled away; 0 if some cyclics remain. 3510 * 3511 * Caller's context 3512 * 3513 * cyclic_move_out() should _only_ be called immediately before a CPU has 3514 * moved out of its partition, with cpu_lock held. It is expected that 3515 * the caller of cyclic_move_out() will change the processor set affiliation 3516 * of the specified CPU immediately after cyclic_move_out() returns 3517 * success (i.e. before dropping cpu_lock). Moreover, it is expected that 3518 * the caller will fail the CPU repartitioning operation if cyclic_move_out() 3519 * returns failure. As with other calls into the cyclic subsystem, no lock 3520 * may be held which is also grabbed by any cyclic handler. 3521 */ 3522 int 3523 cyclic_move_out(cpu_t *c) 3524 { 3525 cyc_id_t *idp; 3526 cyc_cpu_t *cpu = c->cpu_cyclic, *dest; 3527 cyclic_t *cyclic, *cyclics = cpu->cyp_cyclics; 3528 cpupart_t *part = c->cpu_part; 3529 3530 CYC_PTRACE1("move-out", cpu); 3531 ASSERT(MUTEX_HELD(&cpu_lock)); 3532 3533 /* 3534 * If there are any CYF_PART_BOUND cyclics on this CPU, we need 3535 * to try to juggle them away. 3536 */ 3537 for (idp = cyclic_id_head; idp != NULL; idp = idp->cyi_next) { 3538 3539 if (idp->cyi_cpu != cpu) 3540 continue; 3541 3542 cyclic = &cyclics[idp->cyi_ndx]; 3543 3544 if (!(cyclic->cy_flags & CYF_PART_BOUND)) 3545 continue; 3546 3547 dest = cyclic_pick_cpu(part, c, c, cyclic->cy_flags); 3548 3549 if (dest == NULL) { 3550 /* 3551 * We can't juggle this cyclic; we need to return 3552 * failure (we won't bother trying to juggle away 3553 * other cyclics). 3554 */ 3555 CYC_PTRACE("move-out-fail", cpu, idp); 3556 return (0); 3557 } 3558 cyclic_juggle_one_to(idp, dest); 3559 } 3560 3561 CYC_PTRACE1("move-out-done", cpu); 3562 return (1); 3563 } 3564 3565 /* 3566 * void cyclic_suspend() 3567 * 3568 * Overview 3569 * 3570 * cyclic_suspend() suspends all cyclic activity throughout the cyclic 3571 * subsystem. It should be called only by subsystems which are attempting 3572 * to suspend the entire system (e.g. checkpoint/resume, dynamic 3573 * reconfiguration). 3574 * 3575 * Arguments and notes 3576 * 3577 * cyclic_suspend() takes no arguments. Each CPU with an active cyclic 3578 * disables its backend (offline CPUs disable their backends as part of 3579 * the cyclic_offline() operation), thereby disabling future CY_HIGH_LEVEL 3580 * interrupts. 3581 * 3582 * Note that disabling CY_HIGH_LEVEL interrupts does not completely preclude 3583 * cyclic handlers from being called after cyclic_suspend() returns: if a 3584 * CY_LOCK_LEVEL or CY_LOW_LEVEL interrupt thread was blocked at the time 3585 * of cyclic_suspend(), cyclic handlers at its level may continue to be 3586 * called after the interrupt thread becomes unblocked. The 3587 * post-cyclic_suspend() activity is bounded by the pend count on all 3588 * cyclics at the time of cyclic_suspend(). Callers concerned with more 3589 * than simply disabling future CY_HIGH_LEVEL interrupts must check for 3590 * this condition. 3591 * 3592 * On most platforms, timestamps from gethrtime() and gethrestime() are not 3593 * guaranteed to monotonically increase between cyclic_suspend() and 3594 * cyclic_resume(). However, timestamps are guaranteed to monotonically 3595 * increase across the entire cyclic_suspend()/cyclic_resume() operation. 3596 * That is, every timestamp obtained before cyclic_suspend() will be less 3597 * than every timestamp obtained after cyclic_resume(). 3598 * 3599 * Return value 3600 * 3601 * None; cyclic_suspend() always succeeds. 3602 * 3603 * Caller's context 3604 * 3605 * The cyclic subsystem must be configured on every valid CPU; 3606 * cyclic_suspend() may not be called during boot or during dynamic 3607 * reconfiguration. Additionally, cpu_lock must be held, and the caller 3608 * cannot be in high-level interrupt context. However, unlike most other 3609 * cyclic entry points, cyclic_suspend() may be called with locks held 3610 * which are also acquired by CY_LOCK_LEVEL or CY_LOW_LEVEL cyclic 3611 * handlers. 3612 */ 3613 void 3614 cyclic_suspend() 3615 { 3616 cpu_t *c; 3617 cyc_cpu_t *cpu; 3618 cyc_xcallarg_t arg; 3619 cyc_backend_t *be; 3620 3621 CYC_PTRACE0("suspend"); 3622 ASSERT(MUTEX_HELD(&cpu_lock)); 3623 c = cpu_list; 3624 3625 do { 3626 cpu = c->cpu_cyclic; 3627 be = cpu->cyp_backend; 3628 arg.cyx_cpu = cpu; 3629 3630 be->cyb_xcall(be->cyb_arg, c, 3631 (cyc_func_t)cyclic_suspend_xcall, &arg); 3632 } while ((c = c->cpu_next) != cpu_list); 3633 } 3634 3635 /* 3636 * void cyclic_resume() 3637 * 3638 * cyclic_resume() resumes all cyclic activity throughout the cyclic 3639 * subsystem. It should be called only by system-suspending subsystems. 3640 * 3641 * Arguments and notes 3642 * 3643 * cyclic_resume() takes no arguments. Each CPU with an active cyclic 3644 * reenables and reprograms its backend (offline CPUs are not reenabled). 3645 * On most platforms, timestamps from gethrtime() and gethrestime() are not 3646 * guaranteed to monotonically increase between cyclic_suspend() and 3647 * cyclic_resume(). However, timestamps are guaranteed to monotonically 3648 * increase across the entire cyclic_suspend()/cyclic_resume() operation. 3649 * That is, every timestamp obtained before cyclic_suspend() will be less 3650 * than every timestamp obtained after cyclic_resume(). 3651 * 3652 * Return value 3653 * 3654 * None; cyclic_resume() always succeeds. 3655 * 3656 * Caller's context 3657 * 3658 * The cyclic subsystem must be configured on every valid CPU; 3659 * cyclic_resume() may not be called during boot or during dynamic 3660 * reconfiguration. Additionally, cpu_lock must be held, and the caller 3661 * cannot be in high-level interrupt context. However, unlike most other 3662 * cyclic entry points, cyclic_resume() may be called with locks held which 3663 * are also acquired by CY_LOCK_LEVEL or CY_LOW_LEVEL cyclic handlers. 3664 */ 3665 void 3666 cyclic_resume() 3667 { 3668 cpu_t *c; 3669 cyc_cpu_t *cpu; 3670 cyc_xcallarg_t arg; 3671 cyc_backend_t *be; 3672 3673 CYC_PTRACE0("resume"); 3674 ASSERT(MUTEX_HELD(&cpu_lock)); 3675 3676 c = cpu_list; 3677 3678 do { 3679 cpu = c->cpu_cyclic; 3680 be = cpu->cyp_backend; 3681 arg.cyx_cpu = cpu; 3682 3683 be->cyb_xcall(be->cyb_arg, c, 3684 (cyc_func_t)cyclic_resume_xcall, &arg); 3685 } while ((c = c->cpu_next) != cpu_list); 3686 } 3687