Home | History | Annotate | Line # | Download | only in pic
pic_splfuncs.c revision 1.19
      1  1.19  jmcneill /*	$NetBSD: pic_splfuncs.c,v 1.19 2021/03/01 11:29:14 jmcneill Exp $	*/
      2   1.1       bsh /*-
      3   1.1       bsh  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      4   1.1       bsh  * All rights reserved.
      5   1.1       bsh  *
      6   1.1       bsh  * This code is derived from software contributed to The NetBSD Foundation
      7   1.1       bsh  * by Matt Thomas.
      8   1.1       bsh  *
      9   1.1       bsh  * Redistribution and use in source and binary forms, with or without
     10   1.1       bsh  * modification, are permitted provided that the following conditions
     11   1.1       bsh  * are met:
     12   1.1       bsh  * 1. Redistributions of source code must retain the above copyright
     13   1.1       bsh  *    notice, this list of conditions and the following disclaimer.
     14   1.1       bsh  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1       bsh  *    notice, this list of conditions and the following disclaimer in the
     16   1.1       bsh  *    documentation and/or other materials provided with the distribution.
     17   1.1       bsh  *
     18   1.1       bsh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19   1.1       bsh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20   1.1       bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21   1.1       bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22   1.1       bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23   1.1       bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24   1.1       bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25   1.1       bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26   1.1       bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27   1.1       bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28   1.1       bsh  * POSSIBILITY OF SUCH DAMAGE.
     29   1.1       bsh  */
     30   1.1       bsh #include <sys/cdefs.h>
     31  1.19  jmcneill __KERNEL_RCSID(0, "$NetBSD: pic_splfuncs.c,v 1.19 2021/03/01 11:29:14 jmcneill Exp $");
     32   1.1       bsh 
     33   1.1       bsh #define _INTR_PRIVATE
     34   1.1       bsh #include <sys/param.h>
     35   1.4      matt #include <sys/atomic.h>
     36   1.1       bsh #include <sys/evcnt.h>
     37   1.7      matt #include <sys/lwp.h>
     38   1.4      matt #include <sys/kernel.h>
     39   1.4      matt 
     40   1.4      matt #include <dev/cons.h>
     41   1.1       bsh 
     42   1.1       bsh #include <arm/armreg.h>
     43   1.1       bsh #include <arm/cpu.h>
     44   1.1       bsh #include <arm/cpufunc.h>
     45   1.8       ryo #include <arm/locore.h>	/* for compat aarch64 */
     46   1.1       bsh 
     47   1.1       bsh #include <arm/pic/picvar.h>
     48   1.1       bsh 
     49  1.18  jmcneill #if defined(__HAVE_CPU_DOSOFTINTS_CI)
     50  1.18  jmcneill #define	CPU_DOSOFTINTS(ci)	cpu_dosoftints_ci((ci))
     51  1.18  jmcneill #else
     52  1.18  jmcneill #define	CPU_DOSOFTINTS(ci)	cpu_dosoftints()
     53  1.18  jmcneill #endif
     54  1.18  jmcneill 
     55  1.18  jmcneill #if defined(__HAVE_PIC_PENDING_INTRS)
     56  1.18  jmcneill static void	splx_dopendingints(struct cpu_info *, const int);
     57  1.18  jmcneill #endif
     58  1.18  jmcneill 
     59   1.1       bsh int
     60   1.1       bsh _splraise(int newipl)
     61   1.1       bsh {
     62   1.1       bsh 	struct cpu_info * const ci = curcpu();
     63   1.1       bsh 	const int oldipl = ci->ci_cpl;
     64  1.18  jmcneill 	KDASSERT(newipl < NIPL);
     65   1.3      matt 	if (newipl > ci->ci_cpl) {
     66   1.3      matt 		pic_set_priority(ci, newipl);
     67   1.3      matt 	}
     68   1.1       bsh 	return oldipl;
     69   1.1       bsh }
     70  1.18  jmcneill 
     71   1.1       bsh int
     72   1.1       bsh _spllower(int newipl)
     73   1.1       bsh {
     74   1.1       bsh 	struct cpu_info * const ci = curcpu();
     75   1.1       bsh 	const int oldipl = ci->ci_cpl;
     76  1.18  jmcneill 	KDASSERT(panicstr || newipl <= ci->ci_cpl);
     77   1.1       bsh 	if (newipl < ci->ci_cpl) {
     78  1.19  jmcneill 		register_t psw = DISABLE_INTERRUPT_SAVE();
     79  1.14  jmcneill 		ci->ci_intr_depth++;
     80  1.14  jmcneill 		pic_do_pending_ints(psw, newipl, NULL);
     81  1.14  jmcneill 		ci->ci_intr_depth--;
     82  1.19  jmcneill 		if ((psw & I32_bit) == 0 || newipl == IPL_NONE) {
     83  1.19  jmcneill 			ENABLE_INTERRUPT();
     84  1.19  jmcneill 		}
     85   1.4      matt 		cpu_dosoftints();
     86   1.1       bsh 	}
     87   1.1       bsh 	return oldipl;
     88   1.1       bsh }
     89   1.1       bsh 
     90   1.1       bsh void
     91   1.1       bsh splx(int savedipl)
     92   1.1       bsh {
     93   1.1       bsh 	struct cpu_info * const ci = curcpu();
     94  1.18  jmcneill 	KDASSERT(savedipl < NIPL);
     95   1.4      matt 
     96   1.4      matt 	if (__predict_false(savedipl == ci->ci_cpl)) {
     97   1.4      matt 		return;
     98   1.1       bsh 	}
     99   1.4      matt 
    100  1.14  jmcneill #if defined(__HAVE_PIC_PENDING_INTRS)
    101  1.18  jmcneill 	if (__predict_false(ci->ci_pending_ipls != 0)) {
    102  1.18  jmcneill 		splx_dopendingints(ci, savedipl);
    103  1.14  jmcneill 	}
    104  1.18  jmcneill #endif
    105  1.18  jmcneill 
    106  1.18  jmcneill 	pic_set_priority(ci, savedipl);
    107  1.18  jmcneill 	CPU_DOSOFTINTS(ci);
    108  1.10  jmcneill 
    109  1.18  jmcneill 	KDASSERTMSG(ci->ci_cpl == savedipl, "cpl %d savedipl %d",
    110  1.18  jmcneill 	    ci->ci_cpl, savedipl);
    111  1.18  jmcneill }
    112  1.18  jmcneill 
    113  1.18  jmcneill #if defined(__HAVE_PIC_PENDING_INTRS)
    114  1.18  jmcneill static void __noinline
    115  1.18  jmcneill splx_dopendingints(struct cpu_info *ci, const int savedipl)
    116  1.18  jmcneill {
    117  1.19  jmcneill 	const register_t psw = DISABLE_INTERRUPT_SAVE();
    118  1.15  jmcneill 	ci->ci_intr_depth++;
    119  1.14  jmcneill 	while ((ci->ci_pending_ipls & ~__BIT(savedipl)) > __BIT(savedipl)) {
    120  1.14  jmcneill 		KASSERT(ci->ci_pending_ipls < __BIT(NIPL));
    121  1.14  jmcneill 		for (;;) {
    122  1.14  jmcneill 			int ipl = 31 - __builtin_clz(ci->ci_pending_ipls);
    123  1.14  jmcneill 			KASSERT(ipl < NIPL);
    124  1.14  jmcneill 			if (ipl <= savedipl) {
    125  1.14  jmcneill 				break;
    126  1.14  jmcneill 			}
    127  1.14  jmcneill 
    128  1.17  jmcneill 			pic_set_priority(ci, ipl);
    129  1.14  jmcneill 			pic_list_deliver_irqs(ci, psw, ipl, NULL);
    130  1.14  jmcneill 			pic_list_unblock_irqs(ci);
    131  1.13  jmcneill 		}
    132   1.9  jmcneill 	}
    133  1.15  jmcneill 	ci->ci_intr_depth--;
    134  1.16  jmcneill 	if ((psw & I32_bit) == 0) {
    135  1.19  jmcneill 		ENABLE_INTERRUPT();
    136  1.16  jmcneill 	}
    137  1.18  jmcneill }
    138  1.17  jmcneill #endif
    139