162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Internal Header for the Direct Rendering Manager
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright 2018 Intel Corporation
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
762306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
862306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
962306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1062306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
1162306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the next
1462306a36Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
1562306a36Sopenharmony_ci * Software.
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1862306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1962306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
2062306a36Sopenharmony_ci * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2162306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2262306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2362306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2462306a36Sopenharmony_ci */
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#ifndef _DRM_UTIL_H_
2762306a36Sopenharmony_ci#define _DRM_UTIL_H_
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/**
3062306a36Sopenharmony_ci * DOC: drm utils
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * Macros and inline functions that does not naturally belong in other places
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#include <linux/interrupt.h>
3662306a36Sopenharmony_ci#include <linux/kgdb.h>
3762306a36Sopenharmony_ci#include <linux/preempt.h>
3862306a36Sopenharmony_ci#include <linux/smp.h>
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/*
4162306a36Sopenharmony_ci * Use EXPORT_SYMBOL_FOR_TESTS_ONLY() for functions that shall
4262306a36Sopenharmony_ci * only be visible for drmselftests.
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_ci#if defined(CONFIG_DRM_EXPORT_FOR_TESTS)
4562306a36Sopenharmony_ci#define EXPORT_SYMBOL_FOR_TESTS_ONLY(x) EXPORT_SYMBOL(x)
4662306a36Sopenharmony_ci#else
4762306a36Sopenharmony_ci#define EXPORT_SYMBOL_FOR_TESTS_ONLY(x)
4862306a36Sopenharmony_ci#endif
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/**
5162306a36Sopenharmony_ci * for_each_if - helper for handling conditionals in various for_each macros
5262306a36Sopenharmony_ci * @condition: The condition to check
5362306a36Sopenharmony_ci *
5462306a36Sopenharmony_ci * Typical use::
5562306a36Sopenharmony_ci *
5662306a36Sopenharmony_ci *	#define for_each_foo_bar(x, y) \'
5762306a36Sopenharmony_ci *		list_for_each_entry(x, y->list, head) \'
5862306a36Sopenharmony_ci *			for_each_if(x->something == SOMETHING)
5962306a36Sopenharmony_ci *
6062306a36Sopenharmony_ci * The for_each_if() macro makes the use of for_each_foo_bar() less error
6162306a36Sopenharmony_ci * prone.
6262306a36Sopenharmony_ci */
6362306a36Sopenharmony_ci#define for_each_if(condition) if (!(condition)) {} else
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/**
6662306a36Sopenharmony_ci * drm_can_sleep - returns true if currently okay to sleep
6762306a36Sopenharmony_ci *
6862306a36Sopenharmony_ci * This function shall not be used in new code.
6962306a36Sopenharmony_ci * The check for running in atomic context may not work - see linux/preempt.h.
7062306a36Sopenharmony_ci *
7162306a36Sopenharmony_ci * FIXME: All users of drm_can_sleep should be removed (see todo.rst)
7262306a36Sopenharmony_ci *
7362306a36Sopenharmony_ci * Returns:
7462306a36Sopenharmony_ci * False if kgdb is active, we are in atomic context or irqs are disabled.
7562306a36Sopenharmony_ci */
7662306a36Sopenharmony_cistatic inline bool drm_can_sleep(void)
7762306a36Sopenharmony_ci{
7862306a36Sopenharmony_ci	if (in_atomic() || in_dbg_master() || irqs_disabled())
7962306a36Sopenharmony_ci		return false;
8062306a36Sopenharmony_ci	return true;
8162306a36Sopenharmony_ci}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci#endif
84