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