xref: /linux/drivers/gpu/drm/drm_lease.c (revision 62884cd386b876638720ef88374b31a84ca7ee5f)
12ed077e4SKeith Packard /*
22ed077e4SKeith Packard  * Copyright © 2017 Keith Packard <keithp@keithp.com>
32ed077e4SKeith Packard  *
42ed077e4SKeith Packard  * This program is free software; you can redistribute it and/or modify
52ed077e4SKeith Packard  * it under the terms of the GNU General Public License as published by
62ed077e4SKeith Packard  * the Free Software Foundation, either version 2 of the License, or
72ed077e4SKeith Packard  * (at your option) any later version.
82ed077e4SKeith Packard  *
92ed077e4SKeith Packard  * This program is distributed in the hope that it will be useful, but
102ed077e4SKeith Packard  * WITHOUT ANY WARRANTY; without even the implied warranty of
112ed077e4SKeith Packard  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
122ed077e4SKeith Packard  * General Public License for more details.
132ed077e4SKeith Packard  */
142ed077e4SKeith Packard 
152ed077e4SKeith Packard #include <drm/drmP.h>
162ed077e4SKeith Packard #include "drm_internal.h"
172ed077e4SKeith Packard #include "drm_legacy.h"
182ed077e4SKeith Packard #include "drm_crtc_internal.h"
192ed077e4SKeith Packard #include <drm/drm_lease.h>
202ed077e4SKeith Packard #include <drm/drm_auth.h>
212ed077e4SKeith Packard #include <drm/drm_crtc_helper.h>
222ed077e4SKeith Packard 
232ed077e4SKeith Packard #define drm_for_each_lessee(lessee, lessor) \
242ed077e4SKeith Packard 	list_for_each_entry((lessee), &(lessor)->lessees, lessee_list)
252ed077e4SKeith Packard 
26*62884cd3SKeith Packard static uint64_t drm_lease_idr_object;
27*62884cd3SKeith Packard 
282ed077e4SKeith Packard /**
292ed077e4SKeith Packard  * drm_lease_owner - return ancestor owner drm_master
302ed077e4SKeith Packard  * @master: drm_master somewhere within tree of lessees and lessors
312ed077e4SKeith Packard  *
322ed077e4SKeith Packard  * RETURN:
332ed077e4SKeith Packard  *
342ed077e4SKeith Packard  * drm_master at the top of the tree (i.e, with lessor NULL
352ed077e4SKeith Packard  */
362ed077e4SKeith Packard struct drm_master *drm_lease_owner(struct drm_master *master)
372ed077e4SKeith Packard {
382ed077e4SKeith Packard 	while (master->lessor != NULL)
392ed077e4SKeith Packard 		master = master->lessor;
402ed077e4SKeith Packard 	return master;
412ed077e4SKeith Packard }
422ed077e4SKeith Packard EXPORT_SYMBOL(drm_lease_owner);
432ed077e4SKeith Packard 
442ed077e4SKeith Packard /**
452ed077e4SKeith Packard  * _drm_find_lessee - find lessee by id (idr_mutex held)
462ed077e4SKeith Packard  * @master: drm_master of lessor
472ed077e4SKeith Packard  * @id: lessee_id
482ed077e4SKeith Packard  *
492ed077e4SKeith Packard  * RETURN:
502ed077e4SKeith Packard  *
512ed077e4SKeith Packard  * drm_master of the lessee if valid, NULL otherwise
522ed077e4SKeith Packard  */
532ed077e4SKeith Packard 
542ed077e4SKeith Packard static struct drm_master*
552ed077e4SKeith Packard _drm_find_lessee(struct drm_master *master, int lessee_id)
562ed077e4SKeith Packard {
572ed077e4SKeith Packard 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
582ed077e4SKeith Packard 	return idr_find(&drm_lease_owner(master)->lessee_idr, lessee_id);
592ed077e4SKeith Packard }
602ed077e4SKeith Packard 
612ed077e4SKeith Packard /**
622ed077e4SKeith Packard  * _drm_lease_held_master - check to see if an object is leased (or owned) by master (idr_mutex held)
632ed077e4SKeith Packard  * @master: the master to check the lease status of
642ed077e4SKeith Packard  * @id: the id to check
652ed077e4SKeith Packard  *
662ed077e4SKeith Packard  * Checks if the specified master holds a lease on the object. Return
672ed077e4SKeith Packard  * value:
682ed077e4SKeith Packard  *
692ed077e4SKeith Packard  *	true		'master' holds a lease on (or owns) the object
702ed077e4SKeith Packard  *	false		'master' does not hold a lease.
712ed077e4SKeith Packard  */
722ed077e4SKeith Packard static int _drm_lease_held_master(struct drm_master *master, int id)
732ed077e4SKeith Packard {
742ed077e4SKeith Packard 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
752ed077e4SKeith Packard 	if (master->lessor)
762ed077e4SKeith Packard 		return idr_find(&master->leases, id) != NULL;
772ed077e4SKeith Packard 	return true;
782ed077e4SKeith Packard }
792ed077e4SKeith Packard 
802ed077e4SKeith Packard /**
812ed077e4SKeith Packard  * _drm_has_leased - check to see if an object has been leased (idr_mutex held)
822ed077e4SKeith Packard  * @master: the master to check the lease status of
832ed077e4SKeith Packard  * @id: the id to check
842ed077e4SKeith Packard  *
852ed077e4SKeith Packard  * Checks if any lessee of 'master' holds a lease on 'id'. Return
862ed077e4SKeith Packard  * value:
872ed077e4SKeith Packard  *
882ed077e4SKeith Packard  *	true		Some lessee holds a lease on the object.
892ed077e4SKeith Packard  *	false		No lessee has a lease on the object.
902ed077e4SKeith Packard  */
912ed077e4SKeith Packard static bool _drm_has_leased(struct drm_master *master, int id)
922ed077e4SKeith Packard {
932ed077e4SKeith Packard 	struct drm_master *lessee;
942ed077e4SKeith Packard 
952ed077e4SKeith Packard 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
962ed077e4SKeith Packard 	drm_for_each_lessee(lessee, master)
972ed077e4SKeith Packard 		if (_drm_lease_held_master(lessee, id))
982ed077e4SKeith Packard 			return true;
992ed077e4SKeith Packard 	return false;
1002ed077e4SKeith Packard }
1012ed077e4SKeith Packard 
1022ed077e4SKeith Packard /**
1032ed077e4SKeith Packard  * _drm_lease_held - check drm_mode_object lease status (idr_mutex held)
1042ed077e4SKeith Packard  * @master: the drm_master
1052ed077e4SKeith Packard  * @id: the object id
1062ed077e4SKeith Packard  *
1072ed077e4SKeith Packard  * Checks if the specified master holds a lease on the object. Return
1082ed077e4SKeith Packard  * value:
1092ed077e4SKeith Packard  *
1102ed077e4SKeith Packard  *	true		'master' holds a lease on (or owns) the object
1112ed077e4SKeith Packard  *	false		'master' does not hold a lease.
1122ed077e4SKeith Packard  */
1132ed077e4SKeith Packard bool _drm_lease_held(struct drm_file *file_priv, int id)
1142ed077e4SKeith Packard {
1152ed077e4SKeith Packard 	if (file_priv == NULL || file_priv->master == NULL)
1162ed077e4SKeith Packard 		return true;
1172ed077e4SKeith Packard 
1182ed077e4SKeith Packard 	return _drm_lease_held_master(file_priv->master, id);
1192ed077e4SKeith Packard }
1202ed077e4SKeith Packard EXPORT_SYMBOL(_drm_lease_held);
1212ed077e4SKeith Packard 
1222ed077e4SKeith Packard /**
1232ed077e4SKeith Packard  * drm_lease_held - check drm_mode_object lease status (idr_mutex not held)
1242ed077e4SKeith Packard  * @master: the drm_master
1252ed077e4SKeith Packard  * @id: the object id
1262ed077e4SKeith Packard  *
1272ed077e4SKeith Packard  * Checks if the specified master holds a lease on the object. Return
1282ed077e4SKeith Packard  * value:
1292ed077e4SKeith Packard  *
1302ed077e4SKeith Packard  *	true		'master' holds a lease on (or owns) the object
1312ed077e4SKeith Packard  *	false		'master' does not hold a lease.
1322ed077e4SKeith Packard  */
1332ed077e4SKeith Packard bool drm_lease_held(struct drm_file *file_priv, int id)
1342ed077e4SKeith Packard {
1352ed077e4SKeith Packard 	struct drm_master *master;
1362ed077e4SKeith Packard 	bool ret;
1372ed077e4SKeith Packard 
1382ed077e4SKeith Packard 	if (file_priv == NULL || file_priv->master == NULL)
1392ed077e4SKeith Packard 		return true;
1402ed077e4SKeith Packard 
1412ed077e4SKeith Packard 	master = file_priv->master;
1422ed077e4SKeith Packard 	mutex_lock(&master->dev->mode_config.idr_mutex);
1432ed077e4SKeith Packard 	ret = _drm_lease_held_master(master, id);
1442ed077e4SKeith Packard 	mutex_unlock(&master->dev->mode_config.idr_mutex);
1452ed077e4SKeith Packard 	return ret;
1462ed077e4SKeith Packard }
1472ed077e4SKeith Packard EXPORT_SYMBOL(drm_lease_held);
1482ed077e4SKeith Packard 
1492ed077e4SKeith Packard /**
1502ed077e4SKeith Packard  * drm_lease_filter_crtcs - restricted crtc set to leased values (idr_mutex not held)
1512ed077e4SKeith Packard  * @file_priv: requestor file
1522ed077e4SKeith Packard  * @crtcs: bitmask of crtcs to check
1532ed077e4SKeith Packard  *
1542ed077e4SKeith Packard  * Reconstructs a crtc mask based on the crtcs which are visible
1552ed077e4SKeith Packard  * through the specified file.
1562ed077e4SKeith Packard  */
1572ed077e4SKeith Packard uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in)
1582ed077e4SKeith Packard {
1592ed077e4SKeith Packard 	struct drm_master *master;
1602ed077e4SKeith Packard 	struct drm_device *dev;
1612ed077e4SKeith Packard 	struct drm_crtc *crtc;
1622ed077e4SKeith Packard 	int count_in, count_out;
1632ed077e4SKeith Packard 	uint32_t crtcs_out = 0;
1642ed077e4SKeith Packard 
1652ed077e4SKeith Packard 	if (file_priv == NULL || file_priv->master == NULL)
1662ed077e4SKeith Packard 		return crtcs_in;
1672ed077e4SKeith Packard 
1682ed077e4SKeith Packard 	master = file_priv->master;
1692ed077e4SKeith Packard 	dev = master->dev;
1702ed077e4SKeith Packard 
1712ed077e4SKeith Packard 	count_in = count_out = 0;
1722ed077e4SKeith Packard 	mutex_lock(&master->dev->mode_config.idr_mutex);
1732ed077e4SKeith Packard 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1742ed077e4SKeith Packard 		if (_drm_lease_held_master(master, crtc->base.id)) {
1752ed077e4SKeith Packard 			uint32_t mask_in = 1ul << count_in;
1762ed077e4SKeith Packard 			if ((crtcs_in & mask_in) != 0) {
1772ed077e4SKeith Packard 				uint32_t mask_out = 1ul << count_out;
1782ed077e4SKeith Packard 				crtcs_out |= mask_out;
1792ed077e4SKeith Packard 			}
1802ed077e4SKeith Packard 			count_out++;
1812ed077e4SKeith Packard 		}
1822ed077e4SKeith Packard 		count_in++;
1832ed077e4SKeith Packard 	}
1842ed077e4SKeith Packard 	mutex_unlock(&master->dev->mode_config.idr_mutex);
1852ed077e4SKeith Packard 	return crtcs_out;
1862ed077e4SKeith Packard }
1872ed077e4SKeith Packard EXPORT_SYMBOL(drm_lease_filter_crtcs);
1882ed077e4SKeith Packard 
1892ed077e4SKeith Packard /*
1902ed077e4SKeith Packard  * drm_lease_create - create a new drm_master with leased objects (idr_mutex not held)
1912ed077e4SKeith Packard  * @lessor: lease holder (or owner) of objects
1922ed077e4SKeith Packard  * @leases: objects to lease to the new drm_master
1932ed077e4SKeith Packard  *
1942ed077e4SKeith Packard  * Uses drm_master_create to allocate a new drm_master, then checks to
1952ed077e4SKeith Packard  * make sure all of the desired objects can be leased, atomically
1962ed077e4SKeith Packard  * leasing them to the new drmmaster.
1972ed077e4SKeith Packard  *
1982ed077e4SKeith Packard  * 	ERR_PTR(-EACCESS)	some other master holds the title to any object
1992ed077e4SKeith Packard  * 	ERR_PTR(-ENOENT)	some object is not a valid DRM object for this device
2002ed077e4SKeith Packard  * 	ERR_PTR(-EBUSY)		some other lessee holds title to this object
2012ed077e4SKeith Packard  *	ERR_PTR(-EEXIST)	same object specified more than once in the provided list
2022ed077e4SKeith Packard  *	ERR_PTR(-ENOMEM)	allocation failed
2032ed077e4SKeith Packard  */
2042ed077e4SKeith Packard static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idr *leases)
2052ed077e4SKeith Packard {
2062ed077e4SKeith Packard 	struct drm_device *dev = lessor->dev;
2072ed077e4SKeith Packard 	int error;
2082ed077e4SKeith Packard 	struct drm_master *lessee;
2092ed077e4SKeith Packard 	int object;
2102ed077e4SKeith Packard 	int id;
2112ed077e4SKeith Packard 	void *entry;
2122ed077e4SKeith Packard 
2132ed077e4SKeith Packard 	DRM_DEBUG_LEASE("lessor %d\n", lessor->lessee_id);
2142ed077e4SKeith Packard 
2152ed077e4SKeith Packard 	lessee = drm_master_create(lessor->dev);
2162ed077e4SKeith Packard 	if (!lessee) {
2172ed077e4SKeith Packard 		DRM_DEBUG_LEASE("drm_master_create failed\n");
2182ed077e4SKeith Packard 		return ERR_PTR(-ENOMEM);
2192ed077e4SKeith Packard 	}
2202ed077e4SKeith Packard 
2212ed077e4SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
2222ed077e4SKeith Packard 
2232ed077e4SKeith Packard 	/* Insert the new lessee into the tree */
2242ed077e4SKeith Packard 	id = idr_alloc(&(drm_lease_owner(lessor)->lessee_idr), lessee, 1, 0, GFP_KERNEL);
2252ed077e4SKeith Packard 	if (id < 0) {
2262ed077e4SKeith Packard 		error = id;
2272ed077e4SKeith Packard 		goto out_lessee;
2282ed077e4SKeith Packard 	}
2292ed077e4SKeith Packard 
2302ed077e4SKeith Packard 	lessee->lessee_id = id;
2312ed077e4SKeith Packard 	lessee->lessor = drm_master_get(lessor);
2322ed077e4SKeith Packard 	list_add_tail(&lessee->lessee_list, &lessor->lessees);
2332ed077e4SKeith Packard 
2342ed077e4SKeith Packard 	idr_for_each_entry(leases, entry, object) {
2352ed077e4SKeith Packard 		error = 0;
2362ed077e4SKeith Packard 		if (!idr_find(&dev->mode_config.crtc_idr, object))
2372ed077e4SKeith Packard 			error = -ENOENT;
2382ed077e4SKeith Packard 		else if (!_drm_lease_held_master(lessor, object))
2392ed077e4SKeith Packard 			error = -EACCES;
2402ed077e4SKeith Packard 		else if (_drm_has_leased(lessor, object))
2412ed077e4SKeith Packard 			error = -EBUSY;
2422ed077e4SKeith Packard 
2432ed077e4SKeith Packard 		if (error != 0) {
2442ed077e4SKeith Packard 			DRM_DEBUG_LEASE("object %d failed %d\n", object, error);
2452ed077e4SKeith Packard 			goto out_lessee;
2462ed077e4SKeith Packard 		}
2472ed077e4SKeith Packard 	}
2482ed077e4SKeith Packard 
2492ed077e4SKeith Packard 	/* Move the leases over */
2502ed077e4SKeith Packard 	lessee->leases = *leases;
2512ed077e4SKeith Packard 	DRM_DEBUG_LEASE("new lessee %d %p, lessor %d %p\n", lessee->lessee_id, lessee, lessor->lessee_id, lessor);
2522ed077e4SKeith Packard 
2532ed077e4SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
2542ed077e4SKeith Packard 	return lessee;
2552ed077e4SKeith Packard 
2562ed077e4SKeith Packard out_lessee:
2572ed077e4SKeith Packard 	drm_master_put(&lessee);
2582ed077e4SKeith Packard 
2592ed077e4SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
2602ed077e4SKeith Packard 
2612ed077e4SKeith Packard 	return ERR_PTR(error);
2622ed077e4SKeith Packard }
2632ed077e4SKeith Packard 
2642ed077e4SKeith Packard /**
2652ed077e4SKeith Packard  * drm_lease_destroy - a master is going away (idr_mutex not held)
2662ed077e4SKeith Packard  * @master: the drm_master being destroyed
2672ed077e4SKeith Packard  *
2682ed077e4SKeith Packard  * All lessees will have been destroyed as they
2692ed077e4SKeith Packard  * hold a reference on their lessor. Notify any
2702ed077e4SKeith Packard  * lessor for this master so that it can check
2712ed077e4SKeith Packard  * the list of lessees.
2722ed077e4SKeith Packard  */
2732ed077e4SKeith Packard void drm_lease_destroy(struct drm_master *master)
2742ed077e4SKeith Packard {
2752ed077e4SKeith Packard 	struct drm_device *dev = master->dev;
2762ed077e4SKeith Packard 
2772ed077e4SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
2782ed077e4SKeith Packard 
2792ed077e4SKeith Packard 	DRM_DEBUG_LEASE("drm_lease_destroy %d\n", master->lessee_id);
2802ed077e4SKeith Packard 
2812ed077e4SKeith Packard 	/* This master is referenced by all lessees, hence it cannot be destroyed
2822ed077e4SKeith Packard 	 * until all of them have been
2832ed077e4SKeith Packard 	 */
2842ed077e4SKeith Packard 	WARN_ON(!list_empty(&master->lessees));
2852ed077e4SKeith Packard 
2862ed077e4SKeith Packard 	/* Remove this master from the lessee idr in the owner */
2872ed077e4SKeith Packard 	if (master->lessee_id != 0) {
2882ed077e4SKeith Packard 		DRM_DEBUG_LEASE("remove master %d from device list of lessees\n", master->lessee_id);
2892ed077e4SKeith Packard 		idr_remove(&(drm_lease_owner(master)->lessee_idr), master->lessee_id);
2902ed077e4SKeith Packard 	}
2912ed077e4SKeith Packard 
2922ed077e4SKeith Packard 	/* Remove this master from any lessee list it may be on */
2932ed077e4SKeith Packard 	list_del(&master->lessee_list);
2942ed077e4SKeith Packard 
2952ed077e4SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
2962ed077e4SKeith Packard 
2972ed077e4SKeith Packard 	if (master->lessor) {
2982ed077e4SKeith Packard 		/* Tell the master to check the lessee list */
2992ed077e4SKeith Packard 		drm_sysfs_hotplug_event(dev);
3002ed077e4SKeith Packard 		drm_master_put(&master->lessor);
3012ed077e4SKeith Packard 	}
3022ed077e4SKeith Packard 
3032ed077e4SKeith Packard 	DRM_DEBUG_LEASE("drm_lease_destroy done %d\n", master->lessee_id);
3042ed077e4SKeith Packard }
3052ed077e4SKeith Packard 
3062ed077e4SKeith Packard /**
3072ed077e4SKeith Packard  * _drm_lease_revoke - revoke access to all leased objects (idr_mutex held)
3082ed077e4SKeith Packard  * @master: the master losing its lease
3092ed077e4SKeith Packard  */
3102ed077e4SKeith Packard static void _drm_lease_revoke(struct drm_master *top)
3112ed077e4SKeith Packard {
3122ed077e4SKeith Packard 	int object;
3132ed077e4SKeith Packard 	void *entry;
3142ed077e4SKeith Packard 	struct drm_master *master = top;
3152ed077e4SKeith Packard 
3162ed077e4SKeith Packard 	lockdep_assert_held(&top->dev->mode_config.idr_mutex);
3172ed077e4SKeith Packard 
3182ed077e4SKeith Packard 	/*
3192ed077e4SKeith Packard 	 * Walk the tree starting at 'top' emptying all leases. Because
3202ed077e4SKeith Packard 	 * the tree is fully connected, we can do this without recursing
3212ed077e4SKeith Packard 	 */
3222ed077e4SKeith Packard 	for (;;) {
3232ed077e4SKeith Packard 		DRM_DEBUG_LEASE("revoke leases for %p %d\n", master, master->lessee_id);
3242ed077e4SKeith Packard 
3252ed077e4SKeith Packard 		/* Evacuate the lease */
3262ed077e4SKeith Packard 		idr_for_each_entry(&master->leases, entry, object)
3272ed077e4SKeith Packard 			idr_remove(&master->leases, object);
3282ed077e4SKeith Packard 
3292ed077e4SKeith Packard 		/* Depth-first list walk */
3302ed077e4SKeith Packard 
3312ed077e4SKeith Packard 		/* Down */
3322ed077e4SKeith Packard 		if (!list_empty(&master->lessees)) {
3332ed077e4SKeith Packard 			master = list_first_entry(&master->lessees, struct drm_master, lessee_list);
3342ed077e4SKeith Packard 		} else {
3352ed077e4SKeith Packard 			/* Up */
3362ed077e4SKeith Packard 			while (master != top && master == list_last_entry(&master->lessor->lessees, struct drm_master, lessee_list))
3372ed077e4SKeith Packard 				master = master->lessor;
3382ed077e4SKeith Packard 
3392ed077e4SKeith Packard 			if (master == top)
3402ed077e4SKeith Packard 				break;
3412ed077e4SKeith Packard 
3422ed077e4SKeith Packard 			/* Over */
3432ed077e4SKeith Packard 			master = list_entry(master->lessee_list.next, struct drm_master, lessee_list);
3442ed077e4SKeith Packard 		}
3452ed077e4SKeith Packard 	}
3462ed077e4SKeith Packard }
3472ed077e4SKeith Packard 
3482ed077e4SKeith Packard /**
3492ed077e4SKeith Packard  * drm_lease_revoke - revoke access to all leased objects (idr_mutex not held)
3502ed077e4SKeith Packard  * @top: the master losing its lease
3512ed077e4SKeith Packard  */
3522ed077e4SKeith Packard void drm_lease_revoke(struct drm_master *top)
3532ed077e4SKeith Packard {
3542ed077e4SKeith Packard 	mutex_lock(&top->dev->mode_config.idr_mutex);
3552ed077e4SKeith Packard 	_drm_lease_revoke(top);
3562ed077e4SKeith Packard 	mutex_unlock(&top->dev->mode_config.idr_mutex);
3572ed077e4SKeith Packard }
358*62884cd3SKeith Packard 
359*62884cd3SKeith Packard static int validate_lease(struct drm_device *dev,
360*62884cd3SKeith Packard 			  struct drm_file *lessor_priv,
361*62884cd3SKeith Packard 			  int object_count,
362*62884cd3SKeith Packard 			  struct drm_mode_object **objects)
363*62884cd3SKeith Packard {
364*62884cd3SKeith Packard 	int o;
365*62884cd3SKeith Packard 	int has_crtc = -1;
366*62884cd3SKeith Packard 	int has_connector = -1;
367*62884cd3SKeith Packard 	int has_plane = -1;
368*62884cd3SKeith Packard 
369*62884cd3SKeith Packard 	/* we want to confirm that there is at least one crtc, plane
370*62884cd3SKeith Packard 	   connector object. */
371*62884cd3SKeith Packard 
372*62884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
373*62884cd3SKeith Packard 		if (objects[o]->type == DRM_MODE_OBJECT_CRTC && has_crtc == -1) {
374*62884cd3SKeith Packard 			has_crtc = o;
375*62884cd3SKeith Packard 		}
376*62884cd3SKeith Packard 		if (objects[o]->type == DRM_MODE_OBJECT_CONNECTOR && has_connector == -1)
377*62884cd3SKeith Packard 			has_connector = o;
378*62884cd3SKeith Packard 
379*62884cd3SKeith Packard 		if (lessor_priv->universal_planes) {
380*62884cd3SKeith Packard 			if (objects[o]->type == DRM_MODE_OBJECT_PLANE && has_plane == -1)
381*62884cd3SKeith Packard 				has_plane = o;
382*62884cd3SKeith Packard 		}
383*62884cd3SKeith Packard 	}
384*62884cd3SKeith Packard 	if (has_crtc == -1 || has_connector == -1)
385*62884cd3SKeith Packard 		return -EINVAL;
386*62884cd3SKeith Packard 	if (lessor_priv->universal_planes && has_plane == -1)
387*62884cd3SKeith Packard 		return -EINVAL;
388*62884cd3SKeith Packard 	return 0;
389*62884cd3SKeith Packard }
390*62884cd3SKeith Packard 
391*62884cd3SKeith Packard static int fill_object_idr(struct drm_device *dev,
392*62884cd3SKeith Packard 			   struct drm_file *lessor_priv,
393*62884cd3SKeith Packard 			   struct idr *leases,
394*62884cd3SKeith Packard 			   int object_count,
395*62884cd3SKeith Packard 			   u32 *object_ids)
396*62884cd3SKeith Packard {
397*62884cd3SKeith Packard 	struct drm_mode_object **objects;
398*62884cd3SKeith Packard 	u32 o;
399*62884cd3SKeith Packard 	int ret;
400*62884cd3SKeith Packard 	objects = kcalloc(object_count, sizeof(struct drm_mode_object *),
401*62884cd3SKeith Packard 			  GFP_KERNEL);
402*62884cd3SKeith Packard 	if (!objects)
403*62884cd3SKeith Packard 		return -ENOMEM;
404*62884cd3SKeith Packard 
405*62884cd3SKeith Packard 	/* step one - get references to all the mode objects
406*62884cd3SKeith Packard 	   and check for validity. */
407*62884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
408*62884cd3SKeith Packard 		if ((int) object_ids[o] < 0) {
409*62884cd3SKeith Packard 			ret = -EINVAL;
410*62884cd3SKeith Packard 			goto out_free_objects;
411*62884cd3SKeith Packard 		}
412*62884cd3SKeith Packard 
413*62884cd3SKeith Packard 		objects[o] = drm_mode_object_find(dev, lessor_priv,
414*62884cd3SKeith Packard 						  object_ids[o],
415*62884cd3SKeith Packard 						  DRM_MODE_OBJECT_ANY);
416*62884cd3SKeith Packard 		if (!objects[o]) {
417*62884cd3SKeith Packard 			ret = -ENOENT;
418*62884cd3SKeith Packard 			goto out_free_objects;
419*62884cd3SKeith Packard 		}
420*62884cd3SKeith Packard 
421*62884cd3SKeith Packard 		if (!drm_mode_object_lease_required(objects[o]->type)) {
422*62884cd3SKeith Packard 			ret = -EINVAL;
423*62884cd3SKeith Packard 			goto out_free_objects;
424*62884cd3SKeith Packard 		}
425*62884cd3SKeith Packard 	}
426*62884cd3SKeith Packard 
427*62884cd3SKeith Packard 	ret = validate_lease(dev, lessor_priv, object_count, objects);
428*62884cd3SKeith Packard 	if (ret)
429*62884cd3SKeith Packard 		goto out_free_objects;
430*62884cd3SKeith Packard 
431*62884cd3SKeith Packard 	/* add their IDs to the lease request - taking into account
432*62884cd3SKeith Packard 	   universal planes */
433*62884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
434*62884cd3SKeith Packard 		struct drm_mode_object *obj = objects[o];
435*62884cd3SKeith Packard 		u32 object_id = objects[o]->id;
436*62884cd3SKeith Packard 		DRM_DEBUG_LEASE("Adding object %d to lease\n", object_id);
437*62884cd3SKeith Packard 
438*62884cd3SKeith Packard 		/*
439*62884cd3SKeith Packard 		 * We're using an IDR to hold the set of leased
440*62884cd3SKeith Packard 		 * objects, but we don't need to point at the object's
441*62884cd3SKeith Packard 		 * data structure from the lease as the main crtc_idr
442*62884cd3SKeith Packard 		 * will be used to actually find that. Instead, all we
443*62884cd3SKeith Packard 		 * really want is a 'leased/not-leased' result, for
444*62884cd3SKeith Packard 		 * which any non-NULL pointer will work fine.
445*62884cd3SKeith Packard 		 */
446*62884cd3SKeith Packard 		ret = idr_alloc(leases, &drm_lease_idr_object , object_id, object_id + 1, GFP_KERNEL);
447*62884cd3SKeith Packard 		if (ret < 0) {
448*62884cd3SKeith Packard 			DRM_DEBUG_LEASE("Object %d cannot be inserted into leases (%d)\n",
449*62884cd3SKeith Packard 					object_id, ret);
450*62884cd3SKeith Packard 			goto out_free_objects;
451*62884cd3SKeith Packard 		}
452*62884cd3SKeith Packard 		if (obj->type == DRM_MODE_OBJECT_CRTC && !lessor_priv->universal_planes) {
453*62884cd3SKeith Packard 			struct drm_crtc *crtc = obj_to_crtc(obj);
454*62884cd3SKeith Packard 			ret = idr_alloc(leases, &drm_lease_idr_object, crtc->primary->base.id, crtc->primary->base.id + 1, GFP_KERNEL);
455*62884cd3SKeith Packard 			if (ret < 0) {
456*62884cd3SKeith Packard 				DRM_DEBUG_LEASE("Object primary plane %d cannot be inserted into leases (%d)\n",
457*62884cd3SKeith Packard 						object_id, ret);
458*62884cd3SKeith Packard 				goto out_free_objects;
459*62884cd3SKeith Packard 			}
460*62884cd3SKeith Packard 			if (crtc->cursor) {
461*62884cd3SKeith Packard 				ret = idr_alloc(leases, &drm_lease_idr_object, crtc->cursor->base.id, crtc->cursor->base.id + 1, GFP_KERNEL);
462*62884cd3SKeith Packard 				if (ret < 0) {
463*62884cd3SKeith Packard 					DRM_DEBUG_LEASE("Object cursor plane %d cannot be inserted into leases (%d)\n",
464*62884cd3SKeith Packard 							object_id, ret);
465*62884cd3SKeith Packard 					goto out_free_objects;
466*62884cd3SKeith Packard 				}
467*62884cd3SKeith Packard 			}
468*62884cd3SKeith Packard 		}
469*62884cd3SKeith Packard 	}
470*62884cd3SKeith Packard 
471*62884cd3SKeith Packard 	ret = 0;
472*62884cd3SKeith Packard out_free_objects:
473*62884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
474*62884cd3SKeith Packard 		if (objects[o])
475*62884cd3SKeith Packard 			drm_mode_object_put(objects[o]);
476*62884cd3SKeith Packard 	}
477*62884cd3SKeith Packard 	kfree(objects);
478*62884cd3SKeith Packard 	return ret;
479*62884cd3SKeith Packard }
480*62884cd3SKeith Packard 
481*62884cd3SKeith Packard /**
482*62884cd3SKeith Packard  * drm_mode_create_lease_ioctl - create a new lease
483*62884cd3SKeith Packard  * @dev: the drm device
484*62884cd3SKeith Packard  * @data: pointer to struct drm_mode_create_lease
485*62884cd3SKeith Packard  * @file_priv: the file being manipulated
486*62884cd3SKeith Packard  *
487*62884cd3SKeith Packard  * The master associated with the specified file will have a lease
488*62884cd3SKeith Packard  * created containing the objects specified in the ioctl structure.
489*62884cd3SKeith Packard  * A file descriptor will be allocated for that and returned to the
490*62884cd3SKeith Packard  * application.
491*62884cd3SKeith Packard  */
492*62884cd3SKeith Packard int drm_mode_create_lease_ioctl(struct drm_device *dev,
493*62884cd3SKeith Packard 				void *data, struct drm_file *lessor_priv)
494*62884cd3SKeith Packard {
495*62884cd3SKeith Packard 	struct drm_mode_create_lease *cl = data;
496*62884cd3SKeith Packard 	size_t object_count;
497*62884cd3SKeith Packard 	int ret = 0;
498*62884cd3SKeith Packard 	struct idr leases;
499*62884cd3SKeith Packard 	struct drm_master *lessor = lessor_priv->master;
500*62884cd3SKeith Packard 	struct drm_master *lessee = NULL;
501*62884cd3SKeith Packard 	struct file *lessee_file = NULL;
502*62884cd3SKeith Packard 	struct file *lessor_file = lessor_priv->filp;
503*62884cd3SKeith Packard 	struct drm_file *lessee_priv;
504*62884cd3SKeith Packard 	int fd = -1;
505*62884cd3SKeith Packard 	uint32_t *object_ids;
506*62884cd3SKeith Packard 
507*62884cd3SKeith Packard 	/* Can't lease without MODESET */
508*62884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
509*62884cd3SKeith Packard 		return -EINVAL;
510*62884cd3SKeith Packard 
511*62884cd3SKeith Packard 	/* Do not allow sub-leases */
512*62884cd3SKeith Packard 	if (lessor->lessor)
513*62884cd3SKeith Packard 		return -EINVAL;
514*62884cd3SKeith Packard 
515*62884cd3SKeith Packard 	/* need some objects */
516*62884cd3SKeith Packard 	if (cl->object_count == 0)
517*62884cd3SKeith Packard 		return -EINVAL;
518*62884cd3SKeith Packard 
519*62884cd3SKeith Packard 	if (cl->flags && (cl->flags & ~(O_CLOEXEC | O_NONBLOCK)))
520*62884cd3SKeith Packard 		return -EINVAL;
521*62884cd3SKeith Packard 
522*62884cd3SKeith Packard 	object_count = cl->object_count;
523*62884cd3SKeith Packard 
524*62884cd3SKeith Packard 	object_ids = memdup_user(u64_to_user_ptr(cl->object_ids), object_count * sizeof(__u32));
525*62884cd3SKeith Packard 	if (IS_ERR(object_ids))
526*62884cd3SKeith Packard 		return PTR_ERR(object_ids);
527*62884cd3SKeith Packard 
528*62884cd3SKeith Packard 	idr_init(&leases);
529*62884cd3SKeith Packard 
530*62884cd3SKeith Packard 	/* fill and validate the object idr */
531*62884cd3SKeith Packard 	ret = fill_object_idr(dev, lessor_priv, &leases,
532*62884cd3SKeith Packard 			      object_count, object_ids);
533*62884cd3SKeith Packard 	kfree(object_ids);
534*62884cd3SKeith Packard 	if (ret) {
535*62884cd3SKeith Packard 		idr_destroy(&leases);
536*62884cd3SKeith Packard 		return ret;
537*62884cd3SKeith Packard 	}
538*62884cd3SKeith Packard 
539*62884cd3SKeith Packard 	/* Allocate a file descriptor for the lease */
540*62884cd3SKeith Packard 	fd = get_unused_fd_flags(cl->flags & (O_CLOEXEC | O_NONBLOCK));
541*62884cd3SKeith Packard 	if (fd < 0) {
542*62884cd3SKeith Packard 		idr_destroy(&leases);
543*62884cd3SKeith Packard 		return fd;
544*62884cd3SKeith Packard 	}
545*62884cd3SKeith Packard 
546*62884cd3SKeith Packard 	DRM_DEBUG_LEASE("Creating lease\n");
547*62884cd3SKeith Packard 	lessee = drm_lease_create(lessor, &leases);
548*62884cd3SKeith Packard 
549*62884cd3SKeith Packard 	if (IS_ERR(lessee)) {
550*62884cd3SKeith Packard 		ret = PTR_ERR(lessee);
551*62884cd3SKeith Packard 		goto out_leases;
552*62884cd3SKeith Packard 	}
553*62884cd3SKeith Packard 
554*62884cd3SKeith Packard 	/* Clone the lessor file to create a new file for us */
555*62884cd3SKeith Packard 	DRM_DEBUG_LEASE("Allocating lease file\n");
556*62884cd3SKeith Packard 	path_get(&lessor_file->f_path);
557*62884cd3SKeith Packard 	lessee_file = alloc_file(&lessor_file->f_path,
558*62884cd3SKeith Packard 				 lessor_file->f_mode,
559*62884cd3SKeith Packard 				 fops_get(lessor_file->f_inode->i_fop));
560*62884cd3SKeith Packard 
561*62884cd3SKeith Packard 	if (IS_ERR(lessee_file)) {
562*62884cd3SKeith Packard 		ret = PTR_ERR(lessee_file);
563*62884cd3SKeith Packard 		goto out_lessee;
564*62884cd3SKeith Packard 	}
565*62884cd3SKeith Packard 
566*62884cd3SKeith Packard 	/* Initialize the new file for DRM */
567*62884cd3SKeith Packard 	DRM_DEBUG_LEASE("Initializing the file with %p\n", lessee_file->f_op->open);
568*62884cd3SKeith Packard 	ret = lessee_file->f_op->open(lessee_file->f_inode, lessee_file);
569*62884cd3SKeith Packard 	if (ret)
570*62884cd3SKeith Packard 		goto out_lessee_file;
571*62884cd3SKeith Packard 
572*62884cd3SKeith Packard 	lessee_priv = lessee_file->private_data;
573*62884cd3SKeith Packard 
574*62884cd3SKeith Packard 	/* Change the file to a master one */
575*62884cd3SKeith Packard 	drm_master_put(&lessee_priv->master);
576*62884cd3SKeith Packard 	lessee_priv->master = lessee;
577*62884cd3SKeith Packard 	lessee_priv->is_master = 1;
578*62884cd3SKeith Packard 	lessee_priv->authenticated = 1;
579*62884cd3SKeith Packard 
580*62884cd3SKeith Packard 	/* Hook up the fd */
581*62884cd3SKeith Packard 	fd_install(fd, lessee_file);
582*62884cd3SKeith Packard 
583*62884cd3SKeith Packard 	/* Pass fd back to userspace */
584*62884cd3SKeith Packard 	DRM_DEBUG_LEASE("Returning fd %d id %d\n", fd, lessee->lessee_id);
585*62884cd3SKeith Packard 	cl->fd = fd;
586*62884cd3SKeith Packard 	cl->lessee_id = lessee->lessee_id;
587*62884cd3SKeith Packard 
588*62884cd3SKeith Packard 	DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl succeeded\n");
589*62884cd3SKeith Packard 	return 0;
590*62884cd3SKeith Packard 
591*62884cd3SKeith Packard out_lessee_file:
592*62884cd3SKeith Packard 	fput(lessee_file);
593*62884cd3SKeith Packard 
594*62884cd3SKeith Packard out_lessee:
595*62884cd3SKeith Packard 	drm_master_put(&lessee);
596*62884cd3SKeith Packard 
597*62884cd3SKeith Packard out_leases:
598*62884cd3SKeith Packard 	put_unused_fd(fd);
599*62884cd3SKeith Packard 	idr_destroy(&leases);
600*62884cd3SKeith Packard 
601*62884cd3SKeith Packard 	DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl failed: %d\n", ret);
602*62884cd3SKeith Packard 	return ret;
603*62884cd3SKeith Packard }
604*62884cd3SKeith Packard 
605*62884cd3SKeith Packard /**
606*62884cd3SKeith Packard  * drm_mode_list_lessees_ioctl - list lessee ids
607*62884cd3SKeith Packard  * @dev: the drm device
608*62884cd3SKeith Packard  * @data: pointer to struct drm_mode_list_lessees
609*62884cd3SKeith Packard  * @lessor_priv: the file being manipulated
610*62884cd3SKeith Packard  *
611*62884cd3SKeith Packard  * Starting from the master associated with the specified file,
612*62884cd3SKeith Packard  * the master with the provided lessee_id is found, and then
613*62884cd3SKeith Packard  * an array of lessee ids associated with leases from that master
614*62884cd3SKeith Packard  * are returned.
615*62884cd3SKeith Packard  */
616*62884cd3SKeith Packard 
617*62884cd3SKeith Packard int drm_mode_list_lessees_ioctl(struct drm_device *dev,
618*62884cd3SKeith Packard 			       void *data, struct drm_file *lessor_priv)
619*62884cd3SKeith Packard {
620*62884cd3SKeith Packard 	struct drm_mode_list_lessees *arg = data;
621*62884cd3SKeith Packard 	__u32 __user *lessee_ids = (__u32 __user *) (uintptr_t) (arg->lessees_ptr);
622*62884cd3SKeith Packard 	__u32 count_lessees = arg->count_lessees;
623*62884cd3SKeith Packard 	struct drm_master *lessor = lessor_priv->master, *lessee;
624*62884cd3SKeith Packard 	int count;
625*62884cd3SKeith Packard 	int ret = 0;
626*62884cd3SKeith Packard 
627*62884cd3SKeith Packard 	if (arg->pad)
628*62884cd3SKeith Packard 		return -EINVAL;
629*62884cd3SKeith Packard 
630*62884cd3SKeith Packard 	/* Can't lease without MODESET */
631*62884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
632*62884cd3SKeith Packard 		return -EINVAL;
633*62884cd3SKeith Packard 
634*62884cd3SKeith Packard 	DRM_DEBUG_LEASE("List lessees for %d\n", lessor->lessee_id);
635*62884cd3SKeith Packard 
636*62884cd3SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
637*62884cd3SKeith Packard 
638*62884cd3SKeith Packard 	count = 0;
639*62884cd3SKeith Packard 	drm_for_each_lessee(lessee, lessor) {
640*62884cd3SKeith Packard 		/* Only list un-revoked leases */
641*62884cd3SKeith Packard 		if (!idr_is_empty(&lessee->leases)) {
642*62884cd3SKeith Packard 			if (count_lessees > count) {
643*62884cd3SKeith Packard 				DRM_DEBUG_LEASE("Add lessee %d\n", lessee->lessee_id);
644*62884cd3SKeith Packard 				ret = put_user(lessee->lessee_id, lessee_ids + count);
645*62884cd3SKeith Packard 				if (ret)
646*62884cd3SKeith Packard 					break;
647*62884cd3SKeith Packard 			}
648*62884cd3SKeith Packard 			count++;
649*62884cd3SKeith Packard 		}
650*62884cd3SKeith Packard 	}
651*62884cd3SKeith Packard 
652*62884cd3SKeith Packard 	DRM_DEBUG_LEASE("Lessor leases to %d\n", count);
653*62884cd3SKeith Packard 	if (ret == 0)
654*62884cd3SKeith Packard 		arg->count_lessees = count;
655*62884cd3SKeith Packard 
656*62884cd3SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
657*62884cd3SKeith Packard 
658*62884cd3SKeith Packard 	return ret;
659*62884cd3SKeith Packard }
660*62884cd3SKeith Packard 
661*62884cd3SKeith Packard /**
662*62884cd3SKeith Packard  * drm_mode_get_lease_ioctl - list leased objects
663*62884cd3SKeith Packard  * @dev: the drm device
664*62884cd3SKeith Packard  * @data: pointer to struct drm_mode_get_lease
665*62884cd3SKeith Packard  * @file_priv: the file being manipulated
666*62884cd3SKeith Packard  *
667*62884cd3SKeith Packard  * Return the list of leased objects for the specified lessee
668*62884cd3SKeith Packard  */
669*62884cd3SKeith Packard 
670*62884cd3SKeith Packard int drm_mode_get_lease_ioctl(struct drm_device *dev,
671*62884cd3SKeith Packard 			     void *data, struct drm_file *lessee_priv)
672*62884cd3SKeith Packard {
673*62884cd3SKeith Packard 	struct drm_mode_get_lease *arg = data;
674*62884cd3SKeith Packard 	__u32 __user *object_ids = (__u32 __user *) (uintptr_t) (arg->objects_ptr);
675*62884cd3SKeith Packard 	__u32 count_objects = arg->count_objects;
676*62884cd3SKeith Packard 	struct drm_master *lessee = lessee_priv->master;
677*62884cd3SKeith Packard 	struct idr *object_idr;
678*62884cd3SKeith Packard 	int count;
679*62884cd3SKeith Packard 	void *entry;
680*62884cd3SKeith Packard 	int object;
681*62884cd3SKeith Packard 	int ret = 0;
682*62884cd3SKeith Packard 
683*62884cd3SKeith Packard 	if (arg->pad)
684*62884cd3SKeith Packard 		return -EINVAL;
685*62884cd3SKeith Packard 
686*62884cd3SKeith Packard 	/* Can't lease without MODESET */
687*62884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
688*62884cd3SKeith Packard 		return -EINVAL;
689*62884cd3SKeith Packard 
690*62884cd3SKeith Packard 	DRM_DEBUG_LEASE("get lease for %d\n", lessee->lessee_id);
691*62884cd3SKeith Packard 
692*62884cd3SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
693*62884cd3SKeith Packard 
694*62884cd3SKeith Packard 	if (lessee->lessor == NULL)
695*62884cd3SKeith Packard 		/* owner can use all objects */
696*62884cd3SKeith Packard 		object_idr = &lessee->dev->mode_config.crtc_idr;
697*62884cd3SKeith Packard 	else
698*62884cd3SKeith Packard 		/* lessee can only use allowed object */
699*62884cd3SKeith Packard 		object_idr = &lessee->leases;
700*62884cd3SKeith Packard 
701*62884cd3SKeith Packard 	count = 0;
702*62884cd3SKeith Packard 	idr_for_each_entry(object_idr, entry, object) {
703*62884cd3SKeith Packard 		if (count_objects > count) {
704*62884cd3SKeith Packard 			DRM_DEBUG_LEASE("adding object %d\n", object);
705*62884cd3SKeith Packard 			ret = put_user(object, object_ids + count);
706*62884cd3SKeith Packard 			if (ret)
707*62884cd3SKeith Packard 				break;
708*62884cd3SKeith Packard 		}
709*62884cd3SKeith Packard 		count++;
710*62884cd3SKeith Packard 	}
711*62884cd3SKeith Packard 
712*62884cd3SKeith Packard 	DRM_DEBUG("lease holds %d objects\n", count);
713*62884cd3SKeith Packard 	if (ret == 0)
714*62884cd3SKeith Packard 		arg->count_objects = count;
715*62884cd3SKeith Packard 
716*62884cd3SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
717*62884cd3SKeith Packard 
718*62884cd3SKeith Packard 	return ret;
719*62884cd3SKeith Packard }
720*62884cd3SKeith Packard 
721*62884cd3SKeith Packard /**
722*62884cd3SKeith Packard  * drm_mode_revoke_lease_ioctl - revoke lease
723*62884cd3SKeith Packard  * @dev: the drm device
724*62884cd3SKeith Packard  * @data: pointer to struct drm_mode_revoke_lease
725*62884cd3SKeith Packard  * @file_priv: the file being manipulated
726*62884cd3SKeith Packard  *
727*62884cd3SKeith Packard  * This removes all of the objects from the lease without
728*62884cd3SKeith Packard  * actually getting rid of the lease itself; that way all
729*62884cd3SKeith Packard  * references to it still work correctly
730*62884cd3SKeith Packard  */
731*62884cd3SKeith Packard int drm_mode_revoke_lease_ioctl(struct drm_device *dev,
732*62884cd3SKeith Packard 				void *data, struct drm_file *lessor_priv)
733*62884cd3SKeith Packard {
734*62884cd3SKeith Packard 	struct drm_mode_revoke_lease *arg = data;
735*62884cd3SKeith Packard 	struct drm_master *lessor = lessor_priv->master;
736*62884cd3SKeith Packard 	struct drm_master *lessee;
737*62884cd3SKeith Packard 	int ret = 0;
738*62884cd3SKeith Packard 
739*62884cd3SKeith Packard 	DRM_DEBUG_LEASE("revoke lease for %d\n", arg->lessee_id);
740*62884cd3SKeith Packard 
741*62884cd3SKeith Packard 	/* Can't lease without MODESET */
742*62884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
743*62884cd3SKeith Packard 		return -EINVAL;
744*62884cd3SKeith Packard 
745*62884cd3SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
746*62884cd3SKeith Packard 
747*62884cd3SKeith Packard 	lessee = _drm_find_lessee(lessor, arg->lessee_id);
748*62884cd3SKeith Packard 
749*62884cd3SKeith Packard 	/* No such lessee */
750*62884cd3SKeith Packard 	if (!lessee) {
751*62884cd3SKeith Packard 		ret = -ENOENT;
752*62884cd3SKeith Packard 		goto fail;
753*62884cd3SKeith Packard 	}
754*62884cd3SKeith Packard 
755*62884cd3SKeith Packard 	/* Lease is not held by lessor */
756*62884cd3SKeith Packard 	if (lessee->lessor != lessor) {
757*62884cd3SKeith Packard 		ret = -EACCES;
758*62884cd3SKeith Packard 		goto fail;
759*62884cd3SKeith Packard 	}
760*62884cd3SKeith Packard 
761*62884cd3SKeith Packard 	_drm_lease_revoke(lessee);
762*62884cd3SKeith Packard 
763*62884cd3SKeith Packard fail:
764*62884cd3SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
765*62884cd3SKeith Packard 
766*62884cd3SKeith Packard 	return ret;
767*62884cd3SKeith Packard }
768