18c2ecf20Sopenharmony_ci/**************************************************************************
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
48c2ecf20Sopenharmony_ci * All Rights Reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
78c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the
88c2ecf20Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
98c2ecf20Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
108c2ecf20Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
118c2ecf20Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
128c2ecf20Sopenharmony_ci * the following conditions:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the
158c2ecf20Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
168c2ecf20Sopenharmony_ci * of the Software.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
198c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
208c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
218c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
228c2ecf20Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
238c2ecf20Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
248c2ecf20Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci **************************************************************************/
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/** @file ttm_lock.h
328c2ecf20Sopenharmony_ci * This file implements a simple replacement for the buffer manager use
338c2ecf20Sopenharmony_ci * of the DRM heavyweight hardware lock.
348c2ecf20Sopenharmony_ci * The lock is a read-write lock. Taking it in read mode and write mode
358c2ecf20Sopenharmony_ci * is relatively fast, and intended for in-kernel use only.
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * The vt mode is used only when there is a need to block all
388c2ecf20Sopenharmony_ci * user-space processes from validating buffers.
398c2ecf20Sopenharmony_ci * It's allowed to leave kernel space with the vt lock held.
408c2ecf20Sopenharmony_ci * If a user-space process dies while having the vt-lock,
418c2ecf20Sopenharmony_ci * it will be released during the file descriptor release. The vt lock
428c2ecf20Sopenharmony_ci * excludes write lock and read lock.
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * The suspend mode is used to lock out all TTM users when preparing for
458c2ecf20Sopenharmony_ci * and executing suspend operations.
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#ifndef _TTM_LOCK_H_
508c2ecf20Sopenharmony_ci#define _TTM_LOCK_H_
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#include <linux/atomic.h>
538c2ecf20Sopenharmony_ci#include <linux/wait.h>
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#include "ttm_object.h"
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/**
588c2ecf20Sopenharmony_ci * struct ttm_lock
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci * @base: ttm base object used solely to release the lock if the client
618c2ecf20Sopenharmony_ci * holding the lock dies.
628c2ecf20Sopenharmony_ci * @queue: Queue for processes waiting for lock change-of-status.
638c2ecf20Sopenharmony_ci * @lock: Spinlock protecting some lock members.
648c2ecf20Sopenharmony_ci * @rw: Read-write lock counter. Protected by @lock.
658c2ecf20Sopenharmony_ci * @flags: Lock state. Protected by @lock.
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistruct ttm_lock {
698c2ecf20Sopenharmony_ci	struct ttm_base_object base;
708c2ecf20Sopenharmony_ci	wait_queue_head_t queue;
718c2ecf20Sopenharmony_ci	spinlock_t lock;
728c2ecf20Sopenharmony_ci	int32_t rw;
738c2ecf20Sopenharmony_ci	uint32_t flags;
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci/**
788c2ecf20Sopenharmony_ci * ttm_lock_init
798c2ecf20Sopenharmony_ci *
808c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
818c2ecf20Sopenharmony_ci * Initializes the lock.
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_ciextern void ttm_lock_init(struct ttm_lock *lock);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/**
868c2ecf20Sopenharmony_ci * ttm_read_unlock
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
898c2ecf20Sopenharmony_ci *
908c2ecf20Sopenharmony_ci * Releases a read lock.
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_ciextern void ttm_read_unlock(struct ttm_lock *lock);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/**
958c2ecf20Sopenharmony_ci * ttm_read_lock
968c2ecf20Sopenharmony_ci *
978c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
988c2ecf20Sopenharmony_ci * @interruptible: Interruptible sleeping while waiting for a lock.
998c2ecf20Sopenharmony_ci *
1008c2ecf20Sopenharmony_ci * Takes the lock in read mode.
1018c2ecf20Sopenharmony_ci * Returns:
1028c2ecf20Sopenharmony_ci * -ERESTARTSYS If interrupted by a signal and interruptible is true.
1038c2ecf20Sopenharmony_ci */
1048c2ecf20Sopenharmony_ciextern int ttm_read_lock(struct ttm_lock *lock, bool interruptible);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/**
1078c2ecf20Sopenharmony_ci * ttm_read_trylock
1088c2ecf20Sopenharmony_ci *
1098c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
1108c2ecf20Sopenharmony_ci * @interruptible: Interruptible sleeping while waiting for a lock.
1118c2ecf20Sopenharmony_ci *
1128c2ecf20Sopenharmony_ci * Tries to take the lock in read mode. If the lock is already held
1138c2ecf20Sopenharmony_ci * in write mode, the function will return -EBUSY. If the lock is held
1148c2ecf20Sopenharmony_ci * in vt or suspend mode, the function will sleep until these modes
1158c2ecf20Sopenharmony_ci * are unlocked.
1168c2ecf20Sopenharmony_ci *
1178c2ecf20Sopenharmony_ci * Returns:
1188c2ecf20Sopenharmony_ci * -EBUSY The lock was already held in write mode.
1198c2ecf20Sopenharmony_ci * -ERESTARTSYS If interrupted by a signal and interruptible is true.
1208c2ecf20Sopenharmony_ci */
1218c2ecf20Sopenharmony_ciextern int ttm_read_trylock(struct ttm_lock *lock, bool interruptible);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/**
1248c2ecf20Sopenharmony_ci * ttm_write_unlock
1258c2ecf20Sopenharmony_ci *
1268c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
1278c2ecf20Sopenharmony_ci *
1288c2ecf20Sopenharmony_ci * Releases a write lock.
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_ciextern void ttm_write_unlock(struct ttm_lock *lock);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/**
1338c2ecf20Sopenharmony_ci * ttm_write_lock
1348c2ecf20Sopenharmony_ci *
1358c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
1368c2ecf20Sopenharmony_ci * @interruptible: Interruptible sleeping while waiting for a lock.
1378c2ecf20Sopenharmony_ci *
1388c2ecf20Sopenharmony_ci * Takes the lock in write mode.
1398c2ecf20Sopenharmony_ci * Returns:
1408c2ecf20Sopenharmony_ci * -ERESTARTSYS If interrupted by a signal and interruptible is true.
1418c2ecf20Sopenharmony_ci */
1428c2ecf20Sopenharmony_ciextern int ttm_write_lock(struct ttm_lock *lock, bool interruptible);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/**
1458c2ecf20Sopenharmony_ci * ttm_lock_downgrade
1468c2ecf20Sopenharmony_ci *
1478c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
1488c2ecf20Sopenharmony_ci *
1498c2ecf20Sopenharmony_ci * Downgrades a write lock to a read lock.
1508c2ecf20Sopenharmony_ci */
1518c2ecf20Sopenharmony_ciextern void ttm_lock_downgrade(struct ttm_lock *lock);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/**
1548c2ecf20Sopenharmony_ci * ttm_suspend_lock
1558c2ecf20Sopenharmony_ci *
1568c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci * Takes the lock in suspend mode. Excludes read and write mode.
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_ciextern void ttm_suspend_lock(struct ttm_lock *lock);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci/**
1638c2ecf20Sopenharmony_ci * ttm_suspend_unlock
1648c2ecf20Sopenharmony_ci *
1658c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
1668c2ecf20Sopenharmony_ci *
1678c2ecf20Sopenharmony_ci * Releases a suspend lock
1688c2ecf20Sopenharmony_ci */
1698c2ecf20Sopenharmony_ciextern void ttm_suspend_unlock(struct ttm_lock *lock);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci/**
1728c2ecf20Sopenharmony_ci * ttm_vt_lock
1738c2ecf20Sopenharmony_ci *
1748c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
1758c2ecf20Sopenharmony_ci * @interruptible: Interruptible sleeping while waiting for a lock.
1768c2ecf20Sopenharmony_ci * @tfile: Pointer to a struct ttm_object_file to register the lock with.
1778c2ecf20Sopenharmony_ci *
1788c2ecf20Sopenharmony_ci * Takes the lock in vt mode.
1798c2ecf20Sopenharmony_ci * Returns:
1808c2ecf20Sopenharmony_ci * -ERESTARTSYS If interrupted by a signal and interruptible is true.
1818c2ecf20Sopenharmony_ci * -ENOMEM: Out of memory when locking.
1828c2ecf20Sopenharmony_ci */
1838c2ecf20Sopenharmony_ciextern int ttm_vt_lock(struct ttm_lock *lock, bool interruptible,
1848c2ecf20Sopenharmony_ci		       struct ttm_object_file *tfile);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci/**
1878c2ecf20Sopenharmony_ci * ttm_vt_unlock
1888c2ecf20Sopenharmony_ci *
1898c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
1908c2ecf20Sopenharmony_ci *
1918c2ecf20Sopenharmony_ci * Releases a vt lock.
1928c2ecf20Sopenharmony_ci * Returns:
1938c2ecf20Sopenharmony_ci * -EINVAL If the lock was not held.
1948c2ecf20Sopenharmony_ci */
1958c2ecf20Sopenharmony_ciextern int ttm_vt_unlock(struct ttm_lock *lock);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/**
1988c2ecf20Sopenharmony_ci * ttm_write_unlock
1998c2ecf20Sopenharmony_ci *
2008c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
2018c2ecf20Sopenharmony_ci *
2028c2ecf20Sopenharmony_ci * Releases a write lock.
2038c2ecf20Sopenharmony_ci */
2048c2ecf20Sopenharmony_ciextern void ttm_write_unlock(struct ttm_lock *lock);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci/**
2078c2ecf20Sopenharmony_ci * ttm_write_lock
2088c2ecf20Sopenharmony_ci *
2098c2ecf20Sopenharmony_ci * @lock: Pointer to a struct ttm_lock
2108c2ecf20Sopenharmony_ci * @interruptible: Interruptible sleeping while waiting for a lock.
2118c2ecf20Sopenharmony_ci *
2128c2ecf20Sopenharmony_ci * Takes the lock in write mode.
2138c2ecf20Sopenharmony_ci * Returns:
2148c2ecf20Sopenharmony_ci * -ERESTARTSYS If interrupted by a signal and interruptible is true.
2158c2ecf20Sopenharmony_ci */
2168c2ecf20Sopenharmony_ciextern int ttm_write_lock(struct ttm_lock *lock, bool interruptible);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci#endif
219