18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * SPDX-License-Identifier: MIT
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright © 2019 Intel Corporation
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef __INTEL_CONTEXT_H__
88c2ecf20Sopenharmony_ci#define __INTEL_CONTEXT_H__
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/bitops.h>
118c2ecf20Sopenharmony_ci#include <linux/lockdep.h>
128c2ecf20Sopenharmony_ci#include <linux/types.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "i915_active.h"
158c2ecf20Sopenharmony_ci#include "i915_drv.h"
168c2ecf20Sopenharmony_ci#include "intel_context_types.h"
178c2ecf20Sopenharmony_ci#include "intel_engine_types.h"
188c2ecf20Sopenharmony_ci#include "intel_ring_types.h"
198c2ecf20Sopenharmony_ci#include "intel_timeline_types.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define CE_TRACE(ce, fmt, ...) do {					\
228c2ecf20Sopenharmony_ci	const struct intel_context *ce__ = (ce);			\
238c2ecf20Sopenharmony_ci	ENGINE_TRACE(ce__->engine, "context:%llx " fmt,			\
248c2ecf20Sopenharmony_ci		     ce__->timeline->fence_context,			\
258c2ecf20Sopenharmony_ci		     ##__VA_ARGS__);					\
268c2ecf20Sopenharmony_ci} while (0)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct i915_gem_ww_ctx;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_civoid intel_context_init(struct intel_context *ce,
318c2ecf20Sopenharmony_ci			struct intel_engine_cs *engine);
328c2ecf20Sopenharmony_civoid intel_context_fini(struct intel_context *ce);
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistruct intel_context *
358c2ecf20Sopenharmony_ciintel_context_create(struct intel_engine_cs *engine);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciint intel_context_alloc_state(struct intel_context *ce);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_civoid intel_context_free(struct intel_context *ce);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciint intel_context_reconfigure_sseu(struct intel_context *ce,
428c2ecf20Sopenharmony_ci				   const struct intel_sseu sseu);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/**
458c2ecf20Sopenharmony_ci * intel_context_lock_pinned - Stablises the 'pinned' status of the HW context
468c2ecf20Sopenharmony_ci * @ce - the context
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * Acquire a lock on the pinned status of the HW context, such that the context
498c2ecf20Sopenharmony_ci * can neither be bound to the GPU or unbound whilst the lock is held, i.e.
508c2ecf20Sopenharmony_ci * intel_context_is_pinned() remains stable.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistatic inline int intel_context_lock_pinned(struct intel_context *ce)
538c2ecf20Sopenharmony_ci	__acquires(ce->pin_mutex)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	return mutex_lock_interruptible(&ce->pin_mutex);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/**
598c2ecf20Sopenharmony_ci * intel_context_is_pinned - Reports the 'pinned' status
608c2ecf20Sopenharmony_ci * @ce - the context
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * While in use by the GPU, the context, along with its ring and page
638c2ecf20Sopenharmony_ci * tables is pinned into memory and the GTT.
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci * Returns: true if the context is currently pinned for use by the GPU.
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_cistatic inline bool
688c2ecf20Sopenharmony_ciintel_context_is_pinned(struct intel_context *ce)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	return atomic_read(&ce->pin_count);
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/**
748c2ecf20Sopenharmony_ci * intel_context_unlock_pinned - Releases the earlier locking of 'pinned' status
758c2ecf20Sopenharmony_ci * @ce - the context
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * Releases the lock earlier acquired by intel_context_unlock_pinned().
788c2ecf20Sopenharmony_ci */
798c2ecf20Sopenharmony_cistatic inline void intel_context_unlock_pinned(struct intel_context *ce)
808c2ecf20Sopenharmony_ci	__releases(ce->pin_mutex)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	mutex_unlock(&ce->pin_mutex);
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciint __intel_context_do_pin(struct intel_context *ce);
868c2ecf20Sopenharmony_ciint __intel_context_do_pin_ww(struct intel_context *ce,
878c2ecf20Sopenharmony_ci			      struct i915_gem_ww_ctx *ww);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic inline bool intel_context_pin_if_active(struct intel_context *ce)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	return atomic_inc_not_zero(&ce->pin_count);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic inline int intel_context_pin(struct intel_context *ce)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	if (likely(intel_context_pin_if_active(ce)))
978c2ecf20Sopenharmony_ci		return 0;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	return __intel_context_do_pin(ce);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic inline int intel_context_pin_ww(struct intel_context *ce,
1038c2ecf20Sopenharmony_ci				       struct i915_gem_ww_ctx *ww)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	if (likely(intel_context_pin_if_active(ce)))
1068c2ecf20Sopenharmony_ci		return 0;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return __intel_context_do_pin_ww(ce, ww);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic inline void __intel_context_pin(struct intel_context *ce)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	GEM_BUG_ON(!intel_context_is_pinned(ce));
1148c2ecf20Sopenharmony_ci	atomic_inc(&ce->pin_count);
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_civoid intel_context_unpin(struct intel_context *ce);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_civoid intel_context_enter_engine(struct intel_context *ce);
1208c2ecf20Sopenharmony_civoid intel_context_exit_engine(struct intel_context *ce);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic inline void intel_context_enter(struct intel_context *ce)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	lockdep_assert_held(&ce->timeline->mutex);
1258c2ecf20Sopenharmony_ci	if (!ce->active_count++)
1268c2ecf20Sopenharmony_ci		ce->ops->enter(ce);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic inline void intel_context_mark_active(struct intel_context *ce)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	lockdep_assert_held(&ce->timeline->mutex);
1328c2ecf20Sopenharmony_ci	++ce->active_count;
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic inline void intel_context_exit(struct intel_context *ce)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	lockdep_assert_held(&ce->timeline->mutex);
1388c2ecf20Sopenharmony_ci	GEM_BUG_ON(!ce->active_count);
1398c2ecf20Sopenharmony_ci	if (!--ce->active_count)
1408c2ecf20Sopenharmony_ci		ce->ops->exit(ce);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic inline struct intel_context *intel_context_get(struct intel_context *ce)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	kref_get(&ce->ref);
1468c2ecf20Sopenharmony_ci	return ce;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic inline void intel_context_put(struct intel_context *ce)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	kref_put(&ce->ref, ce->ops->destroy);
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic inline struct intel_timeline *__must_check
1558c2ecf20Sopenharmony_ciintel_context_timeline_lock(struct intel_context *ce)
1568c2ecf20Sopenharmony_ci	__acquires(&ce->timeline->mutex)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	struct intel_timeline *tl = ce->timeline;
1598c2ecf20Sopenharmony_ci	int err;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	err = mutex_lock_interruptible(&tl->mutex);
1628c2ecf20Sopenharmony_ci	if (err)
1638c2ecf20Sopenharmony_ci		return ERR_PTR(err);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	return tl;
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic inline void intel_context_timeline_unlock(struct intel_timeline *tl)
1698c2ecf20Sopenharmony_ci	__releases(&tl->mutex)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	mutex_unlock(&tl->mutex);
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ciint intel_context_prepare_remote_request(struct intel_context *ce,
1758c2ecf20Sopenharmony_ci					 struct i915_request *rq);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistruct i915_request *intel_context_create_request(struct intel_context *ce);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic inline struct intel_ring *__intel_context_ring_size(u64 sz)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	return u64_to_ptr(struct intel_ring, sz);
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic inline bool intel_context_is_barrier(const struct intel_context *ce)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	return test_bit(CONTEXT_BARRIER_BIT, &ce->flags);
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic inline bool intel_context_is_closed(const struct intel_context *ce)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	return test_bit(CONTEXT_CLOSED_BIT, &ce->flags);
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic inline bool intel_context_use_semaphores(const struct intel_context *ce)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	return test_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic inline void intel_context_set_use_semaphores(struct intel_context *ce)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	set_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic inline void intel_context_clear_use_semaphores(struct intel_context *ce)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	clear_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic inline bool intel_context_is_banned(const struct intel_context *ce)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	return test_bit(CONTEXT_BANNED, &ce->flags);
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic inline bool intel_context_set_banned(struct intel_context *ce)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	return test_and_set_bit(CONTEXT_BANNED, &ce->flags);
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic inline bool
2208c2ecf20Sopenharmony_ciintel_context_force_single_submission(const struct intel_context *ce)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ce->flags);
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic inline void
2268c2ecf20Sopenharmony_ciintel_context_set_single_submission(struct intel_context *ce)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	__set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ce->flags);
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic inline bool
2328c2ecf20Sopenharmony_ciintel_context_nopreempt(const struct intel_context *ce)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	return test_bit(CONTEXT_NOPREEMPT, &ce->flags);
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic inline void
2388c2ecf20Sopenharmony_ciintel_context_set_nopreempt(struct intel_context *ce)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	set_bit(CONTEXT_NOPREEMPT, &ce->flags);
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic inline void
2448c2ecf20Sopenharmony_ciintel_context_clear_nopreempt(struct intel_context *ce)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	clear_bit(CONTEXT_NOPREEMPT, &ce->flags);
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic inline u64 intel_context_get_total_runtime_ns(struct intel_context *ce)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	const u32 period =
2528c2ecf20Sopenharmony_ci		RUNTIME_INFO(ce->engine->i915)->cs_timestamp_period_ns;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	return READ_ONCE(ce->runtime.total) * period;
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic inline u64 intel_context_get_avg_runtime_ns(struct intel_context *ce)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	const u32 period =
2608c2ecf20Sopenharmony_ci		RUNTIME_INFO(ce->engine->i915)->cs_timestamp_period_ns;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	return mul_u32_u32(ewma_runtime_read(&ce->runtime.avg), period);
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci#endif /* __INTEL_CONTEXT_H__ */
266