xref: /openbsd/sys/dev/pci/drm/include/drm/drm_util.h (revision c349dbc7)
1*c349dbc7Sjsg /*
2*c349dbc7Sjsg  * Internal Header for the Direct Rendering Manager
3*c349dbc7Sjsg  *
4*c349dbc7Sjsg  * Copyright 2018 Intel Corporation
5*c349dbc7Sjsg  *
6*c349dbc7Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
7*c349dbc7Sjsg  * copy of this software and associated documentation files (the "Software"),
8*c349dbc7Sjsg  * to deal in the Software without restriction, including without limitation
9*c349dbc7Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10*c349dbc7Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
11*c349dbc7Sjsg  * Software is furnished to do so, subject to the following conditions:
12*c349dbc7Sjsg  *
13*c349dbc7Sjsg  * The above copyright notice and this permission notice (including the next
14*c349dbc7Sjsg  * paragraph) shall be included in all copies or substantial portions of the
15*c349dbc7Sjsg  * Software.
16*c349dbc7Sjsg  *
17*c349dbc7Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18*c349dbc7Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*c349dbc7Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20*c349dbc7Sjsg  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21*c349dbc7Sjsg  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22*c349dbc7Sjsg  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23*c349dbc7Sjsg  * OTHER DEALINGS IN THE SOFTWARE.
24*c349dbc7Sjsg  */
25*c349dbc7Sjsg 
26*c349dbc7Sjsg #ifndef _DRM_UTIL_H_
27*c349dbc7Sjsg #define _DRM_UTIL_H_
28*c349dbc7Sjsg 
29*c349dbc7Sjsg /**
30*c349dbc7Sjsg  * DOC: drm utils
31*c349dbc7Sjsg  *
32*c349dbc7Sjsg  * Macros and inline functions that does not naturally belong in other places
33*c349dbc7Sjsg  */
34*c349dbc7Sjsg 
35*c349dbc7Sjsg #include <linux/interrupt.h>
36*c349dbc7Sjsg #include <linux/kgdb.h>
37*c349dbc7Sjsg #include <linux/preempt.h>
38*c349dbc7Sjsg #include <linux/smp.h>
39*c349dbc7Sjsg 
40*c349dbc7Sjsg /*
41*c349dbc7Sjsg  * Use EXPORT_SYMBOL_FOR_TESTS_ONLY() for functions that shall
42*c349dbc7Sjsg  * only be visible for drmselftests.
43*c349dbc7Sjsg  */
44*c349dbc7Sjsg #if defined(CONFIG_DRM_EXPORT_FOR_TESTS)
45*c349dbc7Sjsg #define EXPORT_SYMBOL_FOR_TESTS_ONLY(x) EXPORT_SYMBOL(x)
46*c349dbc7Sjsg #else
47*c349dbc7Sjsg #define EXPORT_SYMBOL_FOR_TESTS_ONLY(x)
48*c349dbc7Sjsg #endif
49*c349dbc7Sjsg 
50*c349dbc7Sjsg /**
51*c349dbc7Sjsg  * for_each_if - helper for handling conditionals in various for_each macros
52*c349dbc7Sjsg  * @condition: The condition to check
53*c349dbc7Sjsg  *
54*c349dbc7Sjsg  * Typical use::
55*c349dbc7Sjsg  *
56*c349dbc7Sjsg  *	#define for_each_foo_bar(x, y) \'
57*c349dbc7Sjsg  *		list_for_each_entry(x, y->list, head) \'
58*c349dbc7Sjsg  *			for_each_if(x->something == SOMETHING)
59*c349dbc7Sjsg  *
60*c349dbc7Sjsg  * The for_each_if() macro makes the use of for_each_foo_bar() less error
61*c349dbc7Sjsg  * prone.
62*c349dbc7Sjsg  */
63*c349dbc7Sjsg #define for_each_if(condition) if (!(condition)) {} else
64*c349dbc7Sjsg 
65*c349dbc7Sjsg /**
66*c349dbc7Sjsg  * drm_can_sleep - returns true if currently okay to sleep
67*c349dbc7Sjsg  *
68*c349dbc7Sjsg  * This function shall not be used in new code.
69*c349dbc7Sjsg  * The check for running in atomic context may not work - see linux/preempt.h.
70*c349dbc7Sjsg  *
71*c349dbc7Sjsg  * FIXME: All users of drm_can_sleep should be removed (see todo.rst)
72*c349dbc7Sjsg  *
73*c349dbc7Sjsg  * Returns:
74*c349dbc7Sjsg  * False if kgdb is active, we are in atomic context or irqs are disabled.
75*c349dbc7Sjsg  */
drm_can_sleep(void)76*c349dbc7Sjsg static inline bool drm_can_sleep(void)
77*c349dbc7Sjsg {
78*c349dbc7Sjsg 	if (in_atomic() || in_dbg_master() || irqs_disabled())
79*c349dbc7Sjsg 		return false;
80*c349dbc7Sjsg 	return true;
81*c349dbc7Sjsg }
82*c349dbc7Sjsg 
83*c349dbc7Sjsg #endif
84