18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright © 2016 Intel Corporation
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the next
128c2ecf20Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
138c2ecf20Sopenharmony_ci * Software.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
188c2ecf20Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
208c2ecf20Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
218c2ecf20Sopenharmony_ci * IN THE SOFTWARE.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#ifndef __I915_GEM_H__
268c2ecf20Sopenharmony_ci#define __I915_GEM_H__
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <linux/bug.h>
298c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <drm/drm_drv.h>
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include "i915_utils.h"
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistruct drm_i915_private;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#ifdef CONFIG_DRM_I915_DEBUG_GEM
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define GEM_SHOW_DEBUG() drm_debug_enabled(DRM_UT_DRIVER)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define GEM_BUG_ON(condition) do { if (unlikely((condition))) {	\
428c2ecf20Sopenharmony_ci		GEM_TRACE_ERR("%s:%d GEM_BUG_ON(%s)\n", \
438c2ecf20Sopenharmony_ci			      __func__, __LINE__, __stringify(condition)); \
448c2ecf20Sopenharmony_ci		GEM_TRACE_DUMP(); \
458c2ecf20Sopenharmony_ci		BUG(); \
468c2ecf20Sopenharmony_ci		} \
478c2ecf20Sopenharmony_ci	} while(0)
488c2ecf20Sopenharmony_ci#define GEM_WARN_ON(expr) WARN_ON(expr)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define GEM_DEBUG_DECL(var) var
518c2ecf20Sopenharmony_ci#define GEM_DEBUG_EXEC(expr) expr
528c2ecf20Sopenharmony_ci#define GEM_DEBUG_BUG_ON(expr) GEM_BUG_ON(expr)
538c2ecf20Sopenharmony_ci#define GEM_DEBUG_WARN_ON(expr) GEM_WARN_ON(expr)
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#else
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define GEM_SHOW_DEBUG() (0)
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define GEM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
608c2ecf20Sopenharmony_ci#define GEM_WARN_ON(expr) ({ unlikely(!!(expr)); })
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define GEM_DEBUG_DECL(var)
638c2ecf20Sopenharmony_ci#define GEM_DEBUG_EXEC(expr) do { } while (0)
648c2ecf20Sopenharmony_ci#define GEM_DEBUG_BUG_ON(expr)
658c2ecf20Sopenharmony_ci#define GEM_DEBUG_WARN_ON(expr) ({ BUILD_BUG_ON_INVALID(expr); 0; })
668c2ecf20Sopenharmony_ci#endif
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_DRM_I915_TRACE_GEM)
698c2ecf20Sopenharmony_ci#define GEM_TRACE(...) trace_printk(__VA_ARGS__)
708c2ecf20Sopenharmony_ci#define GEM_TRACE_ERR(...) do {						\
718c2ecf20Sopenharmony_ci	pr_err(__VA_ARGS__);						\
728c2ecf20Sopenharmony_ci	trace_printk(__VA_ARGS__);					\
738c2ecf20Sopenharmony_ci} while (0)
748c2ecf20Sopenharmony_ci#define GEM_TRACE_DUMP() \
758c2ecf20Sopenharmony_ci	do { ftrace_dump(DUMP_ALL); __add_taint_for_CI(TAINT_WARN); } while (0)
768c2ecf20Sopenharmony_ci#define GEM_TRACE_DUMP_ON(expr) \
778c2ecf20Sopenharmony_ci	do { if (expr) GEM_TRACE_DUMP(); } while (0)
788c2ecf20Sopenharmony_ci#else
798c2ecf20Sopenharmony_ci#define GEM_TRACE(...) do { } while (0)
808c2ecf20Sopenharmony_ci#define GEM_TRACE_ERR(...) do { } while (0)
818c2ecf20Sopenharmony_ci#define GEM_TRACE_DUMP() do { } while (0)
828c2ecf20Sopenharmony_ci#define GEM_TRACE_DUMP_ON(expr) BUILD_BUG_ON_INVALID(expr)
838c2ecf20Sopenharmony_ci#endif
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define I915_GEM_IDLE_TIMEOUT (HZ / 5)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic inline void tasklet_lock(struct tasklet_struct *t)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	while (!tasklet_trylock(t))
908c2ecf20Sopenharmony_ci		cpu_relax();
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic inline bool tasklet_is_locked(const struct tasklet_struct *t)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	return test_bit(TASKLET_STATE_RUN, &t->state);
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic inline void __tasklet_disable_sync_once(struct tasklet_struct *t)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	if (!atomic_fetch_inc(&t->count))
1018c2ecf20Sopenharmony_ci		tasklet_unlock_wait(t);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic inline bool __tasklet_is_enabled(const struct tasklet_struct *t)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	return !atomic_read(&t->count);
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic inline bool __tasklet_enable(struct tasklet_struct *t)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	return atomic_dec_and_test(&t->count);
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic inline bool __tasklet_is_scheduled(struct tasklet_struct *t)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	return test_bit(TASKLET_STATE_SCHED, &t->state);
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistruct i915_gem_ww_ctx {
1208c2ecf20Sopenharmony_ci	struct ww_acquire_ctx ctx;
1218c2ecf20Sopenharmony_ci	struct list_head obj_list;
1228c2ecf20Sopenharmony_ci	bool intr;
1238c2ecf20Sopenharmony_ci	struct drm_i915_gem_object *contended;
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_civoid i915_gem_ww_ctx_init(struct i915_gem_ww_ctx *ctx, bool intr);
1278c2ecf20Sopenharmony_civoid i915_gem_ww_ctx_fini(struct i915_gem_ww_ctx *ctx);
1288c2ecf20Sopenharmony_ciint __must_check i915_gem_ww_ctx_backoff(struct i915_gem_ww_ctx *ctx);
1298c2ecf20Sopenharmony_civoid i915_gem_ww_unlock_single(struct drm_i915_gem_object *obj);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci#endif /* __I915_GEM_H__ */
132