18561SScott.Carter@Sun.COM /* 28561SScott.Carter@Sun.COM * CDDL HEADER START 38561SScott.Carter@Sun.COM * 48561SScott.Carter@Sun.COM * The contents of this file are subject to the terms of the 58561SScott.Carter@Sun.COM * Common Development and Distribution License (the "License"). 68561SScott.Carter@Sun.COM * You may not use this file except in compliance with the License. 78561SScott.Carter@Sun.COM * 88561SScott.Carter@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 98561SScott.Carter@Sun.COM * or http://www.opensolaris.org/os/licensing. 108561SScott.Carter@Sun.COM * See the License for the specific language governing permissions 118561SScott.Carter@Sun.COM * and limitations under the License. 128561SScott.Carter@Sun.COM * 138561SScott.Carter@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each 148561SScott.Carter@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 158561SScott.Carter@Sun.COM * If applicable, add the following below this CDDL HEADER, with the 168561SScott.Carter@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying 178561SScott.Carter@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner] 188561SScott.Carter@Sun.COM * 198561SScott.Carter@Sun.COM * CDDL HEADER END 208561SScott.Carter@Sun.COM */ 218561SScott.Carter@Sun.COM /* 22*12473SScott.Carter@Oracle.COM * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 238561SScott.Carter@Sun.COM */ 248561SScott.Carter@Sun.COM 258561SScott.Carter@Sun.COM #include <sys/note.h> 268561SScott.Carter@Sun.COM #include <sys/sysmacros.h> 278561SScott.Carter@Sun.COM #include <sys/types.h> 288561SScott.Carter@Sun.COM #include <sys/param.h> 298561SScott.Carter@Sun.COM #include <sys/systm.h> 308561SScott.Carter@Sun.COM #include <sys/kmem.h> 318561SScott.Carter@Sun.COM #include <sys/cmn_err.h> 328561SScott.Carter@Sun.COM #include <sys/debug.h> 338561SScott.Carter@Sun.COM #include <sys/ddi.h> 348561SScott.Carter@Sun.COM #include <sys/sunndi.h> 358561SScott.Carter@Sun.COM #include <sys/ndi_impldefs.h> /* include prototypes */ 368561SScott.Carter@Sun.COM 378561SScott.Carter@Sun.COM /* 388561SScott.Carter@Sun.COM * Interrupt Resource Management (IRM). 398561SScott.Carter@Sun.COM */ 408561SScott.Carter@Sun.COM 418561SScott.Carter@Sun.COM #define DDI_IRM_BALANCE_DELAY (60) /* In seconds */ 428561SScott.Carter@Sun.COM 438561SScott.Carter@Sun.COM #define DDI_IRM_HAS_CB(c) ((c) && (c->cb_flags & DDI_CB_FLAG_INTR)) 448561SScott.Carter@Sun.COM 458561SScott.Carter@Sun.COM #define DDI_IRM_IS_REDUCIBLE(r) (((r->ireq_flags & DDI_IRM_FLAG_CALLBACK) && \ 468561SScott.Carter@Sun.COM (r->ireq_type == DDI_INTR_TYPE_MSIX)) || \ 478561SScott.Carter@Sun.COM (r->ireq_flags & DDI_IRM_FLAG_NEW)) 488561SScott.Carter@Sun.COM 498561SScott.Carter@Sun.COM extern pri_t minclsyspri; 508561SScott.Carter@Sun.COM 518561SScott.Carter@Sun.COM /* Global policies */ 528561SScott.Carter@Sun.COM int irm_enable = 1; 538561SScott.Carter@Sun.COM boolean_t irm_active = B_FALSE; 548561SScott.Carter@Sun.COM int irm_default_policy = DDI_IRM_POLICY_LARGE; 558561SScott.Carter@Sun.COM uint_t irm_balance_delay = DDI_IRM_BALANCE_DELAY; 568561SScott.Carter@Sun.COM 578561SScott.Carter@Sun.COM /* Global list of interrupt pools */ 588561SScott.Carter@Sun.COM kmutex_t irm_pools_lock; 598561SScott.Carter@Sun.COM list_t irm_pools_list; 608561SScott.Carter@Sun.COM 618561SScott.Carter@Sun.COM /* Global debug tunables */ 628561SScott.Carter@Sun.COM #ifdef DEBUG 638561SScott.Carter@Sun.COM int irm_debug_policy = 0; 648561SScott.Carter@Sun.COM uint_t irm_debug_size = 0; 658561SScott.Carter@Sun.COM #endif /* DEBUG */ 668561SScott.Carter@Sun.COM 678561SScott.Carter@Sun.COM static void irm_balance_thread(ddi_irm_pool_t *); 688561SScott.Carter@Sun.COM static void i_ddi_irm_balance(ddi_irm_pool_t *); 698561SScott.Carter@Sun.COM static void i_ddi_irm_enqueue(ddi_irm_pool_t *, boolean_t); 708561SScott.Carter@Sun.COM static void i_ddi_irm_reduce(ddi_irm_pool_t *pool); 7110173SEvan.Yan@Sun.COM static int i_ddi_irm_reduce_by_policy(ddi_irm_pool_t *, int, int); 728561SScott.Carter@Sun.COM static void i_ddi_irm_reduce_new(ddi_irm_pool_t *, int); 738561SScott.Carter@Sun.COM static void i_ddi_irm_insertion_sort(list_t *, ddi_irm_req_t *); 748561SScott.Carter@Sun.COM static int i_ddi_irm_notify(ddi_irm_pool_t *, ddi_irm_req_t *); 75*12473SScott.Carter@Oracle.COM static int i_ddi_irm_modify_increase(ddi_irm_req_t *, int); 768561SScott.Carter@Sun.COM 778561SScott.Carter@Sun.COM /* 788561SScott.Carter@Sun.COM * OS Initialization Routines 798561SScott.Carter@Sun.COM */ 808561SScott.Carter@Sun.COM 818561SScott.Carter@Sun.COM /* 828561SScott.Carter@Sun.COM * irm_init() 838561SScott.Carter@Sun.COM * 848561SScott.Carter@Sun.COM * Initialize IRM subsystem before any drivers are attached. 858561SScott.Carter@Sun.COM */ 868561SScott.Carter@Sun.COM void 878561SScott.Carter@Sun.COM irm_init(void) 888561SScott.Carter@Sun.COM { 898561SScott.Carter@Sun.COM /* Do nothing if IRM is disabled */ 908561SScott.Carter@Sun.COM if (!irm_enable) 918561SScott.Carter@Sun.COM return; 928561SScott.Carter@Sun.COM 938561SScott.Carter@Sun.COM /* Verify that the default balancing policy is valid */ 948561SScott.Carter@Sun.COM if (!DDI_IRM_POLICY_VALID(irm_default_policy)) 958561SScott.Carter@Sun.COM irm_default_policy = DDI_IRM_POLICY_LARGE; 968561SScott.Carter@Sun.COM 978561SScott.Carter@Sun.COM /* Initialize the global list of interrupt pools */ 988561SScott.Carter@Sun.COM mutex_init(&irm_pools_lock, NULL, MUTEX_DRIVER, NULL); 998561SScott.Carter@Sun.COM list_create(&irm_pools_list, sizeof (ddi_irm_pool_t), 1008561SScott.Carter@Sun.COM offsetof(ddi_irm_pool_t, ipool_link)); 1018561SScott.Carter@Sun.COM } 1028561SScott.Carter@Sun.COM 1038561SScott.Carter@Sun.COM /* 1048561SScott.Carter@Sun.COM * i_ddi_irm_poststartup() 1058561SScott.Carter@Sun.COM * 1068561SScott.Carter@Sun.COM * IRM is not activated until after the IO subsystem is initialized. 1078561SScott.Carter@Sun.COM * When activated, per-pool balancing threads are spawned and a flag 1088561SScott.Carter@Sun.COM * is set so that all future pools will be activated when created. 1098561SScott.Carter@Sun.COM * 1108561SScott.Carter@Sun.COM * NOTE: the global variable 'irm_enable' disables IRM if zero. 1118561SScott.Carter@Sun.COM */ 1128561SScott.Carter@Sun.COM void 1138561SScott.Carter@Sun.COM i_ddi_irm_poststartup(void) 1148561SScott.Carter@Sun.COM { 1158561SScott.Carter@Sun.COM ddi_irm_pool_t *pool_p; 1168561SScott.Carter@Sun.COM 1178561SScott.Carter@Sun.COM /* Do nothing if IRM is disabled */ 1188561SScott.Carter@Sun.COM if (!irm_enable) 1198561SScott.Carter@Sun.COM return; 1208561SScott.Carter@Sun.COM 1218561SScott.Carter@Sun.COM /* Lock the global list */ 1228561SScott.Carter@Sun.COM mutex_enter(&irm_pools_lock); 1238561SScott.Carter@Sun.COM 1248561SScott.Carter@Sun.COM /* Activate all defined pools */ 1258561SScott.Carter@Sun.COM for (pool_p = list_head(&irm_pools_list); pool_p; 1268561SScott.Carter@Sun.COM pool_p = list_next(&irm_pools_list, pool_p)) 1278561SScott.Carter@Sun.COM pool_p->ipool_thread = thread_create(NULL, 0, 1288561SScott.Carter@Sun.COM irm_balance_thread, pool_p, 0, &p0, TS_RUN, minclsyspri); 1298561SScott.Carter@Sun.COM 1308561SScott.Carter@Sun.COM /* Set future pools to be active */ 1318561SScott.Carter@Sun.COM irm_active = B_TRUE; 1328561SScott.Carter@Sun.COM 1338561SScott.Carter@Sun.COM /* Unlock the global list */ 1348561SScott.Carter@Sun.COM mutex_exit(&irm_pools_lock); 1358561SScott.Carter@Sun.COM } 1368561SScott.Carter@Sun.COM 1378561SScott.Carter@Sun.COM /* 1388561SScott.Carter@Sun.COM * NDI interfaces for creating/destroying IRM pools. 1398561SScott.Carter@Sun.COM */ 1408561SScott.Carter@Sun.COM 1418561SScott.Carter@Sun.COM /* 1428561SScott.Carter@Sun.COM * ndi_irm_create() 1438561SScott.Carter@Sun.COM * 1448561SScott.Carter@Sun.COM * Nexus interface to create an IRM pool. Create the new 1458561SScott.Carter@Sun.COM * pool and add it to the global list of interrupt pools. 1468561SScott.Carter@Sun.COM */ 1478561SScott.Carter@Sun.COM int 1488561SScott.Carter@Sun.COM ndi_irm_create(dev_info_t *dip, ddi_irm_params_t *paramsp, 1498561SScott.Carter@Sun.COM ddi_irm_pool_t **pool_retp) 1508561SScott.Carter@Sun.COM { 1518561SScott.Carter@Sun.COM ddi_irm_pool_t *pool_p; 1528561SScott.Carter@Sun.COM 1538561SScott.Carter@Sun.COM ASSERT(dip != NULL); 1548561SScott.Carter@Sun.COM ASSERT(paramsp != NULL); 1558561SScott.Carter@Sun.COM ASSERT(pool_retp != NULL); 1568561SScott.Carter@Sun.COM ASSERT(paramsp->iparams_total >= 1); 1578561SScott.Carter@Sun.COM ASSERT(paramsp->iparams_types != 0); 1588561SScott.Carter@Sun.COM 1598561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "ndi_irm_create: dip %p\n", (void *)dip)); 1608561SScott.Carter@Sun.COM 1618561SScott.Carter@Sun.COM /* Check if IRM is enabled */ 1628561SScott.Carter@Sun.COM if (!irm_enable) 1638561SScott.Carter@Sun.COM return (NDI_FAILURE); 1648561SScott.Carter@Sun.COM 1658561SScott.Carter@Sun.COM /* Validate parameters */ 1668561SScott.Carter@Sun.COM if ((dip == NULL) || (paramsp == NULL) || (pool_retp == NULL) || 1678925SEvan.Yan@Sun.COM (paramsp->iparams_total < 1) || (paramsp->iparams_types == 0)) 1688561SScott.Carter@Sun.COM return (NDI_FAILURE); 1698561SScott.Carter@Sun.COM 1708561SScott.Carter@Sun.COM /* Allocate and initialize the pool */ 1718561SScott.Carter@Sun.COM pool_p = kmem_zalloc(sizeof (ddi_irm_pool_t), KM_SLEEP); 1728561SScott.Carter@Sun.COM pool_p->ipool_owner = dip; 1738561SScott.Carter@Sun.COM pool_p->ipool_policy = irm_default_policy; 1748561SScott.Carter@Sun.COM pool_p->ipool_types = paramsp->iparams_types; 1758561SScott.Carter@Sun.COM pool_p->ipool_totsz = paramsp->iparams_total; 1768925SEvan.Yan@Sun.COM pool_p->ipool_defsz = MIN(DDI_MAX_MSIX_ALLOC, MAX(DDI_MIN_MSIX_ALLOC, 1778925SEvan.Yan@Sun.COM paramsp->iparams_total / DDI_MSIX_ALLOC_DIVIDER)); 1788561SScott.Carter@Sun.COM list_create(&pool_p->ipool_req_list, sizeof (ddi_irm_req_t), 1798561SScott.Carter@Sun.COM offsetof(ddi_irm_req_t, ireq_link)); 1808561SScott.Carter@Sun.COM list_create(&pool_p->ipool_scratch_list, sizeof (ddi_irm_req_t), 1818561SScott.Carter@Sun.COM offsetof(ddi_irm_req_t, ireq_scratch_link)); 1828561SScott.Carter@Sun.COM cv_init(&pool_p->ipool_cv, NULL, CV_DRIVER, NULL); 1838561SScott.Carter@Sun.COM mutex_init(&pool_p->ipool_lock, NULL, MUTEX_DRIVER, NULL); 1848561SScott.Carter@Sun.COM mutex_init(&pool_p->ipool_navail_lock, NULL, MUTEX_DRIVER, NULL); 1858561SScott.Carter@Sun.COM 1868561SScott.Carter@Sun.COM /* Add to global list of pools */ 1878561SScott.Carter@Sun.COM mutex_enter(&irm_pools_lock); 1888561SScott.Carter@Sun.COM list_insert_tail(&irm_pools_list, pool_p); 1898561SScott.Carter@Sun.COM mutex_exit(&irm_pools_lock); 1908561SScott.Carter@Sun.COM 1918561SScott.Carter@Sun.COM /* If IRM is active, then activate the pool */ 1928561SScott.Carter@Sun.COM if (irm_active) 1938561SScott.Carter@Sun.COM pool_p->ipool_thread = thread_create(NULL, 0, 1948561SScott.Carter@Sun.COM irm_balance_thread, pool_p, 0, &p0, TS_RUN, minclsyspri); 1958561SScott.Carter@Sun.COM 1968561SScott.Carter@Sun.COM *pool_retp = pool_p; 1978561SScott.Carter@Sun.COM return (NDI_SUCCESS); 1988561SScott.Carter@Sun.COM } 1998561SScott.Carter@Sun.COM 2008561SScott.Carter@Sun.COM /* 2018561SScott.Carter@Sun.COM * ndi_irm_destroy() 2028561SScott.Carter@Sun.COM * 2038561SScott.Carter@Sun.COM * Nexus interface to destroy an IRM pool. Destroy the pool 2048561SScott.Carter@Sun.COM * and remove it from the global list of interrupt pools. 2058561SScott.Carter@Sun.COM */ 2068561SScott.Carter@Sun.COM int 2078561SScott.Carter@Sun.COM ndi_irm_destroy(ddi_irm_pool_t *pool_p) 2088561SScott.Carter@Sun.COM { 2098561SScott.Carter@Sun.COM ASSERT(pool_p != NULL); 2108561SScott.Carter@Sun.COM ASSERT(pool_p->ipool_resno == 0); 2118561SScott.Carter@Sun.COM 2128561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "ndi_irm_destroy: pool_p %p\n", 2138561SScott.Carter@Sun.COM (void *)pool_p)); 2148561SScott.Carter@Sun.COM 2158561SScott.Carter@Sun.COM /* Validate parameters */ 2168561SScott.Carter@Sun.COM if (pool_p == NULL) 2178561SScott.Carter@Sun.COM return (NDI_FAILURE); 2188561SScott.Carter@Sun.COM 2198561SScott.Carter@Sun.COM /* Validate that pool is empty */ 2208561SScott.Carter@Sun.COM if (pool_p->ipool_resno != 0) 2218561SScott.Carter@Sun.COM return (NDI_BUSY); 2228561SScott.Carter@Sun.COM 2238561SScott.Carter@Sun.COM /* Remove the pool from the global list */ 2248561SScott.Carter@Sun.COM mutex_enter(&irm_pools_lock); 2258561SScott.Carter@Sun.COM list_remove(&irm_pools_list, pool_p); 2268561SScott.Carter@Sun.COM mutex_exit(&irm_pools_lock); 2278561SScott.Carter@Sun.COM 2288561SScott.Carter@Sun.COM /* Terminate the balancing thread */ 2298561SScott.Carter@Sun.COM mutex_enter(&pool_p->ipool_lock); 2308561SScott.Carter@Sun.COM if (pool_p->ipool_thread && 2318561SScott.Carter@Sun.COM (pool_p->ipool_flags & DDI_IRM_FLAG_ACTIVE)) { 2328561SScott.Carter@Sun.COM pool_p->ipool_flags |= DDI_IRM_FLAG_EXIT; 2338561SScott.Carter@Sun.COM cv_signal(&pool_p->ipool_cv); 2348885SJustin.Frank@Sun.COM mutex_exit(&pool_p->ipool_lock); 2358561SScott.Carter@Sun.COM thread_join(pool_p->ipool_thread->t_did); 2368885SJustin.Frank@Sun.COM } else 2378885SJustin.Frank@Sun.COM mutex_exit(&pool_p->ipool_lock); 2388561SScott.Carter@Sun.COM 2398561SScott.Carter@Sun.COM /* Destroy the pool */ 2408561SScott.Carter@Sun.COM cv_destroy(&pool_p->ipool_cv); 2418561SScott.Carter@Sun.COM mutex_destroy(&pool_p->ipool_lock); 2428561SScott.Carter@Sun.COM mutex_destroy(&pool_p->ipool_navail_lock); 2438561SScott.Carter@Sun.COM list_destroy(&pool_p->ipool_req_list); 2448561SScott.Carter@Sun.COM list_destroy(&pool_p->ipool_scratch_list); 2458561SScott.Carter@Sun.COM kmem_free(pool_p, sizeof (ddi_irm_pool_t)); 2468561SScott.Carter@Sun.COM 2478561SScott.Carter@Sun.COM return (NDI_SUCCESS); 2488561SScott.Carter@Sun.COM } 2498561SScott.Carter@Sun.COM 2508561SScott.Carter@Sun.COM /* 2518561SScott.Carter@Sun.COM * Insert/Modify/Remove Interrupt Requests 2528561SScott.Carter@Sun.COM */ 2538561SScott.Carter@Sun.COM 2548561SScott.Carter@Sun.COM /* 2558561SScott.Carter@Sun.COM * i_ddi_irm_insert() 2568561SScott.Carter@Sun.COM * 2578561SScott.Carter@Sun.COM * Insert a new request into an interrupt pool, and balance the pool. 2588561SScott.Carter@Sun.COM */ 2598561SScott.Carter@Sun.COM int 2608561SScott.Carter@Sun.COM i_ddi_irm_insert(dev_info_t *dip, int type, int count) 2618561SScott.Carter@Sun.COM { 2628561SScott.Carter@Sun.COM ddi_irm_req_t *req_p; 2638561SScott.Carter@Sun.COM devinfo_intr_t *intr_p; 2648561SScott.Carter@Sun.COM ddi_irm_pool_t *pool_p; 2658561SScott.Carter@Sun.COM uint_t nreq, nmin, npartial; 2668561SScott.Carter@Sun.COM boolean_t irm_flag = B_FALSE; 2678561SScott.Carter@Sun.COM 2688561SScott.Carter@Sun.COM ASSERT(dip != NULL); 2698561SScott.Carter@Sun.COM ASSERT(DDI_INTR_TYPE_FLAG_VALID(type)); 2708561SScott.Carter@Sun.COM ASSERT(count > 0); 2718561SScott.Carter@Sun.COM 2728561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_insert: dip %p type %d count %d\n", 2738561SScott.Carter@Sun.COM (void *)dip, type, count)); 2748561SScott.Carter@Sun.COM 2758561SScott.Carter@Sun.COM /* Validate parameters */ 2768561SScott.Carter@Sun.COM if ((dip == NULL) || (count < 1) || !DDI_INTR_TYPE_FLAG_VALID(type)) { 2778561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_insert: invalid args\n")); 2788561SScott.Carter@Sun.COM return (DDI_EINVAL); 2798561SScott.Carter@Sun.COM } 2808561SScott.Carter@Sun.COM 2818561SScott.Carter@Sun.COM /* Check for an existing request */ 2828561SScott.Carter@Sun.COM if (((intr_p = DEVI(dip)->devi_intr_p) != NULL) && 2838561SScott.Carter@Sun.COM (intr_p->devi_irm_req_p != NULL)) 2848561SScott.Carter@Sun.COM return (DDI_SUCCESS); 2858561SScott.Carter@Sun.COM 2868561SScott.Carter@Sun.COM /* Check for IRM support from the system */ 2878561SScott.Carter@Sun.COM if ((pool_p = i_ddi_intr_get_pool(dip, type)) == NULL) { 2888561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_insert: not supported\n")); 2898561SScott.Carter@Sun.COM return (DDI_ENOTSUP); 2908561SScott.Carter@Sun.COM } 2918561SScott.Carter@Sun.COM 2928561SScott.Carter@Sun.COM /* Check for IRM support from the driver */ 293*12473SScott.Carter@Oracle.COM if (i_ddi_irm_supported(dip, type) == DDI_SUCCESS) 2948561SScott.Carter@Sun.COM irm_flag = B_TRUE; 2958561SScott.Carter@Sun.COM 2968561SScott.Carter@Sun.COM /* Determine request size */ 2979205SEvan.Yan@Sun.COM nreq = (irm_flag) ? count : 298*12473SScott.Carter@Oracle.COM MIN(count, i_ddi_intr_get_limit(dip, type, pool_p)); 2998561SScott.Carter@Sun.COM nmin = (irm_flag) ? 1 : nreq; 3008561SScott.Carter@Sun.COM npartial = MIN(nreq, pool_p->ipool_defsz); 3018561SScott.Carter@Sun.COM 3028561SScott.Carter@Sun.COM /* Allocate and initialize the request */ 3038561SScott.Carter@Sun.COM req_p = kmem_zalloc(sizeof (ddi_irm_req_t), KM_SLEEP); 3048561SScott.Carter@Sun.COM req_p->ireq_type = type; 3058561SScott.Carter@Sun.COM req_p->ireq_dip = dip; 3068561SScott.Carter@Sun.COM req_p->ireq_pool_p = pool_p; 3078561SScott.Carter@Sun.COM req_p->ireq_nreq = nreq; 3088561SScott.Carter@Sun.COM req_p->ireq_flags = DDI_IRM_FLAG_NEW; 309*12473SScott.Carter@Oracle.COM if (irm_flag) 3108561SScott.Carter@Sun.COM req_p->ireq_flags |= DDI_IRM_FLAG_CALLBACK; 3118561SScott.Carter@Sun.COM 3128561SScott.Carter@Sun.COM /* Lock the pool */ 3138561SScott.Carter@Sun.COM mutex_enter(&pool_p->ipool_lock); 3148561SScott.Carter@Sun.COM 3158561SScott.Carter@Sun.COM /* Check for minimal fit before inserting */ 3168561SScott.Carter@Sun.COM if ((pool_p->ipool_minno + nmin) > pool_p->ipool_totsz) { 3178561SScott.Carter@Sun.COM cmn_err(CE_WARN, "%s%d: interrupt pool too full.\n", 3188561SScott.Carter@Sun.COM ddi_driver_name(dip), ddi_get_instance(dip)); 3198561SScott.Carter@Sun.COM mutex_exit(&pool_p->ipool_lock); 3208561SScott.Carter@Sun.COM kmem_free(req_p, sizeof (ddi_irm_req_t)); 3218561SScott.Carter@Sun.COM return (DDI_EAGAIN); 3228561SScott.Carter@Sun.COM } 3238561SScott.Carter@Sun.COM 3248561SScott.Carter@Sun.COM /* Insert the request into the pool */ 3258561SScott.Carter@Sun.COM pool_p->ipool_reqno += nreq; 3268561SScott.Carter@Sun.COM pool_p->ipool_minno += nmin; 3278561SScott.Carter@Sun.COM i_ddi_irm_insertion_sort(&pool_p->ipool_req_list, req_p); 3288561SScott.Carter@Sun.COM 3298561SScott.Carter@Sun.COM /* 3308561SScott.Carter@Sun.COM * Try to fulfill the request. 3318561SScott.Carter@Sun.COM * 3328561SScott.Carter@Sun.COM * If all the interrupts are available, and either the request 3338561SScott.Carter@Sun.COM * is static or the pool is active, then just take them directly. 3348561SScott.Carter@Sun.COM * 3358561SScott.Carter@Sun.COM * If only some of the interrupts are available, and the request 3368561SScott.Carter@Sun.COM * can receive future callbacks, then take some now but queue the 3378561SScott.Carter@Sun.COM * pool to be rebalanced later. 3388561SScott.Carter@Sun.COM * 3398561SScott.Carter@Sun.COM * Otherwise, immediately rebalance the pool and wait. 3408561SScott.Carter@Sun.COM */ 3418561SScott.Carter@Sun.COM if ((!irm_flag || (pool_p->ipool_flags & DDI_IRM_FLAG_ACTIVE)) && 3428561SScott.Carter@Sun.COM ((pool_p->ipool_resno + nreq) <= pool_p->ipool_totsz)) { 3438561SScott.Carter@Sun.COM 3448561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_insert: " 3458561SScott.Carter@Sun.COM "request completely fulfilled.\n")); 3468561SScott.Carter@Sun.COM pool_p->ipool_resno += nreq; 3478561SScott.Carter@Sun.COM req_p->ireq_navail = nreq; 3488561SScott.Carter@Sun.COM req_p->ireq_flags &= ~(DDI_IRM_FLAG_NEW); 3498561SScott.Carter@Sun.COM 3508561SScott.Carter@Sun.COM } else if (irm_flag && 3518561SScott.Carter@Sun.COM ((pool_p->ipool_resno + npartial) <= pool_p->ipool_totsz)) { 3528561SScott.Carter@Sun.COM 3538561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_insert: " 3548561SScott.Carter@Sun.COM "request partially fulfilled.\n")); 3558561SScott.Carter@Sun.COM pool_p->ipool_resno += npartial; 3568561SScott.Carter@Sun.COM req_p->ireq_navail = npartial; 3578561SScott.Carter@Sun.COM req_p->ireq_flags &= ~(DDI_IRM_FLAG_NEW); 3588561SScott.Carter@Sun.COM i_ddi_irm_enqueue(pool_p, B_FALSE); 3598561SScott.Carter@Sun.COM 3608561SScott.Carter@Sun.COM } else { 3618561SScott.Carter@Sun.COM 3628561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_insert: " 3638561SScott.Carter@Sun.COM "request needs immediate rebalance.\n")); 3648561SScott.Carter@Sun.COM i_ddi_irm_enqueue(pool_p, B_TRUE); 3658561SScott.Carter@Sun.COM req_p->ireq_flags &= ~(DDI_IRM_FLAG_NEW); 3668561SScott.Carter@Sun.COM } 3678561SScott.Carter@Sun.COM 3688561SScott.Carter@Sun.COM /* Fail if the request cannot be fulfilled at all */ 3698561SScott.Carter@Sun.COM if (req_p->ireq_navail == 0) { 3708561SScott.Carter@Sun.COM cmn_err(CE_WARN, "%s%d: interrupt pool too full.\n", 3718561SScott.Carter@Sun.COM ddi_driver_name(dip), ddi_get_instance(dip)); 3728561SScott.Carter@Sun.COM pool_p->ipool_reqno -= nreq; 3738561SScott.Carter@Sun.COM pool_p->ipool_minno -= nmin; 3748561SScott.Carter@Sun.COM list_remove(&pool_p->ipool_req_list, req_p); 37510173SEvan.Yan@Sun.COM mutex_exit(&pool_p->ipool_lock); 3768561SScott.Carter@Sun.COM kmem_free(req_p, sizeof (ddi_irm_req_t)); 3778561SScott.Carter@Sun.COM return (DDI_EAGAIN); 3788561SScott.Carter@Sun.COM } 3798561SScott.Carter@Sun.COM 3808561SScott.Carter@Sun.COM /* Unlock the pool */ 3818561SScott.Carter@Sun.COM mutex_exit(&pool_p->ipool_lock); 3828561SScott.Carter@Sun.COM 3838561SScott.Carter@Sun.COM intr_p->devi_irm_req_p = req_p; 3848561SScott.Carter@Sun.COM return (DDI_SUCCESS); 3858561SScott.Carter@Sun.COM } 3868561SScott.Carter@Sun.COM 3878561SScott.Carter@Sun.COM /* 3888561SScott.Carter@Sun.COM * i_ddi_irm_modify() 3898561SScott.Carter@Sun.COM * 3908561SScott.Carter@Sun.COM * Modify an existing request in an interrupt pool, and balance the pool. 3918561SScott.Carter@Sun.COM */ 3928561SScott.Carter@Sun.COM int 3938561SScott.Carter@Sun.COM i_ddi_irm_modify(dev_info_t *dip, int nreq) 3948561SScott.Carter@Sun.COM { 3958561SScott.Carter@Sun.COM devinfo_intr_t *intr_p; 3968561SScott.Carter@Sun.COM ddi_irm_req_t *req_p; 3978561SScott.Carter@Sun.COM ddi_irm_pool_t *pool_p; 398*12473SScott.Carter@Oracle.COM int type; 399*12473SScott.Carter@Oracle.COM int retval = DDI_SUCCESS; 4008561SScott.Carter@Sun.COM 4018561SScott.Carter@Sun.COM ASSERT(dip != NULL); 402*12473SScott.Carter@Oracle.COM ASSERT(nreq > 0); 4038561SScott.Carter@Sun.COM 4048561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_modify: dip %p nreq %d\n", 4058561SScott.Carter@Sun.COM (void *)dip, nreq)); 4068561SScott.Carter@Sun.COM 4078561SScott.Carter@Sun.COM /* Validate parameters */ 4088561SScott.Carter@Sun.COM if ((dip == NULL) || (nreq < 1)) { 4098561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_modify: invalid args\n")); 4108561SScott.Carter@Sun.COM return (DDI_EINVAL); 4118561SScott.Carter@Sun.COM } 4128561SScott.Carter@Sun.COM 413*12473SScott.Carter@Oracle.COM /* Do nothing if not mapped to an IRM pool */ 414*12473SScott.Carter@Oracle.COM if (((intr_p = DEVI(dip)->devi_intr_p) == NULL) || 415*12473SScott.Carter@Oracle.COM ((req_p = intr_p->devi_irm_req_p) == NULL)) 416*12473SScott.Carter@Oracle.COM return (DDI_SUCCESS); 417*12473SScott.Carter@Oracle.COM 418*12473SScott.Carter@Oracle.COM /* Do nothing if new size is the same */ 419*12473SScott.Carter@Oracle.COM if (nreq == req_p->ireq_nreq) 420*12473SScott.Carter@Oracle.COM return (DDI_SUCCESS); 421*12473SScott.Carter@Oracle.COM 422*12473SScott.Carter@Oracle.COM /* Do not allow MSI requests to be resized */ 423*12473SScott.Carter@Oracle.COM if ((type = req_p->ireq_type) == DDI_INTR_TYPE_MSI) { 424*12473SScott.Carter@Oracle.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_modify: invalid type\n")); 4258561SScott.Carter@Sun.COM return (DDI_ENOTSUP); 4268561SScott.Carter@Sun.COM } 4278561SScott.Carter@Sun.COM 428*12473SScott.Carter@Oracle.COM /* Select the pool */ 429*12473SScott.Carter@Oracle.COM if ((pool_p = req_p->ireq_pool_p) == NULL) { 430*12473SScott.Carter@Oracle.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_modify: missing pool\n")); 431*12473SScott.Carter@Oracle.COM return (DDI_FAILURE); 432*12473SScott.Carter@Oracle.COM } 433*12473SScott.Carter@Oracle.COM 4348561SScott.Carter@Sun.COM /* Validate request size is not too large */ 435*12473SScott.Carter@Oracle.COM if (nreq > i_ddi_intr_get_limit(dip, type, pool_p)) { 4368561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_modify: invalid args\n")); 4378561SScott.Carter@Sun.COM return (DDI_EINVAL); 4388561SScott.Carter@Sun.COM } 4398561SScott.Carter@Sun.COM 440*12473SScott.Carter@Oracle.COM /* Lock the pool */ 441*12473SScott.Carter@Oracle.COM mutex_enter(&pool_p->ipool_lock); 442*12473SScott.Carter@Oracle.COM 4438561SScott.Carter@Sun.COM /* 444*12473SScott.Carter@Oracle.COM * Process the modification. 445*12473SScott.Carter@Oracle.COM * 446*12473SScott.Carter@Oracle.COM * - To increase a non-IRM request, call the implementation in 447*12473SScott.Carter@Oracle.COM * i_ddi_irm_modify_increase(). 448*12473SScott.Carter@Oracle.COM * 449*12473SScott.Carter@Oracle.COM * - To decrease a non-IRM request, directly update the pool and 450*12473SScott.Carter@Oracle.COM * request, then queue the pool for later rebalancing. 451*12473SScott.Carter@Oracle.COM * 452*12473SScott.Carter@Oracle.COM * - To modify an IRM request, always queue the pool for later 453*12473SScott.Carter@Oracle.COM * rebalancing. IRM consumers rely upon callbacks for changes. 4548561SScott.Carter@Sun.COM */ 455*12473SScott.Carter@Oracle.COM if ((nreq > req_p->ireq_nreq) && 456*12473SScott.Carter@Oracle.COM (i_ddi_irm_supported(dip, type) != DDI_SUCCESS)) { 4578561SScott.Carter@Sun.COM 458*12473SScott.Carter@Oracle.COM retval = i_ddi_irm_modify_increase(req_p, nreq); 459*12473SScott.Carter@Oracle.COM 460*12473SScott.Carter@Oracle.COM } else { 4618561SScott.Carter@Sun.COM 4628561SScott.Carter@Sun.COM /* Update pool and request */ 4638561SScott.Carter@Sun.COM pool_p->ipool_reqno -= req_p->ireq_nreq; 4648561SScott.Carter@Sun.COM pool_p->ipool_reqno += nreq; 465*12473SScott.Carter@Oracle.COM if (i_ddi_irm_supported(dip, type) != DDI_SUCCESS) { 466*12473SScott.Carter@Oracle.COM pool_p->ipool_minno -= req_p->ireq_navail; 467*12473SScott.Carter@Oracle.COM pool_p->ipool_resno -= req_p->ireq_navail; 468*12473SScott.Carter@Oracle.COM pool_p->ipool_minno += nreq; 469*12473SScott.Carter@Oracle.COM pool_p->ipool_resno += nreq; 470*12473SScott.Carter@Oracle.COM req_p->ireq_navail = nreq; 471*12473SScott.Carter@Oracle.COM } 4728561SScott.Carter@Sun.COM req_p->ireq_nreq = nreq; 4738561SScott.Carter@Sun.COM 474*12473SScott.Carter@Oracle.COM /* Re-sort request into the pool */ 4758561SScott.Carter@Sun.COM list_remove(&pool_p->ipool_req_list, req_p); 4768561SScott.Carter@Sun.COM i_ddi_irm_insertion_sort(&pool_p->ipool_req_list, req_p); 4778561SScott.Carter@Sun.COM 478*12473SScott.Carter@Oracle.COM /* Queue pool for asynchronous rebalance */ 4798561SScott.Carter@Sun.COM i_ddi_irm_enqueue(pool_p, B_FALSE); 480*12473SScott.Carter@Oracle.COM } 481*12473SScott.Carter@Oracle.COM 482*12473SScott.Carter@Oracle.COM /* Unlock the pool */ 483*12473SScott.Carter@Oracle.COM mutex_exit(&pool_p->ipool_lock); 484*12473SScott.Carter@Oracle.COM 485*12473SScott.Carter@Oracle.COM return (retval); 486*12473SScott.Carter@Oracle.COM } 487*12473SScott.Carter@Oracle.COM 488*12473SScott.Carter@Oracle.COM /* 489*12473SScott.Carter@Oracle.COM * i_ddi_irm_modify_increase() 490*12473SScott.Carter@Oracle.COM * 491*12473SScott.Carter@Oracle.COM * Increase a non-IRM request. The additional interrupts are 492*12473SScott.Carter@Oracle.COM * directly taken from the pool when possible. Otherwise, an 493*12473SScott.Carter@Oracle.COM * immediate, synchronous rebalance is performed. A temporary 494*12473SScott.Carter@Oracle.COM * proxy request is used for any rebalance operation to ensure 495*12473SScott.Carter@Oracle.COM * the request is not reduced below its current allocation. 496*12473SScott.Carter@Oracle.COM * 497*12473SScott.Carter@Oracle.COM * NOTE: pool must already be locked. 498*12473SScott.Carter@Oracle.COM */ 499*12473SScott.Carter@Oracle.COM static int 500*12473SScott.Carter@Oracle.COM i_ddi_irm_modify_increase(ddi_irm_req_t *req_p, int nreq) 501*12473SScott.Carter@Oracle.COM { 502*12473SScott.Carter@Oracle.COM dev_info_t *dip = req_p->ireq_dip; 503*12473SScott.Carter@Oracle.COM ddi_irm_pool_t *pool_p = req_p->ireq_pool_p; 504*12473SScott.Carter@Oracle.COM ddi_irm_req_t new_req; 505*12473SScott.Carter@Oracle.COM int count, delta; 506*12473SScott.Carter@Oracle.COM 507*12473SScott.Carter@Oracle.COM ASSERT(MUTEX_HELD(&pool_p->ipool_lock)); 508*12473SScott.Carter@Oracle.COM 509*12473SScott.Carter@Oracle.COM /* Compute number of additional vectors */ 510*12473SScott.Carter@Oracle.COM count = nreq - req_p->ireq_nreq; 511*12473SScott.Carter@Oracle.COM 512*12473SScott.Carter@Oracle.COM /* Check for minimal fit */ 513*12473SScott.Carter@Oracle.COM if ((pool_p->ipool_minno + count) > pool_p->ipool_totsz) { 514*12473SScott.Carter@Oracle.COM cmn_err(CE_WARN, "%s%d: interrupt pool too full.\n", 515*12473SScott.Carter@Oracle.COM ddi_driver_name(dip), ddi_get_instance(dip)); 516*12473SScott.Carter@Oracle.COM return (DDI_EAGAIN); 517*12473SScott.Carter@Oracle.COM } 5188561SScott.Carter@Sun.COM 519*12473SScott.Carter@Oracle.COM /* Update the pool */ 520*12473SScott.Carter@Oracle.COM pool_p->ipool_reqno += count; 521*12473SScott.Carter@Oracle.COM pool_p->ipool_minno += count; 522*12473SScott.Carter@Oracle.COM 523*12473SScott.Carter@Oracle.COM /* Attempt direct implementation */ 524*12473SScott.Carter@Oracle.COM if ((pool_p->ipool_resno + count) <= pool_p->ipool_totsz) { 525*12473SScott.Carter@Oracle.COM req_p->ireq_nreq += count; 526*12473SScott.Carter@Oracle.COM req_p->ireq_navail += count; 527*12473SScott.Carter@Oracle.COM pool_p->ipool_resno += count; 528*12473SScott.Carter@Oracle.COM return (DDI_SUCCESS); 529*12473SScott.Carter@Oracle.COM } 530*12473SScott.Carter@Oracle.COM 531*12473SScott.Carter@Oracle.COM /* Rebalance required: fail if pool is not active */ 532*12473SScott.Carter@Oracle.COM if ((pool_p->ipool_flags & DDI_IRM_FLAG_ACTIVE) == 0) { 533*12473SScott.Carter@Oracle.COM pool_p->ipool_reqno -= count; 534*12473SScott.Carter@Oracle.COM pool_p->ipool_minno -= count; 535*12473SScott.Carter@Oracle.COM return (DDI_EAGAIN); 5368561SScott.Carter@Sun.COM } 5378561SScott.Carter@Sun.COM 538*12473SScott.Carter@Oracle.COM /* Insert temporary proxy request */ 539*12473SScott.Carter@Oracle.COM bzero(&new_req, sizeof (ddi_irm_req_t)); 540*12473SScott.Carter@Oracle.COM new_req.ireq_dip = dip; 541*12473SScott.Carter@Oracle.COM new_req.ireq_nreq = count; 542*12473SScott.Carter@Oracle.COM new_req.ireq_pool_p = pool_p; 543*12473SScott.Carter@Oracle.COM new_req.ireq_type = req_p->ireq_type; 544*12473SScott.Carter@Oracle.COM new_req.ireq_flags = DDI_IRM_FLAG_NEW; 545*12473SScott.Carter@Oracle.COM i_ddi_irm_insertion_sort(&pool_p->ipool_req_list, &new_req); 546*12473SScott.Carter@Oracle.COM 547*12473SScott.Carter@Oracle.COM /* Synchronously rebalance */ 548*12473SScott.Carter@Oracle.COM i_ddi_irm_enqueue(pool_p, B_TRUE); 549*12473SScott.Carter@Oracle.COM 550*12473SScott.Carter@Oracle.COM /* Remove proxy request, and merge into original request */ 551*12473SScott.Carter@Oracle.COM req_p->ireq_nreq += count; 552*12473SScott.Carter@Oracle.COM if ((delta = (count - new_req.ireq_navail)) > 0) { 553*12473SScott.Carter@Oracle.COM req_p->ireq_nreq -= delta; 554*12473SScott.Carter@Oracle.COM pool_p->ipool_reqno -= delta; 555*12473SScott.Carter@Oracle.COM pool_p->ipool_minno -= delta; 556*12473SScott.Carter@Oracle.COM } 557*12473SScott.Carter@Oracle.COM req_p->ireq_navail += new_req.ireq_navail; 558*12473SScott.Carter@Oracle.COM list_remove(&pool_p->ipool_req_list, req_p); 559*12473SScott.Carter@Oracle.COM list_remove(&pool_p->ipool_req_list, &new_req); 560*12473SScott.Carter@Oracle.COM i_ddi_irm_insertion_sort(&pool_p->ipool_req_list, req_p); 561*12473SScott.Carter@Oracle.COM 5628561SScott.Carter@Sun.COM return (DDI_SUCCESS); 5638561SScott.Carter@Sun.COM } 5648561SScott.Carter@Sun.COM 5658561SScott.Carter@Sun.COM /* 5668561SScott.Carter@Sun.COM * i_ddi_irm_remove() 5678561SScott.Carter@Sun.COM * 5688561SScott.Carter@Sun.COM * Remove a request from an interrupt pool, and balance the pool. 5698561SScott.Carter@Sun.COM */ 5708561SScott.Carter@Sun.COM int 5718561SScott.Carter@Sun.COM i_ddi_irm_remove(dev_info_t *dip) 5728561SScott.Carter@Sun.COM { 5738561SScott.Carter@Sun.COM devinfo_intr_t *intr_p; 5748561SScott.Carter@Sun.COM ddi_irm_pool_t *pool_p; 5758561SScott.Carter@Sun.COM ddi_irm_req_t *req_p; 5768561SScott.Carter@Sun.COM uint_t nmin; 5778561SScott.Carter@Sun.COM 5788561SScott.Carter@Sun.COM ASSERT(dip != NULL); 5798561SScott.Carter@Sun.COM 5808561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_remove: dip %p\n", (void *)dip)); 5818561SScott.Carter@Sun.COM 5828561SScott.Carter@Sun.COM /* Validate parameters */ 5838561SScott.Carter@Sun.COM if (dip == NULL) { 5848561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_remove: invalid args\n")); 5858561SScott.Carter@Sun.COM return (DDI_EINVAL); 5868561SScott.Carter@Sun.COM } 5878561SScott.Carter@Sun.COM 5888561SScott.Carter@Sun.COM /* Check if the device has a request */ 5898561SScott.Carter@Sun.COM if (!(intr_p = DEVI(dip)->devi_intr_p) || 5908561SScott.Carter@Sun.COM !(req_p = intr_p->devi_irm_req_p)) { 5918561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_modify: not found\n")); 5928561SScott.Carter@Sun.COM return (DDI_EINVAL); 5938561SScott.Carter@Sun.COM } 5948561SScott.Carter@Sun.COM 5958561SScott.Carter@Sun.COM /* Lock the pool */ 5968561SScott.Carter@Sun.COM pool_p = req_p->ireq_pool_p; 5978561SScott.Carter@Sun.COM mutex_enter(&pool_p->ipool_lock); 5988561SScott.Carter@Sun.COM 5998561SScott.Carter@Sun.COM /* Remove request */ 6008561SScott.Carter@Sun.COM nmin = DDI_IRM_IS_REDUCIBLE(req_p) ? 1 : req_p->ireq_nreq; 6018561SScott.Carter@Sun.COM pool_p->ipool_minno -= nmin; 6028561SScott.Carter@Sun.COM pool_p->ipool_reqno -= req_p->ireq_nreq; 6038561SScott.Carter@Sun.COM pool_p->ipool_resno -= req_p->ireq_navail; 6048561SScott.Carter@Sun.COM list_remove(&pool_p->ipool_req_list, req_p); 6058561SScott.Carter@Sun.COM 6068561SScott.Carter@Sun.COM /* Queue pool to be rebalanced */ 6078561SScott.Carter@Sun.COM i_ddi_irm_enqueue(pool_p, B_FALSE); 6088561SScott.Carter@Sun.COM 6098561SScott.Carter@Sun.COM /* Unlock the pool */ 6108561SScott.Carter@Sun.COM mutex_exit(&pool_p->ipool_lock); 6118561SScott.Carter@Sun.COM 6128561SScott.Carter@Sun.COM /* Destroy the request */ 6138561SScott.Carter@Sun.COM intr_p->devi_irm_req_p = NULL; 6148561SScott.Carter@Sun.COM kmem_free(req_p, sizeof (ddi_irm_req_t)); 6158561SScott.Carter@Sun.COM 6168561SScott.Carter@Sun.COM return (DDI_SUCCESS); 6178561SScott.Carter@Sun.COM } 6188561SScott.Carter@Sun.COM 6198561SScott.Carter@Sun.COM /* 6208561SScott.Carter@Sun.COM * i_ddi_irm_set_cb() 6218561SScott.Carter@Sun.COM * 6228561SScott.Carter@Sun.COM * Change the callback flag for a request, in response to 6238561SScott.Carter@Sun.COM * a change in its callback registration. Then rebalance 6248561SScott.Carter@Sun.COM * the interrupt pool. 6258561SScott.Carter@Sun.COM * 6268561SScott.Carter@Sun.COM * NOTE: the request is not locked because the navail value 6278561SScott.Carter@Sun.COM * is not directly affected. The balancing thread may 6288561SScott.Carter@Sun.COM * modify the navail value in the background after it 6298561SScott.Carter@Sun.COM * locks the request itself. 6308561SScott.Carter@Sun.COM */ 6318561SScott.Carter@Sun.COM void 6328561SScott.Carter@Sun.COM i_ddi_irm_set_cb(dev_info_t *dip, boolean_t has_cb_flag) 6338561SScott.Carter@Sun.COM { 6348561SScott.Carter@Sun.COM devinfo_intr_t *intr_p; 6358561SScott.Carter@Sun.COM ddi_irm_pool_t *pool_p; 6368561SScott.Carter@Sun.COM ddi_irm_req_t *req_p; 6378561SScott.Carter@Sun.COM uint_t nreq; 6388561SScott.Carter@Sun.COM 6398561SScott.Carter@Sun.COM ASSERT(dip != NULL); 6408561SScott.Carter@Sun.COM 6418561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_set_cb: dip %p has_cb_flag %d\n", 6428561SScott.Carter@Sun.COM (void *)dip, (int)has_cb_flag)); 6438561SScott.Carter@Sun.COM 6448561SScott.Carter@Sun.COM /* Validate parameters */ 6458561SScott.Carter@Sun.COM if (dip == NULL) 6468561SScott.Carter@Sun.COM return; 6478561SScott.Carter@Sun.COM 6488561SScott.Carter@Sun.COM /* Check for association with interrupt pool */ 6498561SScott.Carter@Sun.COM if (!(intr_p = DEVI(dip)->devi_intr_p) || 6508561SScott.Carter@Sun.COM !(req_p = intr_p->devi_irm_req_p)) { 6518561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_set_cb: not in pool\n")); 6528561SScott.Carter@Sun.COM return; 6538561SScott.Carter@Sun.COM } 6548561SScott.Carter@Sun.COM 6558561SScott.Carter@Sun.COM /* Lock the pool */ 6568561SScott.Carter@Sun.COM pool_p = req_p->ireq_pool_p; 6578561SScott.Carter@Sun.COM mutex_enter(&pool_p->ipool_lock); 6588561SScott.Carter@Sun.COM 6598561SScott.Carter@Sun.COM /* 6608561SScott.Carter@Sun.COM * Update the request and the pool 6618561SScott.Carter@Sun.COM */ 6628561SScott.Carter@Sun.COM if (has_cb_flag) { 6638561SScott.Carter@Sun.COM 6648561SScott.Carter@Sun.COM /* Update pool statistics */ 6658561SScott.Carter@Sun.COM if (req_p->ireq_type == DDI_INTR_TYPE_MSIX) 6668561SScott.Carter@Sun.COM pool_p->ipool_minno -= (req_p->ireq_nreq - 1); 6678561SScott.Carter@Sun.COM 6688561SScott.Carter@Sun.COM /* Update request */ 6698561SScott.Carter@Sun.COM req_p->ireq_flags |= DDI_IRM_FLAG_CALLBACK; 6708561SScott.Carter@Sun.COM 6718561SScott.Carter@Sun.COM /* Rebalance in background */ 6728561SScott.Carter@Sun.COM i_ddi_irm_enqueue(pool_p, B_FALSE); 6738561SScott.Carter@Sun.COM 6748561SScott.Carter@Sun.COM } else { 6758561SScott.Carter@Sun.COM 6768561SScott.Carter@Sun.COM /* Determine new request size */ 6778561SScott.Carter@Sun.COM nreq = MIN(req_p->ireq_nreq, pool_p->ipool_defsz); 6788561SScott.Carter@Sun.COM 6798561SScott.Carter@Sun.COM /* Update pool statistics */ 6808561SScott.Carter@Sun.COM pool_p->ipool_reqno -= req_p->ireq_nreq; 6818561SScott.Carter@Sun.COM pool_p->ipool_reqno += nreq; 6828561SScott.Carter@Sun.COM if (req_p->ireq_type == DDI_INTR_TYPE_MSIX) { 6838561SScott.Carter@Sun.COM pool_p->ipool_minno -= 1; 6848561SScott.Carter@Sun.COM pool_p->ipool_minno += nreq; 6858561SScott.Carter@Sun.COM } else { 6868561SScott.Carter@Sun.COM pool_p->ipool_minno -= req_p->ireq_nreq; 6878561SScott.Carter@Sun.COM pool_p->ipool_minno += nreq; 6888561SScott.Carter@Sun.COM } 6898561SScott.Carter@Sun.COM 6908561SScott.Carter@Sun.COM /* Update request size, and re-sort in pool */ 6918561SScott.Carter@Sun.COM req_p->ireq_nreq = nreq; 6928561SScott.Carter@Sun.COM list_remove(&pool_p->ipool_req_list, req_p); 6938561SScott.Carter@Sun.COM i_ddi_irm_insertion_sort(&pool_p->ipool_req_list, req_p); 6948561SScott.Carter@Sun.COM 6958561SScott.Carter@Sun.COM /* Rebalance synchronously, before losing callback */ 6968561SScott.Carter@Sun.COM i_ddi_irm_enqueue(pool_p, B_TRUE); 6978561SScott.Carter@Sun.COM 6988561SScott.Carter@Sun.COM /* Remove callback flag */ 6998561SScott.Carter@Sun.COM req_p->ireq_flags &= ~(DDI_IRM_FLAG_CALLBACK); 7008561SScott.Carter@Sun.COM } 7018561SScott.Carter@Sun.COM 7028561SScott.Carter@Sun.COM /* Unlock the pool */ 7038561SScott.Carter@Sun.COM mutex_exit(&pool_p->ipool_lock); 7048561SScott.Carter@Sun.COM } 7058561SScott.Carter@Sun.COM 7068561SScott.Carter@Sun.COM /* 707*12473SScott.Carter@Oracle.COM * i_ddi_irm_supported() 708*12473SScott.Carter@Oracle.COM * 709*12473SScott.Carter@Oracle.COM * Query if IRM is supported by a driver using a specific interrupt type. 710*12473SScott.Carter@Oracle.COM * Notice that IRM is limited to MSI-X users with registered callbacks. 711*12473SScott.Carter@Oracle.COM */ 712*12473SScott.Carter@Oracle.COM int 713*12473SScott.Carter@Oracle.COM i_ddi_irm_supported(dev_info_t *dip, int type) 714*12473SScott.Carter@Oracle.COM { 715*12473SScott.Carter@Oracle.COM ddi_cb_t *cb_p = DEVI(dip)->devi_cb_p; 716*12473SScott.Carter@Oracle.COM 717*12473SScott.Carter@Oracle.COM return ((DDI_IRM_HAS_CB(cb_p) && (type == DDI_INTR_TYPE_MSIX)) ? 718*12473SScott.Carter@Oracle.COM DDI_SUCCESS : DDI_ENOTSUP); 719*12473SScott.Carter@Oracle.COM } 720*12473SScott.Carter@Oracle.COM 721*12473SScott.Carter@Oracle.COM /* 7228561SScott.Carter@Sun.COM * Interrupt Pool Balancing 7238561SScott.Carter@Sun.COM */ 7248561SScott.Carter@Sun.COM 7258561SScott.Carter@Sun.COM /* 7268561SScott.Carter@Sun.COM * irm_balance_thread() 7278561SScott.Carter@Sun.COM * 7288561SScott.Carter@Sun.COM * One instance of this thread operates per each defined IRM pool. 7298561SScott.Carter@Sun.COM * It does the initial activation of the pool, as well as balancing 7308561SScott.Carter@Sun.COM * any requests that were queued up before the pool was active. 7318561SScott.Carter@Sun.COM * Once active, it waits forever to service balance operations. 7328561SScott.Carter@Sun.COM */ 7338561SScott.Carter@Sun.COM static void 7348561SScott.Carter@Sun.COM irm_balance_thread(ddi_irm_pool_t *pool_p) 7358561SScott.Carter@Sun.COM { 73611066Srafael.vanoni@sun.com clock_t interval; 7378561SScott.Carter@Sun.COM 7388561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "irm_balance_thread: pool_p %p\n", 7398561SScott.Carter@Sun.COM (void *)pool_p)); 7408561SScott.Carter@Sun.COM 7418561SScott.Carter@Sun.COM /* Lock the pool */ 7428561SScott.Carter@Sun.COM mutex_enter(&pool_p->ipool_lock); 7438561SScott.Carter@Sun.COM 7448561SScott.Carter@Sun.COM /* Perform initial balance if required */ 7458561SScott.Carter@Sun.COM if (pool_p->ipool_reqno > pool_p->ipool_resno) 7468561SScott.Carter@Sun.COM i_ddi_irm_balance(pool_p); 7478561SScott.Carter@Sun.COM 7488561SScott.Carter@Sun.COM /* Activate the pool */ 7498561SScott.Carter@Sun.COM pool_p->ipool_flags |= DDI_IRM_FLAG_ACTIVE; 7508561SScott.Carter@Sun.COM 7518561SScott.Carter@Sun.COM /* Main loop */ 7528561SScott.Carter@Sun.COM for (;;) { 7538561SScott.Carter@Sun.COM 7548561SScott.Carter@Sun.COM /* Compute the delay interval */ 7558561SScott.Carter@Sun.COM interval = drv_usectohz(irm_balance_delay * 1000000); 7568561SScott.Carter@Sun.COM 7578561SScott.Carter@Sun.COM /* Sleep until queued */ 7588561SScott.Carter@Sun.COM cv_wait(&pool_p->ipool_cv, &pool_p->ipool_lock); 7598561SScott.Carter@Sun.COM 7608561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "irm_balance_thread: signaled.\n")); 7618561SScott.Carter@Sun.COM 7628561SScott.Carter@Sun.COM /* Wait one interval, or until there are waiters */ 7638561SScott.Carter@Sun.COM if ((interval > 0) && 7648561SScott.Carter@Sun.COM !(pool_p->ipool_flags & DDI_IRM_FLAG_WAITERS) && 7658561SScott.Carter@Sun.COM !(pool_p->ipool_flags & DDI_IRM_FLAG_EXIT)) { 76611066Srafael.vanoni@sun.com (void) cv_reltimedwait(&pool_p->ipool_cv, 76711066Srafael.vanoni@sun.com &pool_p->ipool_lock, interval, TR_CLOCK_TICK); 7688561SScott.Carter@Sun.COM } 7698561SScott.Carter@Sun.COM 7708561SScott.Carter@Sun.COM /* Check if awakened to exit */ 7718561SScott.Carter@Sun.COM if (pool_p->ipool_flags & DDI_IRM_FLAG_EXIT) { 7728561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, 7738561SScott.Carter@Sun.COM "irm_balance_thread: exiting...\n")); 7748561SScott.Carter@Sun.COM mutex_exit(&pool_p->ipool_lock); 7758561SScott.Carter@Sun.COM thread_exit(); 7768561SScott.Carter@Sun.COM } 7778561SScott.Carter@Sun.COM 7788561SScott.Carter@Sun.COM /* Balance the pool */ 7798561SScott.Carter@Sun.COM i_ddi_irm_balance(pool_p); 7808561SScott.Carter@Sun.COM 7818561SScott.Carter@Sun.COM /* Notify waiters */ 7828561SScott.Carter@Sun.COM if (pool_p->ipool_flags & DDI_IRM_FLAG_WAITERS) { 7838561SScott.Carter@Sun.COM cv_broadcast(&pool_p->ipool_cv); 7848561SScott.Carter@Sun.COM pool_p->ipool_flags &= ~(DDI_IRM_FLAG_WAITERS); 7858561SScott.Carter@Sun.COM } 7868561SScott.Carter@Sun.COM 7878561SScott.Carter@Sun.COM /* Clear QUEUED condition */ 7888561SScott.Carter@Sun.COM pool_p->ipool_flags &= ~(DDI_IRM_FLAG_QUEUED); 7898561SScott.Carter@Sun.COM } 7908561SScott.Carter@Sun.COM } 7918561SScott.Carter@Sun.COM 7928561SScott.Carter@Sun.COM /* 7938561SScott.Carter@Sun.COM * i_ddi_irm_balance() 7948561SScott.Carter@Sun.COM * 7958561SScott.Carter@Sun.COM * Balance a pool. The general algorithm is to first reset all 7968561SScott.Carter@Sun.COM * requests to their maximum size, use reduction algorithms to 7978561SScott.Carter@Sun.COM * solve any imbalance, and then notify affected drivers. 7988561SScott.Carter@Sun.COM */ 7998561SScott.Carter@Sun.COM static void 8008561SScott.Carter@Sun.COM i_ddi_irm_balance(ddi_irm_pool_t *pool_p) 8018561SScott.Carter@Sun.COM { 8028561SScott.Carter@Sun.COM ddi_irm_req_t *req_p; 8038561SScott.Carter@Sun.COM 8048561SScott.Carter@Sun.COM #ifdef DEBUG 8058561SScott.Carter@Sun.COM uint_t debug_totsz = 0; 8068561SScott.Carter@Sun.COM int debug_policy = 0; 8078561SScott.Carter@Sun.COM #endif /* DEBUG */ 8088561SScott.Carter@Sun.COM 8098561SScott.Carter@Sun.COM ASSERT(pool_p != NULL); 8108561SScott.Carter@Sun.COM ASSERT(MUTEX_HELD(&pool_p->ipool_lock)); 8118561SScott.Carter@Sun.COM 8128561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_balance: pool_p %p\n", 8138561SScott.Carter@Sun.COM (void *)pool_p)); 8148561SScott.Carter@Sun.COM 8158561SScott.Carter@Sun.COM #ifdef DEBUG /* Adjust size and policy settings */ 8168561SScott.Carter@Sun.COM if (irm_debug_size > pool_p->ipool_minno) { 8178561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_balance: debug size %d\n", 8188561SScott.Carter@Sun.COM irm_debug_size)); 8198561SScott.Carter@Sun.COM debug_totsz = pool_p->ipool_totsz; 8208561SScott.Carter@Sun.COM pool_p->ipool_totsz = irm_debug_size; 8218561SScott.Carter@Sun.COM } 8228561SScott.Carter@Sun.COM if (DDI_IRM_POLICY_VALID(irm_debug_policy)) { 8238561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, 8248561SScott.Carter@Sun.COM "i_ddi_irm_balance: debug policy %d\n", irm_debug_policy)); 8258561SScott.Carter@Sun.COM debug_policy = pool_p->ipool_policy; 8268561SScott.Carter@Sun.COM pool_p->ipool_policy = irm_debug_policy; 8278561SScott.Carter@Sun.COM } 8288561SScott.Carter@Sun.COM #endif /* DEBUG */ 8298561SScott.Carter@Sun.COM 8308561SScott.Carter@Sun.COM /* Lock the availability lock */ 8318561SScott.Carter@Sun.COM mutex_enter(&pool_p->ipool_navail_lock); 8328561SScott.Carter@Sun.COM 8338561SScott.Carter@Sun.COM /* 8348561SScott.Carter@Sun.COM * Put all of the reducible requests into a scratch list. 8358561SScott.Carter@Sun.COM * Reset each one of them to their maximum availability. 8368561SScott.Carter@Sun.COM */ 8378561SScott.Carter@Sun.COM for (req_p = list_head(&pool_p->ipool_req_list); req_p; 8388561SScott.Carter@Sun.COM req_p = list_next(&pool_p->ipool_req_list, req_p)) { 8398561SScott.Carter@Sun.COM if (DDI_IRM_IS_REDUCIBLE(req_p)) { 8408561SScott.Carter@Sun.COM pool_p->ipool_resno -= req_p->ireq_navail; 8418561SScott.Carter@Sun.COM req_p->ireq_scratch = req_p->ireq_navail; 8428561SScott.Carter@Sun.COM req_p->ireq_navail = req_p->ireq_nreq; 8438561SScott.Carter@Sun.COM pool_p->ipool_resno += req_p->ireq_navail; 8448561SScott.Carter@Sun.COM list_insert_tail(&pool_p->ipool_scratch_list, req_p); 8458561SScott.Carter@Sun.COM } 8468561SScott.Carter@Sun.COM } 8478561SScott.Carter@Sun.COM 8488561SScott.Carter@Sun.COM /* Balance the requests */ 8498561SScott.Carter@Sun.COM i_ddi_irm_reduce(pool_p); 8508561SScott.Carter@Sun.COM 8518561SScott.Carter@Sun.COM /* Unlock the availability lock */ 8528561SScott.Carter@Sun.COM mutex_exit(&pool_p->ipool_navail_lock); 8538561SScott.Carter@Sun.COM 8548561SScott.Carter@Sun.COM /* 8558561SScott.Carter@Sun.COM * Process REMOVE notifications. 8568561SScott.Carter@Sun.COM * 8578561SScott.Carter@Sun.COM * If a driver fails to release interrupts: exclude it from 8588561SScott.Carter@Sun.COM * further processing, correct the resulting imbalance, and 8598561SScott.Carter@Sun.COM * start over again at the head of the scratch list. 8608561SScott.Carter@Sun.COM */ 8618561SScott.Carter@Sun.COM req_p = list_head(&pool_p->ipool_scratch_list); 8628561SScott.Carter@Sun.COM while (req_p) { 8638561SScott.Carter@Sun.COM if ((req_p->ireq_navail < req_p->ireq_scratch) && 8648561SScott.Carter@Sun.COM (i_ddi_irm_notify(pool_p, req_p) != DDI_SUCCESS)) { 8658561SScott.Carter@Sun.COM list_remove(&pool_p->ipool_scratch_list, req_p); 8668561SScott.Carter@Sun.COM mutex_enter(&pool_p->ipool_navail_lock); 8678561SScott.Carter@Sun.COM i_ddi_irm_reduce(pool_p); 8688561SScott.Carter@Sun.COM mutex_exit(&pool_p->ipool_navail_lock); 8698561SScott.Carter@Sun.COM req_p = list_head(&pool_p->ipool_scratch_list); 8708561SScott.Carter@Sun.COM } else { 8718561SScott.Carter@Sun.COM req_p = list_next(&pool_p->ipool_scratch_list, req_p); 8728561SScott.Carter@Sun.COM } 8738561SScott.Carter@Sun.COM } 8748561SScott.Carter@Sun.COM 8758561SScott.Carter@Sun.COM /* 8768561SScott.Carter@Sun.COM * Process ADD notifications. 8778561SScott.Carter@Sun.COM * 8788561SScott.Carter@Sun.COM * This is the last use of the scratch list, so empty it. 8798561SScott.Carter@Sun.COM */ 8808561SScott.Carter@Sun.COM while (req_p = list_remove_head(&pool_p->ipool_scratch_list)) { 8818561SScott.Carter@Sun.COM if (req_p->ireq_navail > req_p->ireq_scratch) { 8828561SScott.Carter@Sun.COM (void) i_ddi_irm_notify(pool_p, req_p); 8838561SScott.Carter@Sun.COM } 8848561SScott.Carter@Sun.COM } 8858561SScott.Carter@Sun.COM 8868561SScott.Carter@Sun.COM #ifdef DEBUG /* Restore size and policy settings */ 8878561SScott.Carter@Sun.COM if (debug_totsz != 0) 8888561SScott.Carter@Sun.COM pool_p->ipool_totsz = debug_totsz; 8898561SScott.Carter@Sun.COM if (debug_policy != 0) 8908561SScott.Carter@Sun.COM pool_p->ipool_policy = debug_policy; 8918561SScott.Carter@Sun.COM #endif /* DEBUG */ 8928561SScott.Carter@Sun.COM } 8938561SScott.Carter@Sun.COM 8948561SScott.Carter@Sun.COM /* 8958561SScott.Carter@Sun.COM * i_ddi_irm_reduce() 8968561SScott.Carter@Sun.COM * 8978561SScott.Carter@Sun.COM * Use reduction algorithms to correct an imbalance in a pool. 8988561SScott.Carter@Sun.COM */ 8998561SScott.Carter@Sun.COM static void 9008561SScott.Carter@Sun.COM i_ddi_irm_reduce(ddi_irm_pool_t *pool_p) 9018561SScott.Carter@Sun.COM { 90210173SEvan.Yan@Sun.COM int imbalance; 9038561SScott.Carter@Sun.COM 9048561SScott.Carter@Sun.COM ASSERT(pool_p != NULL); 9058561SScott.Carter@Sun.COM ASSERT(MUTEX_HELD(&pool_p->ipool_lock)); 9068561SScott.Carter@Sun.COM ASSERT(DDI_IRM_POLICY_VALID(pool_p->ipool_policy)); 9078561SScott.Carter@Sun.COM 9088561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_reduce: pool_p %p\n", 9098561SScott.Carter@Sun.COM (void *)pool_p)); 9108561SScott.Carter@Sun.COM 9118561SScott.Carter@Sun.COM /* Compute the imbalance. Do nothing if already balanced. */ 9128561SScott.Carter@Sun.COM if ((imbalance = pool_p->ipool_resno - pool_p->ipool_totsz) <= 0) 9138561SScott.Carter@Sun.COM return; 9148561SScott.Carter@Sun.COM 9158561SScott.Carter@Sun.COM /* 91610173SEvan.Yan@Sun.COM * Try policy based reduction first. If it failed, then 9178561SScott.Carter@Sun.COM * possibly reduce new requests as a last resort. 9188561SScott.Carter@Sun.COM */ 91910173SEvan.Yan@Sun.COM if (i_ddi_irm_reduce_by_policy(pool_p, imbalance, pool_p->ipool_policy) 92010173SEvan.Yan@Sun.COM != DDI_SUCCESS) { 9218561SScott.Carter@Sun.COM 9228561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, 9238561SScott.Carter@Sun.COM "i_ddi_irm_reduce: policy reductions failed.\n")); 9248561SScott.Carter@Sun.COM 9258561SScott.Carter@Sun.COM /* Compute remaining imbalance */ 9268561SScott.Carter@Sun.COM imbalance = pool_p->ipool_resno - pool_p->ipool_totsz; 9278561SScott.Carter@Sun.COM 9288561SScott.Carter@Sun.COM ASSERT(imbalance > 0); 9298561SScott.Carter@Sun.COM 9308561SScott.Carter@Sun.COM i_ddi_irm_reduce_new(pool_p, imbalance); 9318561SScott.Carter@Sun.COM } 9328561SScott.Carter@Sun.COM } 9338561SScott.Carter@Sun.COM 9348561SScott.Carter@Sun.COM /* 9358561SScott.Carter@Sun.COM * i_ddi_irm_enqueue() 9368561SScott.Carter@Sun.COM * 9378561SScott.Carter@Sun.COM * Queue a pool to be balanced. Signals the balancing thread to wake 9388561SScott.Carter@Sun.COM * up and process the pool. If 'wait_flag' is true, then the current 9398561SScott.Carter@Sun.COM * thread becomes a waiter and blocks until the balance is completed. 9408561SScott.Carter@Sun.COM */ 9418561SScott.Carter@Sun.COM static void 9428561SScott.Carter@Sun.COM i_ddi_irm_enqueue(ddi_irm_pool_t *pool_p, boolean_t wait_flag) 9438561SScott.Carter@Sun.COM { 9448561SScott.Carter@Sun.COM ASSERT(pool_p != NULL); 9458561SScott.Carter@Sun.COM ASSERT(MUTEX_HELD(&pool_p->ipool_lock)); 9468561SScott.Carter@Sun.COM 9478561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_enqueue: pool_p %p wait_flag %d\n", 9488561SScott.Carter@Sun.COM (void *)pool_p, (int)wait_flag)); 9498561SScott.Carter@Sun.COM 9508561SScott.Carter@Sun.COM /* Do nothing if pool is already balanced */ 9518561SScott.Carter@Sun.COM #ifndef DEBUG 9528561SScott.Carter@Sun.COM if ((pool_p->ipool_reqno == pool_p->ipool_resno)) { 9538561SScott.Carter@Sun.COM #else 9548561SScott.Carter@Sun.COM if ((pool_p->ipool_reqno == pool_p->ipool_resno) && !irm_debug_size) { 9558561SScott.Carter@Sun.COM #endif /* DEBUG */ 9568561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, 9578561SScott.Carter@Sun.COM "i_ddi_irm_enqueue: pool already balanced\n")); 9588561SScott.Carter@Sun.COM return; 9598561SScott.Carter@Sun.COM } 9608561SScott.Carter@Sun.COM 9618561SScott.Carter@Sun.COM /* Avoid deadlocks when IRM is not active */ 9628561SScott.Carter@Sun.COM if (!irm_active && wait_flag) { 9638561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, 9648561SScott.Carter@Sun.COM "i_ddi_irm_enqueue: pool not active.\n")); 9658561SScott.Carter@Sun.COM return; 9668561SScott.Carter@Sun.COM } 9678561SScott.Carter@Sun.COM 9688561SScott.Carter@Sun.COM if (wait_flag) 9698561SScott.Carter@Sun.COM pool_p->ipool_flags |= DDI_IRM_FLAG_WAITERS; 9708561SScott.Carter@Sun.COM 9718561SScott.Carter@Sun.COM if (wait_flag || !(pool_p->ipool_flags & DDI_IRM_FLAG_QUEUED)) { 9728561SScott.Carter@Sun.COM pool_p->ipool_flags |= DDI_IRM_FLAG_QUEUED; 9738561SScott.Carter@Sun.COM cv_signal(&pool_p->ipool_cv); 9748561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_enqueue: pool queued.\n")); 9758561SScott.Carter@Sun.COM } 9768561SScott.Carter@Sun.COM 9778561SScott.Carter@Sun.COM if (wait_flag) { 9788561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_enqueue: waiting...\n")); 9798561SScott.Carter@Sun.COM cv_wait(&pool_p->ipool_cv, &pool_p->ipool_lock); 9808561SScott.Carter@Sun.COM } 9818561SScott.Carter@Sun.COM } 9828561SScott.Carter@Sun.COM 9838561SScott.Carter@Sun.COM /* 98410173SEvan.Yan@Sun.COM * i_ddi_irm_reduce_by_policy() 9858561SScott.Carter@Sun.COM * 98610173SEvan.Yan@Sun.COM * Reduces requests based on reduction policies. 9878561SScott.Carter@Sun.COM * 98810173SEvan.Yan@Sun.COM * For the DDI_IRM_POLICY_LARGE reduction policy, the algorithm 98910173SEvan.Yan@Sun.COM * generally reduces larger requests first, before advancing 99010173SEvan.Yan@Sun.COM * to smaller requests. 99110173SEvan.Yan@Sun.COM * For the DDI_IRM_POLICY_EVEN reduction policy, the algorithm 99210173SEvan.Yan@Sun.COM * reduces requests evenly, without giving a specific preference 99310173SEvan.Yan@Sun.COM * to smaller or larger requests. Each iteration reduces all 99410173SEvan.Yan@Sun.COM * reducible requests by the same amount until the imbalance is 99510173SEvan.Yan@Sun.COM * corrected. 99610173SEvan.Yan@Sun.COM * 99710173SEvan.Yan@Sun.COM * The scratch list is initially sorted in descending order by current 99810173SEvan.Yan@Sun.COM * navail values, which are maximized prior to reduction. This sorted 99910173SEvan.Yan@Sun.COM * order is preserved. It avoids reducing requests below the threshold 100010173SEvan.Yan@Sun.COM * of the interrupt pool's default allocation size. 10018561SScott.Carter@Sun.COM * 10028561SScott.Carter@Sun.COM * Optimizations in this algorithm include trying to reduce multiple 100310173SEvan.Yan@Sun.COM * requests together. And the algorithm attempts to reduce in larger 100410173SEvan.Yan@Sun.COM * increments when possible to minimize the total number of iterations. 10058561SScott.Carter@Sun.COM */ 10068561SScott.Carter@Sun.COM static int 100710173SEvan.Yan@Sun.COM i_ddi_irm_reduce_by_policy(ddi_irm_pool_t *pool_p, int imbalance, int policy) 10088561SScott.Carter@Sun.COM { 10098561SScott.Carter@Sun.COM ASSERT(pool_p != NULL); 10108561SScott.Carter@Sun.COM ASSERT(imbalance > 0); 10118561SScott.Carter@Sun.COM ASSERT(MUTEX_HELD(&pool_p->ipool_lock)); 10128561SScott.Carter@Sun.COM 10138561SScott.Carter@Sun.COM while (imbalance > 0) { 101410173SEvan.Yan@Sun.COM list_t *slist_p = &pool_p->ipool_scratch_list; 101510173SEvan.Yan@Sun.COM ddi_irm_req_t *req_p = list_head(slist_p), *last_p; 101610173SEvan.Yan@Sun.COM uint_t nreduce = 0, nremain = 0, stop_navail; 101710173SEvan.Yan@Sun.COM uint_t pool_defsz = pool_p->ipool_defsz; 101810173SEvan.Yan@Sun.COM uint_t reduction, max_redu; 10198561SScott.Carter@Sun.COM 102010173SEvan.Yan@Sun.COM /* Fail if none are reducible */ 102110173SEvan.Yan@Sun.COM if (!req_p || req_p->ireq_navail <= pool_defsz) { 10228561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, 102310173SEvan.Yan@Sun.COM "i_ddi_irm_reduce_by_policy: Failure. " 10248925SEvan.Yan@Sun.COM "All requests have downsized to low limit.\n")); 10258561SScott.Carter@Sun.COM return (DDI_FAILURE); 10268561SScott.Carter@Sun.COM } 10278561SScott.Carter@Sun.COM 10288561SScott.Carter@Sun.COM /* Count reducible requests */ 102910173SEvan.Yan@Sun.COM stop_navail = (policy == DDI_IRM_POLICY_LARGE) ? 103010173SEvan.Yan@Sun.COM req_p->ireq_navail - 1 : pool_defsz; 103110173SEvan.Yan@Sun.COM for (; req_p; req_p = list_next(slist_p, req_p)) { 103210173SEvan.Yan@Sun.COM if (req_p->ireq_navail <= stop_navail) 10338561SScott.Carter@Sun.COM break; 10348561SScott.Carter@Sun.COM nreduce++; 10358561SScott.Carter@Sun.COM } 10368561SScott.Carter@Sun.COM 103710173SEvan.Yan@Sun.COM /* Compute reduction */ 103810173SEvan.Yan@Sun.COM last_p = req_p ? list_prev(slist_p, req_p) : list_tail(slist_p); 103910173SEvan.Yan@Sun.COM if ((policy == DDI_IRM_POLICY_LARGE) && req_p && 104010173SEvan.Yan@Sun.COM req_p->ireq_navail > pool_defsz) 104110173SEvan.Yan@Sun.COM reduction = last_p->ireq_navail - req_p->ireq_navail; 104210173SEvan.Yan@Sun.COM else 104310173SEvan.Yan@Sun.COM reduction = last_p->ireq_navail - pool_defsz; 104410173SEvan.Yan@Sun.COM 104510173SEvan.Yan@Sun.COM if ((max_redu = reduction * nreduce) > imbalance) { 104610173SEvan.Yan@Sun.COM reduction = imbalance / nreduce; 104710173SEvan.Yan@Sun.COM nremain = imbalance % nreduce; 104810173SEvan.Yan@Sun.COM pool_p->ipool_resno -= imbalance; 104910173SEvan.Yan@Sun.COM imbalance = 0; 105010173SEvan.Yan@Sun.COM } else { 105110173SEvan.Yan@Sun.COM pool_p->ipool_resno -= max_redu; 105210173SEvan.Yan@Sun.COM imbalance -= max_redu; 10538561SScott.Carter@Sun.COM } 10548561SScott.Carter@Sun.COM 105510173SEvan.Yan@Sun.COM /* Reduce */ 105610173SEvan.Yan@Sun.COM for (req_p = list_head(slist_p); (reduction != 0) && nreduce--; 105710173SEvan.Yan@Sun.COM req_p = list_next(slist_p, req_p)) { 105810173SEvan.Yan@Sun.COM req_p->ireq_navail -= reduction; 10598561SScott.Carter@Sun.COM } 10608561SScott.Carter@Sun.COM 106110173SEvan.Yan@Sun.COM for (req_p = last_p; nremain--; 106210173SEvan.Yan@Sun.COM req_p = list_prev(slist_p, req_p)) { 106310173SEvan.Yan@Sun.COM req_p->ireq_navail--; 10648561SScott.Carter@Sun.COM } 10658561SScott.Carter@Sun.COM } 10668561SScott.Carter@Sun.COM 10678561SScott.Carter@Sun.COM return (DDI_SUCCESS); 10688561SScott.Carter@Sun.COM } 10698561SScott.Carter@Sun.COM 10708561SScott.Carter@Sun.COM /* 10718561SScott.Carter@Sun.COM * i_ddi_irm_reduce_new() 10728561SScott.Carter@Sun.COM * 10738925SEvan.Yan@Sun.COM * Reduces new requests. This is only used as a last resort 10748925SEvan.Yan@Sun.COM * after another reduction algorithm failed. 107510173SEvan.Yan@Sun.COM * 107610173SEvan.Yan@Sun.COM * NOTE: The pool locking in i_ddi_irm_insert() ensures 107710173SEvan.Yan@Sun.COM * there can be only one new request at a time in a pool. 10788561SScott.Carter@Sun.COM */ 10798561SScott.Carter@Sun.COM static void 10808561SScott.Carter@Sun.COM i_ddi_irm_reduce_new(ddi_irm_pool_t *pool_p, int imbalance) 10818561SScott.Carter@Sun.COM { 10828561SScott.Carter@Sun.COM ddi_irm_req_t *req_p; 10838561SScott.Carter@Sun.COM 10848561SScott.Carter@Sun.COM ASSERT(pool_p != NULL); 10858561SScott.Carter@Sun.COM ASSERT(imbalance > 0); 10868561SScott.Carter@Sun.COM ASSERT(MUTEX_HELD(&pool_p->ipool_lock)); 10878561SScott.Carter@Sun.COM 108810173SEvan.Yan@Sun.COM DDI_INTR_IRMDBG((CE_CONT, 108910173SEvan.Yan@Sun.COM "i_ddi_irm_reduce_new: pool_p %p imbalance %d\n", 109010173SEvan.Yan@Sun.COM (void *)pool_p, imbalance)); 109110173SEvan.Yan@Sun.COM 109210173SEvan.Yan@Sun.COM for (req_p = list_head(&pool_p->ipool_scratch_list); req_p; 109310173SEvan.Yan@Sun.COM req_p = list_next(&pool_p->ipool_scratch_list, req_p)) { 109410173SEvan.Yan@Sun.COM if (req_p->ireq_flags & DDI_IRM_FLAG_NEW) { 109510173SEvan.Yan@Sun.COM ASSERT(req_p->ireq_navail >= imbalance); 109610173SEvan.Yan@Sun.COM req_p->ireq_navail -= imbalance; 109710173SEvan.Yan@Sun.COM pool_p->ipool_resno -= imbalance; 109810173SEvan.Yan@Sun.COM return; 10998925SEvan.Yan@Sun.COM } 11008925SEvan.Yan@Sun.COM } 11018925SEvan.Yan@Sun.COM 110210173SEvan.Yan@Sun.COM /* should never go here */ 110310173SEvan.Yan@Sun.COM ASSERT(B_FALSE); 11048561SScott.Carter@Sun.COM } 11058561SScott.Carter@Sun.COM 11068561SScott.Carter@Sun.COM /* 11078561SScott.Carter@Sun.COM * Miscellaneous Helper Functions 11088561SScott.Carter@Sun.COM */ 11098561SScott.Carter@Sun.COM 11108561SScott.Carter@Sun.COM /* 11118561SScott.Carter@Sun.COM * i_ddi_intr_get_pool() 11128561SScott.Carter@Sun.COM * 11138561SScott.Carter@Sun.COM * Get an IRM pool that supplies interrupts of a specified type. 11148561SScott.Carter@Sun.COM * Invokes a DDI_INTROP_GETPOOL to the bus nexus driver. Fails 11158561SScott.Carter@Sun.COM * if no pool exists. 11168561SScott.Carter@Sun.COM */ 11178561SScott.Carter@Sun.COM ddi_irm_pool_t * 11188561SScott.Carter@Sun.COM i_ddi_intr_get_pool(dev_info_t *dip, int type) 11198561SScott.Carter@Sun.COM { 11208561SScott.Carter@Sun.COM devinfo_intr_t *intr_p; 11218561SScott.Carter@Sun.COM ddi_irm_pool_t *pool_p; 11228561SScott.Carter@Sun.COM ddi_irm_req_t *req_p; 11238561SScott.Carter@Sun.COM ddi_intr_handle_impl_t hdl; 11248561SScott.Carter@Sun.COM 11258561SScott.Carter@Sun.COM ASSERT(dip != NULL); 11268561SScott.Carter@Sun.COM ASSERT(DDI_INTR_TYPE_FLAG_VALID(type)); 11278561SScott.Carter@Sun.COM 11288561SScott.Carter@Sun.COM if (((intr_p = DEVI(dip)->devi_intr_p) != NULL) && 11298561SScott.Carter@Sun.COM ((req_p = intr_p->devi_irm_req_p) != NULL) && 11308561SScott.Carter@Sun.COM ((pool_p = req_p->ireq_pool_p) != NULL) && 11318561SScott.Carter@Sun.COM (pool_p->ipool_types & type)) { 11328561SScott.Carter@Sun.COM return (pool_p); 11338561SScott.Carter@Sun.COM } 11348561SScott.Carter@Sun.COM 11358561SScott.Carter@Sun.COM bzero(&hdl, sizeof (ddi_intr_handle_impl_t)); 11368561SScott.Carter@Sun.COM hdl.ih_dip = dip; 11378561SScott.Carter@Sun.COM hdl.ih_type = type; 11388561SScott.Carter@Sun.COM 11398561SScott.Carter@Sun.COM if (i_ddi_intr_ops(dip, dip, DDI_INTROP_GETPOOL, 11408561SScott.Carter@Sun.COM &hdl, (void *)&pool_p) == DDI_SUCCESS) 11418561SScott.Carter@Sun.COM return (pool_p); 11428561SScott.Carter@Sun.COM 11438561SScott.Carter@Sun.COM return (NULL); 11448561SScott.Carter@Sun.COM } 11458561SScott.Carter@Sun.COM 11468561SScott.Carter@Sun.COM /* 11478561SScott.Carter@Sun.COM * i_ddi_irm_insertion_sort() 11488561SScott.Carter@Sun.COM * 11498561SScott.Carter@Sun.COM * Use the insertion sort method to insert a request into a list. 11508561SScott.Carter@Sun.COM * The list is sorted in descending order by request size. 11518561SScott.Carter@Sun.COM */ 11528561SScott.Carter@Sun.COM static void 11538561SScott.Carter@Sun.COM i_ddi_irm_insertion_sort(list_t *req_list, ddi_irm_req_t *req_p) 11548561SScott.Carter@Sun.COM { 11558561SScott.Carter@Sun.COM ddi_irm_req_t *next_p; 11568561SScott.Carter@Sun.COM 11578561SScott.Carter@Sun.COM next_p = list_head(req_list); 11588561SScott.Carter@Sun.COM 11598561SScott.Carter@Sun.COM while (next_p && (next_p->ireq_nreq > req_p->ireq_nreq)) 11608561SScott.Carter@Sun.COM next_p = list_next(req_list, next_p); 11618561SScott.Carter@Sun.COM 11628561SScott.Carter@Sun.COM list_insert_before(req_list, next_p, req_p); 11638561SScott.Carter@Sun.COM } 11648561SScott.Carter@Sun.COM 11658561SScott.Carter@Sun.COM /* 11668561SScott.Carter@Sun.COM * i_ddi_irm_notify() 11678561SScott.Carter@Sun.COM * 11688561SScott.Carter@Sun.COM * Notify a driver of changes to its interrupt request using the 11698561SScott.Carter@Sun.COM * generic callback mechanism. Checks for errors in processing. 11708561SScott.Carter@Sun.COM */ 11718561SScott.Carter@Sun.COM static int 11728561SScott.Carter@Sun.COM i_ddi_irm_notify(ddi_irm_pool_t *pool_p, ddi_irm_req_t *req_p) 11738561SScott.Carter@Sun.COM { 11748561SScott.Carter@Sun.COM ddi_cb_action_t action; 11758561SScott.Carter@Sun.COM ddi_cb_t *cb_p; 11768561SScott.Carter@Sun.COM uint_t nintrs; 11778561SScott.Carter@Sun.COM int ret, count; 11788561SScott.Carter@Sun.COM 11798561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_notify: pool_p %p req_p %p\n", 11808561SScott.Carter@Sun.COM (void *)pool_p, (void *)req_p)); 11818561SScott.Carter@Sun.COM 11828561SScott.Carter@Sun.COM /* Do not notify new or unchanged requests */ 11838561SScott.Carter@Sun.COM if ((req_p->ireq_navail == req_p->ireq_scratch) || 11848561SScott.Carter@Sun.COM (req_p->ireq_flags & DDI_IRM_FLAG_NEW)) 11858561SScott.Carter@Sun.COM return (DDI_SUCCESS); 11868561SScott.Carter@Sun.COM 11878561SScott.Carter@Sun.COM /* Determine action and count */ 11888561SScott.Carter@Sun.COM if (req_p->ireq_navail > req_p->ireq_scratch) { 11898561SScott.Carter@Sun.COM action = DDI_CB_INTR_ADD; 11908561SScott.Carter@Sun.COM count = req_p->ireq_navail - req_p->ireq_scratch; 11918561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_notify: adding %d\n", 11928561SScott.Carter@Sun.COM count)); 11938561SScott.Carter@Sun.COM } else { 11948561SScott.Carter@Sun.COM action = DDI_CB_INTR_REMOVE; 11958561SScott.Carter@Sun.COM count = req_p->ireq_scratch - req_p->ireq_navail; 11968561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_notify: removing %d\n", 11978561SScott.Carter@Sun.COM count)); 11988561SScott.Carter@Sun.COM } 11998561SScott.Carter@Sun.COM 12008561SScott.Carter@Sun.COM /* Lookup driver callback */ 12018561SScott.Carter@Sun.COM if ((cb_p = DEVI(req_p->ireq_dip)->devi_cb_p) == NULL) { 12028561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_WARN, "i_ddi_irm_notify: no callback!\n")); 12038561SScott.Carter@Sun.COM return (DDI_FAILURE); 12048561SScott.Carter@Sun.COM } 12058561SScott.Carter@Sun.COM 12068561SScott.Carter@Sun.COM /* Do callback */ 12078561SScott.Carter@Sun.COM ret = cb_p->cb_func(req_p->ireq_dip, action, (void *)(uintptr_t)count, 12088561SScott.Carter@Sun.COM cb_p->cb_arg1, cb_p->cb_arg2); 12098561SScott.Carter@Sun.COM 12108561SScott.Carter@Sun.COM /* Log callback errors */ 12118561SScott.Carter@Sun.COM if (ret != DDI_SUCCESS) { 12128561SScott.Carter@Sun.COM cmn_err(CE_WARN, "%s%d: failed callback (action=%d, ret=%d)\n", 12138561SScott.Carter@Sun.COM ddi_driver_name(req_p->ireq_dip), 12148561SScott.Carter@Sun.COM ddi_get_instance(req_p->ireq_dip), (int)action, ret); 12158561SScott.Carter@Sun.COM } 12168561SScott.Carter@Sun.COM 12178561SScott.Carter@Sun.COM /* Check if the driver exceeds its availability */ 12188561SScott.Carter@Sun.COM nintrs = i_ddi_intr_get_current_nintrs(req_p->ireq_dip); 12198561SScott.Carter@Sun.COM if (nintrs > req_p->ireq_navail) { 12208561SScott.Carter@Sun.COM cmn_err(CE_WARN, "%s%d: failed to release interrupts " 12218561SScott.Carter@Sun.COM "(nintrs=%d, navail=%d).\n", 12228561SScott.Carter@Sun.COM ddi_driver_name(req_p->ireq_dip), 12238561SScott.Carter@Sun.COM ddi_get_instance(req_p->ireq_dip), nintrs, 12248561SScott.Carter@Sun.COM req_p->ireq_navail); 12258561SScott.Carter@Sun.COM pool_p->ipool_resno += (nintrs - req_p->ireq_navail); 12268561SScott.Carter@Sun.COM req_p->ireq_navail = nintrs; 12278561SScott.Carter@Sun.COM return (DDI_FAILURE); 12288561SScott.Carter@Sun.COM } 12298561SScott.Carter@Sun.COM 12308561SScott.Carter@Sun.COM /* Update request */ 12318561SScott.Carter@Sun.COM req_p->ireq_scratch = req_p->ireq_navail; 12328561SScott.Carter@Sun.COM 12338561SScott.Carter@Sun.COM return (DDI_SUCCESS); 12348561SScott.Carter@Sun.COM } 12358561SScott.Carter@Sun.COM 12368561SScott.Carter@Sun.COM /* 12378561SScott.Carter@Sun.COM * i_ddi_irm_debug_balance() 12388561SScott.Carter@Sun.COM * 12398561SScott.Carter@Sun.COM * A debug/test only routine to force the immediate, 12408561SScott.Carter@Sun.COM * synchronous rebalancing of an interrupt pool. 12418561SScott.Carter@Sun.COM */ 12428561SScott.Carter@Sun.COM #ifdef DEBUG 12438561SScott.Carter@Sun.COM void 12448561SScott.Carter@Sun.COM i_ddi_irm_debug_balance(dev_info_t *dip, boolean_t wait_flag) 12458561SScott.Carter@Sun.COM { 12468561SScott.Carter@Sun.COM ddi_irm_pool_t *pool_p; 12478561SScott.Carter@Sun.COM int type; 12488561SScott.Carter@Sun.COM 12498561SScott.Carter@Sun.COM DDI_INTR_IRMDBG((CE_CONT, "i_ddi_irm_debug_balance: dip %p wait %d\n", 12508561SScott.Carter@Sun.COM (void *)dip, (int)wait_flag)); 12518561SScott.Carter@Sun.COM 12528561SScott.Carter@Sun.COM if (((type = i_ddi_intr_get_current_type(dip)) != 0) && 12538561SScott.Carter@Sun.COM ((pool_p = i_ddi_intr_get_pool(dip, type)) != NULL)) { 12548561SScott.Carter@Sun.COM mutex_enter(&pool_p->ipool_lock); 12558561SScott.Carter@Sun.COM i_ddi_irm_enqueue(pool_p, wait_flag); 12568561SScott.Carter@Sun.COM mutex_exit(&pool_p->ipool_lock); 12578561SScott.Carter@Sun.COM } 12588561SScott.Carter@Sun.COM } 12598561SScott.Carter@Sun.COM #endif 1260