/*
 * SPDX-License-Identifier: MIT
 *
 * Copyright © 2019 Intel Corporation
 */

#include <linux/wait_bit.h>

#include "intel_runtime_pm.h"
#include "intel_wakeref.h"

int __intel_wakeref_get_first(struct intel_wakeref *wf)
{
	intel_wakeref_t wakeref = intel_runtime_pm_get(wf->rpm);
	int err = 0;

	/*
	 * Treat get/put as different subclasses, as we may need to run
	 * the put callback from under the shrinker and do not want to
	 * cross-contanimate that callback with any extra work performed
	 * upon acquiring the wakeref.
	 */
	mutex_lock_nested(&wf->mutex, SINGLE_DEPTH_NESTING);

	if (likely(!atomic_read(&wf->count))) {
		INTEL_WAKEREF_BUG_ON(wf->wakeref);
		wf->wakeref = fetch_and_zero(&wakeref);

		err = wf->ops->get(wf);
		if (unlikely(err)) {
			wakeref = xchg(&wf->wakeref, 0);
			wake_up_var(&wf->wakeref);
			goto unlock;
		}

		smp_mb__before_atomic(); /* release wf->count */
	}
	atomic_inc(&wf->count);
	INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);

unlock:
	mutex_unlock(&wf->mutex);
	if (unlikely(wakeref))
		intel_runtime_pm_put(wf->rpm, wakeref);

	return err;
}

static void ____intel_wakeref_put_last(struct intel_wakeref *wf)
{
	intel_wakeref_t wakeref = 0;

	INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
	if (unlikely(!atomic_dec_and_test(&wf->count)))
		goto unlock;

	/* ops->put() must reschedule its own release on error/deferral */
	if (likely(!wf->ops->put(wf))) {
		INTEL_WAKEREF_BUG_ON(!wf->wakeref);
		wakeref = xchg(&wf->wakeref, 0);
		wake_up_var(&wf->wakeref);
#if IS_ENABLED(CPTCFG_DRM_I915_DEBUG_WAKEREF)
		ref_tracker_dir_exit(&wf->debug);
#endif
	}

unlock:
	mutex_unlock(&wf->mutex);
	if (wakeref)
		intel_runtime_pm_put(wf->rpm, wakeref);
}

void __intel_wakeref_put_last(struct intel_wakeref *wf, unsigned long flags)
{
	INTEL_WAKEREF_BUG_ON(delayed_work_pending(&wf->work));

	/* Assume we are not in process context and so cannot sleep. */
	if (flags & INTEL_WAKEREF_PUT_ASYNC || !mutex_trylock(&wf->mutex)) {
		mod_delayed_work(system_wq, &wf->work,
				 FIELD_GET(INTEL_WAKEREF_PUT_DELAY, flags));
		return;
	}

	____intel_wakeref_put_last(wf);
}

static void __intel_wakeref_put_work(struct work_struct *wrk)
{
	struct intel_wakeref *wf = container_of(wrk, typeof(*wf), work.work);

	if (atomic_add_unless(&wf->count, -1, 1))
		return;

	mutex_lock(&wf->mutex);
	____intel_wakeref_put_last(wf);
}

void __intel_wakeref_init(struct intel_wakeref *wf,
			  struct intel_runtime_pm *rpm,
			  const struct intel_wakeref_ops *ops,
			  struct intel_wakeref_lockclass *key,
			  const char *name)
{
	wf->rpm = rpm;
	wf->ops = ops;

	__mutex_init(&wf->mutex, "wakeref.mutex", &key->mutex);
	atomic_set(&wf->count, 0);
	wf->wakeref = 0;

	INIT_DELAYED_WORK(&wf->work, __intel_wakeref_put_work);
	lockdep_init_map(&wf->work.work.lockdep_map,
			 "wakeref.work", &key->work, 0);

#if IS_ENABLED(CPTCFG_DRM_I915_DEBUG_WAKEREF)
	ref_tracker_dir_init(&wf->debug, INTEL_REFTRACK_DEAD_COUNT, name);
#endif
}

int intel_wakeref_wait_for_idle(struct intel_wakeref *wf)
{
	int err;

	might_sleep();

	/* Beware re-arming wakerefs; recheck after flushing the callback */
	do {
		err = wait_var_event_killable(&wf->wakeref,
					      !intel_wakeref_is_active(wf));
		if (err)
			return err;

		intel_wakeref_unlock_wait(wf);
	} while (intel_wakeref_is_active(wf));

	return 0;
}
