xref: /linux/drivers/gpu/drm/drm_lease.c (revision d793b8f732d6acbc6390be7342fb2e92b069dc7f)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22ed077e4SKeith Packard /*
32ed077e4SKeith Packard  * Copyright © 2017 Keith Packard <keithp@keithp.com>
42ed077e4SKeith Packard  */
50500c04eSSam Ravnborg #include <linux/file.h>
60500c04eSSam Ravnborg #include <linux/uaccess.h>
72ed077e4SKeith Packard 
82ed077e4SKeith Packard #include <drm/drm_auth.h>
92ed077e4SKeith Packard #include <drm/drm_crtc_helper.h>
100500c04eSSam Ravnborg #include <drm/drm_drv.h>
110500c04eSSam Ravnborg #include <drm/drm_file.h>
120500c04eSSam Ravnborg #include <drm/drm_lease.h>
130500c04eSSam Ravnborg #include <drm/drm_print.h>
140500c04eSSam Ravnborg 
150500c04eSSam Ravnborg #include "drm_crtc_internal.h"
160500c04eSSam Ravnborg #include "drm_internal.h"
172ed077e4SKeith Packard 
18*d793b8f7SDesmond Cheong Zhi Xi /**
19*d793b8f7SDesmond Cheong Zhi Xi  * DOC: drm leasing
20*d793b8f7SDesmond Cheong Zhi Xi  *
21*d793b8f7SDesmond Cheong Zhi Xi  * DRM leases provide information about whether a DRM master may control a DRM
22*d793b8f7SDesmond Cheong Zhi Xi  * mode setting object. This enables the creation of multiple DRM masters that
23*d793b8f7SDesmond Cheong Zhi Xi  * manage subsets of display resources.
24*d793b8f7SDesmond Cheong Zhi Xi  *
25*d793b8f7SDesmond Cheong Zhi Xi  * The original DRM master of a device 'owns' the available drm resources. It
26*d793b8f7SDesmond Cheong Zhi Xi  * may create additional DRM masters and 'lease' resources which it controls
27*d793b8f7SDesmond Cheong Zhi Xi  * to the new DRM master. This gives the new DRM master control over the
28*d793b8f7SDesmond Cheong Zhi Xi  * leased resources until the owner revokes the lease, or the new DRM master
29*d793b8f7SDesmond Cheong Zhi Xi  * is closed. Some helpful terminology:
30*d793b8f7SDesmond Cheong Zhi Xi  *
31*d793b8f7SDesmond Cheong Zhi Xi  * - An 'owner' is a &struct drm_master that is not leasing objects from
32*d793b8f7SDesmond Cheong Zhi Xi  *   another &struct drm_master, and hence 'owns' the objects. The owner can be
33*d793b8f7SDesmond Cheong Zhi Xi  *   identified as the &struct drm_master for which &drm_master.lessor is NULL.
34*d793b8f7SDesmond Cheong Zhi Xi  *
35*d793b8f7SDesmond Cheong Zhi Xi  * - A 'lessor' is a &struct drm_master which is leasing objects to one or more
36*d793b8f7SDesmond Cheong Zhi Xi  *   other &struct drm_master. Currently, lessees are not allowed to
37*d793b8f7SDesmond Cheong Zhi Xi  *   create sub-leases, hence the lessor is the same as the owner.
38*d793b8f7SDesmond Cheong Zhi Xi  *
39*d793b8f7SDesmond Cheong Zhi Xi  * - A 'lessee' is a &struct drm_master which is leasing objects from some
40*d793b8f7SDesmond Cheong Zhi Xi  *   other &struct drm_master. Each lessee only leases resources from a single
41*d793b8f7SDesmond Cheong Zhi Xi  *   lessor recorded in &drm_master.lessor, and holds the set of objects that
42*d793b8f7SDesmond Cheong Zhi Xi  *   it is leasing in &drm_master.leases.
43*d793b8f7SDesmond Cheong Zhi Xi  *
44*d793b8f7SDesmond Cheong Zhi Xi  * - A 'lease' is a contract between the lessor and lessee that identifies
45*d793b8f7SDesmond Cheong Zhi Xi  *   which resources may be controlled by the lessee. All of the resources
46*d793b8f7SDesmond Cheong Zhi Xi  *   that are leased must be owned by or leased to the lessor, and lessors are
47*d793b8f7SDesmond Cheong Zhi Xi  *   not permitted to lease the same object to multiple lessees.
48*d793b8f7SDesmond Cheong Zhi Xi  *
49*d793b8f7SDesmond Cheong Zhi Xi  * The set of objects any &struct drm_master 'controls' is limited to the set
50*d793b8f7SDesmond Cheong Zhi Xi  * of objects it leases (for lessees) or all objects (for owners).
51*d793b8f7SDesmond Cheong Zhi Xi  *
52*d793b8f7SDesmond Cheong Zhi Xi  * Objects not controlled by a &struct drm_master cannot be modified through
53*d793b8f7SDesmond Cheong Zhi Xi  * the various state manipulating ioctls, and any state reported back to user
54*d793b8f7SDesmond Cheong Zhi Xi  * space will be edited to make them appear idle and/or unusable. For
55*d793b8f7SDesmond Cheong Zhi Xi  * instance, connectors always report 'disconnected', while encoders
56*d793b8f7SDesmond Cheong Zhi Xi  * report no possible crtcs or clones.
57*d793b8f7SDesmond Cheong Zhi Xi  *
58*d793b8f7SDesmond Cheong Zhi Xi  * Since each lessee may lease objects from a single lessor, display resource
59*d793b8f7SDesmond Cheong Zhi Xi  * leases form a tree of &struct drm_master. As lessees are currently not
60*d793b8f7SDesmond Cheong Zhi Xi  * allowed to create sub-leases, the tree depth is limited to 1. All of
61*d793b8f7SDesmond Cheong Zhi Xi  * these get activated simultaneously when the top level device owner changes
62*d793b8f7SDesmond Cheong Zhi Xi  * through the SETMASTER or DROPMASTER IOCTL, so &drm_device.master points to
63*d793b8f7SDesmond Cheong Zhi Xi  * the owner at the top of the lease tree (i.e. the &struct drm_master for which
64*d793b8f7SDesmond Cheong Zhi Xi  * &drm_master.lessor is NULL). The full list of lessees that are leasing
65*d793b8f7SDesmond Cheong Zhi Xi  * objects from the owner can be searched via the owner's
66*d793b8f7SDesmond Cheong Zhi Xi  * &drm_master.lessee_idr.
67*d793b8f7SDesmond Cheong Zhi Xi  */
68*d793b8f7SDesmond Cheong Zhi Xi 
692ed077e4SKeith Packard #define drm_for_each_lessee(lessee, lessor) \
702ed077e4SKeith Packard 	list_for_each_entry((lessee), &(lessor)->lessees, lessee_list)
712ed077e4SKeith Packard 
7262884cd3SKeith Packard static uint64_t drm_lease_idr_object;
7362884cd3SKeith Packard 
742ed077e4SKeith Packard /**
752ed077e4SKeith Packard  * drm_lease_owner - return ancestor owner drm_master
762ed077e4SKeith Packard  * @master: drm_master somewhere within tree of lessees and lessors
772ed077e4SKeith Packard  *
782ed077e4SKeith Packard  * RETURN:
792ed077e4SKeith Packard  *
802ed077e4SKeith Packard  * drm_master at the top of the tree (i.e, with lessor NULL
812ed077e4SKeith Packard  */
822ed077e4SKeith Packard struct drm_master *drm_lease_owner(struct drm_master *master)
832ed077e4SKeith Packard {
842ed077e4SKeith Packard 	while (master->lessor != NULL)
852ed077e4SKeith Packard 		master = master->lessor;
862ed077e4SKeith Packard 	return master;
872ed077e4SKeith Packard }
882ed077e4SKeith Packard 
892ed077e4SKeith Packard /**
902ed077e4SKeith Packard  * _drm_find_lessee - find lessee by id (idr_mutex held)
912ed077e4SKeith Packard  * @master: drm_master of lessor
927591844cSChris Wilson  * @lessee_id: id
932ed077e4SKeith Packard  *
942ed077e4SKeith Packard  * RETURN:
952ed077e4SKeith Packard  *
962ed077e4SKeith Packard  * drm_master of the lessee if valid, NULL otherwise
972ed077e4SKeith Packard  */
982ed077e4SKeith Packard 
992ed077e4SKeith Packard static struct drm_master*
1002ed077e4SKeith Packard _drm_find_lessee(struct drm_master *master, int lessee_id)
1012ed077e4SKeith Packard {
1022ed077e4SKeith Packard 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
1032ed077e4SKeith Packard 	return idr_find(&drm_lease_owner(master)->lessee_idr, lessee_id);
1042ed077e4SKeith Packard }
1052ed077e4SKeith Packard 
1062ed077e4SKeith Packard /**
1072ed077e4SKeith Packard  * _drm_lease_held_master - check to see if an object is leased (or owned) by master (idr_mutex held)
1082ed077e4SKeith Packard  * @master: the master to check the lease status of
1092ed077e4SKeith Packard  * @id: the id to check
1102ed077e4SKeith Packard  *
1112ed077e4SKeith Packard  * Checks if the specified master holds a lease on the object. Return
1122ed077e4SKeith Packard  * value:
1132ed077e4SKeith Packard  *
1142ed077e4SKeith Packard  *	true		'master' holds a lease on (or owns) the object
1152ed077e4SKeith Packard  *	false		'master' does not hold a lease.
1162ed077e4SKeith Packard  */
1172ed077e4SKeith Packard static int _drm_lease_held_master(struct drm_master *master, int id)
1182ed077e4SKeith Packard {
1192ed077e4SKeith Packard 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
1202ed077e4SKeith Packard 	if (master->lessor)
1212ed077e4SKeith Packard 		return idr_find(&master->leases, id) != NULL;
1222ed077e4SKeith Packard 	return true;
1232ed077e4SKeith Packard }
1242ed077e4SKeith Packard 
1252ed077e4SKeith Packard /**
1262ed077e4SKeith Packard  * _drm_has_leased - check to see if an object has been leased (idr_mutex held)
1272ed077e4SKeith Packard  * @master: the master to check the lease status of
1282ed077e4SKeith Packard  * @id: the id to check
1292ed077e4SKeith Packard  *
1302ed077e4SKeith Packard  * Checks if any lessee of 'master' holds a lease on 'id'. Return
1312ed077e4SKeith Packard  * value:
1322ed077e4SKeith Packard  *
1332ed077e4SKeith Packard  *	true		Some lessee holds a lease on the object.
1342ed077e4SKeith Packard  *	false		No lessee has a lease on the object.
1352ed077e4SKeith Packard  */
1362ed077e4SKeith Packard static bool _drm_has_leased(struct drm_master *master, int id)
1372ed077e4SKeith Packard {
1382ed077e4SKeith Packard 	struct drm_master *lessee;
1392ed077e4SKeith Packard 
1402ed077e4SKeith Packard 	lockdep_assert_held(&master->dev->mode_config.idr_mutex);
1412ed077e4SKeith Packard 	drm_for_each_lessee(lessee, master)
1422ed077e4SKeith Packard 		if (_drm_lease_held_master(lessee, id))
1432ed077e4SKeith Packard 			return true;
1442ed077e4SKeith Packard 	return false;
1452ed077e4SKeith Packard }
1462ed077e4SKeith Packard 
1472ed077e4SKeith Packard /**
1482ed077e4SKeith Packard  * _drm_lease_held - check drm_mode_object lease status (idr_mutex held)
1497591844cSChris Wilson  * @file_priv: the master drm_file
1502ed077e4SKeith Packard  * @id: the object id
1512ed077e4SKeith Packard  *
1522ed077e4SKeith Packard  * Checks if the specified master holds a lease on the object. Return
1532ed077e4SKeith Packard  * value:
1542ed077e4SKeith Packard  *
1552ed077e4SKeith Packard  *	true		'master' holds a lease on (or owns) the object
1562ed077e4SKeith Packard  *	false		'master' does not hold a lease.
1572ed077e4SKeith Packard  */
1582ed077e4SKeith Packard bool _drm_lease_held(struct drm_file *file_priv, int id)
1592ed077e4SKeith Packard {
16056f0729aSDesmond Cheong Zhi Xi 	bool ret;
16156f0729aSDesmond Cheong Zhi Xi 	struct drm_master *master;
16256f0729aSDesmond Cheong Zhi Xi 
16356f0729aSDesmond Cheong Zhi Xi 	if (!file_priv)
1642ed077e4SKeith Packard 		return true;
1652ed077e4SKeith Packard 
16656f0729aSDesmond Cheong Zhi Xi 	master = drm_file_get_master(file_priv);
16756f0729aSDesmond Cheong Zhi Xi 	if (!master)
16856f0729aSDesmond Cheong Zhi Xi 		return true;
16956f0729aSDesmond Cheong Zhi Xi 	ret = _drm_lease_held_master(master, id);
17056f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&master);
17156f0729aSDesmond Cheong Zhi Xi 
17256f0729aSDesmond Cheong Zhi Xi 	return ret;
1732ed077e4SKeith Packard }
1742ed077e4SKeith Packard 
1752ed077e4SKeith Packard /**
1762ed077e4SKeith Packard  * drm_lease_held - check drm_mode_object lease status (idr_mutex not held)
1777591844cSChris Wilson  * @file_priv: the master drm_file
1782ed077e4SKeith Packard  * @id: the object id
1792ed077e4SKeith Packard  *
1802ed077e4SKeith Packard  * Checks if the specified master holds a lease on the object. Return
1812ed077e4SKeith Packard  * value:
1822ed077e4SKeith Packard  *
1832ed077e4SKeith Packard  *	true		'master' holds a lease on (or owns) the object
1842ed077e4SKeith Packard  *	false		'master' does not hold a lease.
1852ed077e4SKeith Packard  */
1862ed077e4SKeith Packard bool drm_lease_held(struct drm_file *file_priv, int id)
1872ed077e4SKeith Packard {
1882ed077e4SKeith Packard 	struct drm_master *master;
1892ed077e4SKeith Packard 	bool ret;
1902ed077e4SKeith Packard 
19156f0729aSDesmond Cheong Zhi Xi 	if (!file_priv)
1922ed077e4SKeith Packard 		return true;
1932ed077e4SKeith Packard 
19456f0729aSDesmond Cheong Zhi Xi 	master = drm_file_get_master(file_priv);
19556f0729aSDesmond Cheong Zhi Xi 	if (!master)
19656f0729aSDesmond Cheong Zhi Xi 		return true;
19756f0729aSDesmond Cheong Zhi Xi 	if (!master->lessor) {
19856f0729aSDesmond Cheong Zhi Xi 		ret = true;
19956f0729aSDesmond Cheong Zhi Xi 		goto out;
20056f0729aSDesmond Cheong Zhi Xi 	}
2012ed077e4SKeith Packard 	mutex_lock(&master->dev->mode_config.idr_mutex);
2022ed077e4SKeith Packard 	ret = _drm_lease_held_master(master, id);
2032ed077e4SKeith Packard 	mutex_unlock(&master->dev->mode_config.idr_mutex);
20456f0729aSDesmond Cheong Zhi Xi 
20556f0729aSDesmond Cheong Zhi Xi out:
20656f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&master);
2072ed077e4SKeith Packard 	return ret;
2082ed077e4SKeith Packard }
2092ed077e4SKeith Packard 
2102ed077e4SKeith Packard /**
2112ed077e4SKeith Packard  * drm_lease_filter_crtcs - restricted crtc set to leased values (idr_mutex not held)
2122ed077e4SKeith Packard  * @file_priv: requestor file
2137591844cSChris Wilson  * @crtcs_in: bitmask of crtcs to check
2142ed077e4SKeith Packard  *
2152ed077e4SKeith Packard  * Reconstructs a crtc mask based on the crtcs which are visible
2162ed077e4SKeith Packard  * through the specified file.
2172ed077e4SKeith Packard  */
2182ed077e4SKeith Packard uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in)
2192ed077e4SKeith Packard {
2202ed077e4SKeith Packard 	struct drm_master *master;
2212ed077e4SKeith Packard 	struct drm_device *dev;
2222ed077e4SKeith Packard 	struct drm_crtc *crtc;
2232ed077e4SKeith Packard 	int count_in, count_out;
2242ed077e4SKeith Packard 	uint32_t crtcs_out = 0;
2252ed077e4SKeith Packard 
22656f0729aSDesmond Cheong Zhi Xi 	if (!file_priv)
2272ed077e4SKeith Packard 		return crtcs_in;
2282ed077e4SKeith Packard 
22956f0729aSDesmond Cheong Zhi Xi 	master = drm_file_get_master(file_priv);
23056f0729aSDesmond Cheong Zhi Xi 	if (!master)
23156f0729aSDesmond Cheong Zhi Xi 		return crtcs_in;
23256f0729aSDesmond Cheong Zhi Xi 	if (!master->lessor) {
23356f0729aSDesmond Cheong Zhi Xi 		crtcs_out = crtcs_in;
23456f0729aSDesmond Cheong Zhi Xi 		goto out;
23556f0729aSDesmond Cheong Zhi Xi 	}
2362ed077e4SKeith Packard 	dev = master->dev;
2372ed077e4SKeith Packard 
2382ed077e4SKeith Packard 	count_in = count_out = 0;
2392ed077e4SKeith Packard 	mutex_lock(&master->dev->mode_config.idr_mutex);
2402ed077e4SKeith Packard 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2412ed077e4SKeith Packard 		if (_drm_lease_held_master(master, crtc->base.id)) {
2422ed077e4SKeith Packard 			uint32_t mask_in = 1ul << count_in;
243948de842SSuraj Upadhyay 
2442ed077e4SKeith Packard 			if ((crtcs_in & mask_in) != 0) {
2452ed077e4SKeith Packard 				uint32_t mask_out = 1ul << count_out;
246948de842SSuraj Upadhyay 
2472ed077e4SKeith Packard 				crtcs_out |= mask_out;
2482ed077e4SKeith Packard 			}
2492ed077e4SKeith Packard 			count_out++;
2502ed077e4SKeith Packard 		}
2512ed077e4SKeith Packard 		count_in++;
2522ed077e4SKeith Packard 	}
2532ed077e4SKeith Packard 	mutex_unlock(&master->dev->mode_config.idr_mutex);
25456f0729aSDesmond Cheong Zhi Xi 
25556f0729aSDesmond Cheong Zhi Xi out:
25656f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&master);
2572ed077e4SKeith Packard 	return crtcs_out;
2582ed077e4SKeith Packard }
2592ed077e4SKeith Packard 
2602ed077e4SKeith Packard /*
2612ed077e4SKeith Packard  * drm_lease_create - create a new drm_master with leased objects (idr_mutex not held)
2622ed077e4SKeith Packard  * @lessor: lease holder (or owner) of objects
2632ed077e4SKeith Packard  * @leases: objects to lease to the new drm_master
2642ed077e4SKeith Packard  *
2652ed077e4SKeith Packard  * Uses drm_master_create to allocate a new drm_master, then checks to
2662ed077e4SKeith Packard  * make sure all of the desired objects can be leased, atomically
2672ed077e4SKeith Packard  * leasing them to the new drmmaster.
2682ed077e4SKeith Packard  *
269918d89bbSColin Ian King  * 	ERR_PTR(-EACCES)	some other master holds the title to any object
2702ed077e4SKeith Packard  * 	ERR_PTR(-ENOENT)	some object is not a valid DRM object for this device
2712ed077e4SKeith Packard  * 	ERR_PTR(-EBUSY)		some other lessee holds title to this object
2722ed077e4SKeith Packard  *	ERR_PTR(-EEXIST)	same object specified more than once in the provided list
2732ed077e4SKeith Packard  *	ERR_PTR(-ENOMEM)	allocation failed
2742ed077e4SKeith Packard  */
2752ed077e4SKeith Packard static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idr *leases)
2762ed077e4SKeith Packard {
2772ed077e4SKeith Packard 	struct drm_device *dev = lessor->dev;
2782ed077e4SKeith Packard 	int error;
2792ed077e4SKeith Packard 	struct drm_master *lessee;
2802ed077e4SKeith Packard 	int object;
2812ed077e4SKeith Packard 	int id;
2822ed077e4SKeith Packard 	void *entry;
2832ed077e4SKeith Packard 
2842ed077e4SKeith Packard 	DRM_DEBUG_LEASE("lessor %d\n", lessor->lessee_id);
2852ed077e4SKeith Packard 
2862ed077e4SKeith Packard 	lessee = drm_master_create(lessor->dev);
2872ed077e4SKeith Packard 	if (!lessee) {
2882ed077e4SKeith Packard 		DRM_DEBUG_LEASE("drm_master_create failed\n");
2892ed077e4SKeith Packard 		return ERR_PTR(-ENOMEM);
2902ed077e4SKeith Packard 	}
2912ed077e4SKeith Packard 
2922ed077e4SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
2932ed077e4SKeith Packard 
2942ed077e4SKeith Packard 	idr_for_each_entry(leases, entry, object) {
2952ed077e4SKeith Packard 		error = 0;
296b5f06893SShayenne da Luz Moura 		if (!idr_find(&dev->mode_config.object_idr, object))
2972ed077e4SKeith Packard 			error = -ENOENT;
2982ed077e4SKeith Packard 		else if (_drm_has_leased(lessor, object))
2992ed077e4SKeith Packard 			error = -EBUSY;
3002ed077e4SKeith Packard 
3012ed077e4SKeith Packard 		if (error != 0) {
3022ed077e4SKeith Packard 			DRM_DEBUG_LEASE("object %d failed %d\n", object, error);
3032ed077e4SKeith Packard 			goto out_lessee;
3042ed077e4SKeith Packard 		}
3052ed077e4SKeith Packard 	}
3062ed077e4SKeith Packard 
307d2a48e52SKeith Packard 	/* Insert the new lessee into the tree */
308d2a48e52SKeith Packard 	id = idr_alloc(&(drm_lease_owner(lessor)->lessee_idr), lessee, 1, 0, GFP_KERNEL);
309d2a48e52SKeith Packard 	if (id < 0) {
310d2a48e52SKeith Packard 		error = id;
311d2a48e52SKeith Packard 		goto out_lessee;
312d2a48e52SKeith Packard 	}
313d2a48e52SKeith Packard 
314d2a48e52SKeith Packard 	lessee->lessee_id = id;
315d2a48e52SKeith Packard 	lessee->lessor = drm_master_get(lessor);
316d2a48e52SKeith Packard 	list_add_tail(&lessee->lessee_list, &lessor->lessees);
317d2a48e52SKeith Packard 
3182ed077e4SKeith Packard 	/* Move the leases over */
3192ed077e4SKeith Packard 	lessee->leases = *leases;
3202ed077e4SKeith Packard 	DRM_DEBUG_LEASE("new lessee %d %p, lessor %d %p\n", lessee->lessee_id, lessee, lessor->lessee_id, lessor);
3212ed077e4SKeith Packard 
3222ed077e4SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
3232ed077e4SKeith Packard 	return lessee;
3242ed077e4SKeith Packard 
3252ed077e4SKeith Packard out_lessee:
3262ed077e4SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
3272ed077e4SKeith Packard 
328bd36d3baSMarius Vlad 	drm_master_put(&lessee);
329bd36d3baSMarius Vlad 
3302ed077e4SKeith Packard 	return ERR_PTR(error);
3312ed077e4SKeith Packard }
3322ed077e4SKeith Packard 
3332ed077e4SKeith Packard /**
3342ed077e4SKeith Packard  * drm_lease_destroy - a master is going away (idr_mutex not held)
3352ed077e4SKeith Packard  * @master: the drm_master being destroyed
3362ed077e4SKeith Packard  *
3372ed077e4SKeith Packard  * All lessees will have been destroyed as they
3382ed077e4SKeith Packard  * hold a reference on their lessor. Notify any
3392ed077e4SKeith Packard  * lessor for this master so that it can check
3402ed077e4SKeith Packard  * the list of lessees.
3412ed077e4SKeith Packard  */
3422ed077e4SKeith Packard void drm_lease_destroy(struct drm_master *master)
3432ed077e4SKeith Packard {
3442ed077e4SKeith Packard 	struct drm_device *dev = master->dev;
3452ed077e4SKeith Packard 
3462ed077e4SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
3472ed077e4SKeith Packard 
3482ed077e4SKeith Packard 	DRM_DEBUG_LEASE("drm_lease_destroy %d\n", master->lessee_id);
3492ed077e4SKeith Packard 
3502ed077e4SKeith Packard 	/* This master is referenced by all lessees, hence it cannot be destroyed
3512ed077e4SKeith Packard 	 * until all of them have been
3522ed077e4SKeith Packard 	 */
3532ed077e4SKeith Packard 	WARN_ON(!list_empty(&master->lessees));
3542ed077e4SKeith Packard 
3552ed077e4SKeith Packard 	/* Remove this master from the lessee idr in the owner */
3562ed077e4SKeith Packard 	if (master->lessee_id != 0) {
3572ed077e4SKeith Packard 		DRM_DEBUG_LEASE("remove master %d from device list of lessees\n", master->lessee_id);
3582ed077e4SKeith Packard 		idr_remove(&(drm_lease_owner(master)->lessee_idr), master->lessee_id);
3592ed077e4SKeith Packard 	}
3602ed077e4SKeith Packard 
3612ed077e4SKeith Packard 	/* Remove this master from any lessee list it may be on */
3622ed077e4SKeith Packard 	list_del(&master->lessee_list);
3632ed077e4SKeith Packard 
3642ed077e4SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
3652ed077e4SKeith Packard 
3662ed077e4SKeith Packard 	if (master->lessor) {
3672ed077e4SKeith Packard 		/* Tell the master to check the lessee list */
368ce858828SDaniel Vetter 		drm_sysfs_lease_event(dev);
3692ed077e4SKeith Packard 		drm_master_put(&master->lessor);
3702ed077e4SKeith Packard 	}
3712ed077e4SKeith Packard 
3722ed077e4SKeith Packard 	DRM_DEBUG_LEASE("drm_lease_destroy done %d\n", master->lessee_id);
3732ed077e4SKeith Packard }
3742ed077e4SKeith Packard 
3752ed077e4SKeith Packard /**
3762ed077e4SKeith Packard  * _drm_lease_revoke - revoke access to all leased objects (idr_mutex held)
3777591844cSChris Wilson  * @top: the master losing its lease
3782ed077e4SKeith Packard  */
3792ed077e4SKeith Packard static void _drm_lease_revoke(struct drm_master *top)
3802ed077e4SKeith Packard {
3812ed077e4SKeith Packard 	int object;
3822ed077e4SKeith Packard 	void *entry;
3832ed077e4SKeith Packard 	struct drm_master *master = top;
3842ed077e4SKeith Packard 
3852ed077e4SKeith Packard 	lockdep_assert_held(&top->dev->mode_config.idr_mutex);
3862ed077e4SKeith Packard 
3872ed077e4SKeith Packard 	/*
3882ed077e4SKeith Packard 	 * Walk the tree starting at 'top' emptying all leases. Because
3892ed077e4SKeith Packard 	 * the tree is fully connected, we can do this without recursing
3902ed077e4SKeith Packard 	 */
3912ed077e4SKeith Packard 	for (;;) {
3922ed077e4SKeith Packard 		DRM_DEBUG_LEASE("revoke leases for %p %d\n", master, master->lessee_id);
3932ed077e4SKeith Packard 
3942ed077e4SKeith Packard 		/* Evacuate the lease */
3952ed077e4SKeith Packard 		idr_for_each_entry(&master->leases, entry, object)
3962ed077e4SKeith Packard 			idr_remove(&master->leases, object);
3972ed077e4SKeith Packard 
3982ed077e4SKeith Packard 		/* Depth-first list walk */
3992ed077e4SKeith Packard 
4002ed077e4SKeith Packard 		/* Down */
4012ed077e4SKeith Packard 		if (!list_empty(&master->lessees)) {
4022ed077e4SKeith Packard 			master = list_first_entry(&master->lessees, struct drm_master, lessee_list);
4032ed077e4SKeith Packard 		} else {
4042ed077e4SKeith Packard 			/* Up */
4052ed077e4SKeith Packard 			while (master != top && master == list_last_entry(&master->lessor->lessees, struct drm_master, lessee_list))
4062ed077e4SKeith Packard 				master = master->lessor;
4072ed077e4SKeith Packard 
4082ed077e4SKeith Packard 			if (master == top)
4092ed077e4SKeith Packard 				break;
4102ed077e4SKeith Packard 
4112ed077e4SKeith Packard 			/* Over */
412aec06c76SArushi Singhal 			master = list_next_entry(master, lessee_list);
4132ed077e4SKeith Packard 		}
4142ed077e4SKeith Packard 	}
4152ed077e4SKeith Packard }
4162ed077e4SKeith Packard 
4172ed077e4SKeith Packard /**
4182ed077e4SKeith Packard  * drm_lease_revoke - revoke access to all leased objects (idr_mutex not held)
4192ed077e4SKeith Packard  * @top: the master losing its lease
4202ed077e4SKeith Packard  */
4212ed077e4SKeith Packard void drm_lease_revoke(struct drm_master *top)
4222ed077e4SKeith Packard {
4232ed077e4SKeith Packard 	mutex_lock(&top->dev->mode_config.idr_mutex);
4242ed077e4SKeith Packard 	_drm_lease_revoke(top);
4252ed077e4SKeith Packard 	mutex_unlock(&top->dev->mode_config.idr_mutex);
4262ed077e4SKeith Packard }
42762884cd3SKeith Packard 
42862884cd3SKeith Packard static int validate_lease(struct drm_device *dev,
42962884cd3SKeith Packard 			  int object_count,
43096802905SDaniel Vetter 			  struct drm_mode_object **objects,
43196802905SDaniel Vetter 			  bool universal_planes)
43262884cd3SKeith Packard {
43362884cd3SKeith Packard 	int o;
43462884cd3SKeith Packard 	int has_crtc = -1;
43562884cd3SKeith Packard 	int has_connector = -1;
43662884cd3SKeith Packard 	int has_plane = -1;
43762884cd3SKeith Packard 
43862884cd3SKeith Packard 	/* we want to confirm that there is at least one crtc, plane
43962884cd3SKeith Packard 	   connector object. */
44062884cd3SKeith Packard 
44162884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
44262884cd3SKeith Packard 		if (objects[o]->type == DRM_MODE_OBJECT_CRTC && has_crtc == -1) {
44362884cd3SKeith Packard 			has_crtc = o;
44462884cd3SKeith Packard 		}
44562884cd3SKeith Packard 		if (objects[o]->type == DRM_MODE_OBJECT_CONNECTOR && has_connector == -1)
44662884cd3SKeith Packard 			has_connector = o;
44762884cd3SKeith Packard 
44896802905SDaniel Vetter 		if (universal_planes) {
44962884cd3SKeith Packard 			if (objects[o]->type == DRM_MODE_OBJECT_PLANE && has_plane == -1)
45062884cd3SKeith Packard 				has_plane = o;
45162884cd3SKeith Packard 		}
45262884cd3SKeith Packard 	}
45362884cd3SKeith Packard 	if (has_crtc == -1 || has_connector == -1)
45462884cd3SKeith Packard 		return -EINVAL;
45596802905SDaniel Vetter 	if (universal_planes && has_plane == -1)
45662884cd3SKeith Packard 		return -EINVAL;
45762884cd3SKeith Packard 	return 0;
45862884cd3SKeith Packard }
45962884cd3SKeith Packard 
46062884cd3SKeith Packard static int fill_object_idr(struct drm_device *dev,
46162884cd3SKeith Packard 			   struct drm_file *lessor_priv,
46262884cd3SKeith Packard 			   struct idr *leases,
46362884cd3SKeith Packard 			   int object_count,
46462884cd3SKeith Packard 			   u32 *object_ids)
46562884cd3SKeith Packard {
46662884cd3SKeith Packard 	struct drm_mode_object **objects;
46762884cd3SKeith Packard 	u32 o;
46862884cd3SKeith Packard 	int ret;
46996802905SDaniel Vetter 	bool universal_planes = READ_ONCE(lessor_priv->universal_planes);
47096802905SDaniel Vetter 
47162884cd3SKeith Packard 	objects = kcalloc(object_count, sizeof(struct drm_mode_object *),
47262884cd3SKeith Packard 			  GFP_KERNEL);
47362884cd3SKeith Packard 	if (!objects)
47462884cd3SKeith Packard 		return -ENOMEM;
47562884cd3SKeith Packard 
47662884cd3SKeith Packard 	/* step one - get references to all the mode objects
47762884cd3SKeith Packard 	   and check for validity. */
47862884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
47962884cd3SKeith Packard 		objects[o] = drm_mode_object_find(dev, lessor_priv,
48062884cd3SKeith Packard 						  object_ids[o],
48162884cd3SKeith Packard 						  DRM_MODE_OBJECT_ANY);
48262884cd3SKeith Packard 		if (!objects[o]) {
48362884cd3SKeith Packard 			ret = -ENOENT;
48462884cd3SKeith Packard 			goto out_free_objects;
48562884cd3SKeith Packard 		}
48662884cd3SKeith Packard 
48762884cd3SKeith Packard 		if (!drm_mode_object_lease_required(objects[o]->type)) {
488a0c1af46SDaniel Vetter 			DRM_DEBUG_KMS("invalid object for lease\n");
48962884cd3SKeith Packard 			ret = -EINVAL;
49062884cd3SKeith Packard 			goto out_free_objects;
49162884cd3SKeith Packard 		}
49262884cd3SKeith Packard 	}
49362884cd3SKeith Packard 
49496802905SDaniel Vetter 	ret = validate_lease(dev, object_count, objects, universal_planes);
495a0c1af46SDaniel Vetter 	if (ret) {
496a0c1af46SDaniel Vetter 		DRM_DEBUG_LEASE("lease validation failed\n");
49762884cd3SKeith Packard 		goto out_free_objects;
498a0c1af46SDaniel Vetter 	}
49962884cd3SKeith Packard 
50062884cd3SKeith Packard 	/* add their IDs to the lease request - taking into account
50162884cd3SKeith Packard 	   universal planes */
50262884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
50362884cd3SKeith Packard 		struct drm_mode_object *obj = objects[o];
50462884cd3SKeith Packard 		u32 object_id = objects[o]->id;
505948de842SSuraj Upadhyay 
50662884cd3SKeith Packard 		DRM_DEBUG_LEASE("Adding object %d to lease\n", object_id);
50762884cd3SKeith Packard 
50862884cd3SKeith Packard 		/*
50962884cd3SKeith Packard 		 * We're using an IDR to hold the set of leased
51062884cd3SKeith Packard 		 * objects, but we don't need to point at the object's
511b5f06893SShayenne da Luz Moura 		 * data structure from the lease as the main object_idr
51262884cd3SKeith Packard 		 * will be used to actually find that. Instead, all we
51362884cd3SKeith Packard 		 * really want is a 'leased/not-leased' result, for
51462884cd3SKeith Packard 		 * which any non-NULL pointer will work fine.
51562884cd3SKeith Packard 		 */
51662884cd3SKeith Packard 		ret = idr_alloc(leases, &drm_lease_idr_object , object_id, object_id + 1, GFP_KERNEL);
51762884cd3SKeith Packard 		if (ret < 0) {
51862884cd3SKeith Packard 			DRM_DEBUG_LEASE("Object %d cannot be inserted into leases (%d)\n",
51962884cd3SKeith Packard 					object_id, ret);
52062884cd3SKeith Packard 			goto out_free_objects;
52162884cd3SKeith Packard 		}
52296802905SDaniel Vetter 		if (obj->type == DRM_MODE_OBJECT_CRTC && !universal_planes) {
52362884cd3SKeith Packard 			struct drm_crtc *crtc = obj_to_crtc(obj);
524948de842SSuraj Upadhyay 
52562884cd3SKeith Packard 			ret = idr_alloc(leases, &drm_lease_idr_object, crtc->primary->base.id, crtc->primary->base.id + 1, GFP_KERNEL);
52662884cd3SKeith Packard 			if (ret < 0) {
52762884cd3SKeith Packard 				DRM_DEBUG_LEASE("Object primary plane %d cannot be inserted into leases (%d)\n",
52862884cd3SKeith Packard 						object_id, ret);
52962884cd3SKeith Packard 				goto out_free_objects;
53062884cd3SKeith Packard 			}
53162884cd3SKeith Packard 			if (crtc->cursor) {
53262884cd3SKeith Packard 				ret = idr_alloc(leases, &drm_lease_idr_object, crtc->cursor->base.id, crtc->cursor->base.id + 1, GFP_KERNEL);
53362884cd3SKeith Packard 				if (ret < 0) {
53462884cd3SKeith Packard 					DRM_DEBUG_LEASE("Object cursor plane %d cannot be inserted into leases (%d)\n",
53562884cd3SKeith Packard 							object_id, ret);
53662884cd3SKeith Packard 					goto out_free_objects;
53762884cd3SKeith Packard 				}
53862884cd3SKeith Packard 			}
53962884cd3SKeith Packard 		}
54062884cd3SKeith Packard 	}
54162884cd3SKeith Packard 
54262884cd3SKeith Packard 	ret = 0;
54362884cd3SKeith Packard out_free_objects:
54462884cd3SKeith Packard 	for (o = 0; o < object_count; o++) {
54562884cd3SKeith Packard 		if (objects[o])
54662884cd3SKeith Packard 			drm_mode_object_put(objects[o]);
54762884cd3SKeith Packard 	}
54862884cd3SKeith Packard 	kfree(objects);
54962884cd3SKeith Packard 	return ret;
55062884cd3SKeith Packard }
55162884cd3SKeith Packard 
55262884cd3SKeith Packard /**
55362884cd3SKeith Packard  * drm_mode_create_lease_ioctl - create a new lease
55462884cd3SKeith Packard  * @dev: the drm device
55562884cd3SKeith Packard  * @data: pointer to struct drm_mode_create_lease
5567591844cSChris Wilson  * @lessor_priv: the file being manipulated
55762884cd3SKeith Packard  *
55862884cd3SKeith Packard  * The master associated with the specified file will have a lease
55962884cd3SKeith Packard  * created containing the objects specified in the ioctl structure.
56062884cd3SKeith Packard  * A file descriptor will be allocated for that and returned to the
56162884cd3SKeith Packard  * application.
56262884cd3SKeith Packard  */
56362884cd3SKeith Packard int drm_mode_create_lease_ioctl(struct drm_device *dev,
56462884cd3SKeith Packard 				void *data, struct drm_file *lessor_priv)
56562884cd3SKeith Packard {
56662884cd3SKeith Packard 	struct drm_mode_create_lease *cl = data;
56762884cd3SKeith Packard 	size_t object_count;
56862884cd3SKeith Packard 	int ret = 0;
56962884cd3SKeith Packard 	struct idr leases;
57056f0729aSDesmond Cheong Zhi Xi 	struct drm_master *lessor;
57162884cd3SKeith Packard 	struct drm_master *lessee = NULL;
57262884cd3SKeith Packard 	struct file *lessee_file = NULL;
57362884cd3SKeith Packard 	struct file *lessor_file = lessor_priv->filp;
57462884cd3SKeith Packard 	struct drm_file *lessee_priv;
57562884cd3SKeith Packard 	int fd = -1;
57662884cd3SKeith Packard 	uint32_t *object_ids;
57762884cd3SKeith Packard 
57862884cd3SKeith Packard 	/* Can't lease without MODESET */
57962884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
58069fdf420SChris Wilson 		return -EOPNOTSUPP;
58162884cd3SKeith Packard 
58262884cd3SKeith Packard 	/* need some objects */
583a0c1af46SDaniel Vetter 	if (cl->object_count == 0) {
584a0c1af46SDaniel Vetter 		DRM_DEBUG_LEASE("no objects in lease\n");
58562884cd3SKeith Packard 		return -EINVAL;
586a0c1af46SDaniel Vetter 	}
58762884cd3SKeith Packard 
588a0c1af46SDaniel Vetter 	if (cl->flags && (cl->flags & ~(O_CLOEXEC | O_NONBLOCK))) {
589a0c1af46SDaniel Vetter 		DRM_DEBUG_LEASE("invalid flags\n");
59062884cd3SKeith Packard 		return -EINVAL;
591a0c1af46SDaniel Vetter 	}
59262884cd3SKeith Packard 
59356f0729aSDesmond Cheong Zhi Xi 	lessor = drm_file_get_master(lessor_priv);
59456f0729aSDesmond Cheong Zhi Xi 	/* Do not allow sub-leases */
59556f0729aSDesmond Cheong Zhi Xi 	if (lessor->lessor) {
59656f0729aSDesmond Cheong Zhi Xi 		DRM_DEBUG_LEASE("recursive leasing not allowed\n");
59756f0729aSDesmond Cheong Zhi Xi 		ret = -EINVAL;
59856f0729aSDesmond Cheong Zhi Xi 		goto out_lessor;
59956f0729aSDesmond Cheong Zhi Xi 	}
60056f0729aSDesmond Cheong Zhi Xi 
60162884cd3SKeith Packard 	object_count = cl->object_count;
60262884cd3SKeith Packard 
60369ef943dSMatthew Wilcox 	object_ids = memdup_user(u64_to_user_ptr(cl->object_ids),
60469ef943dSMatthew Wilcox 			array_size(object_count, sizeof(__u32)));
60556f0729aSDesmond Cheong Zhi Xi 	if (IS_ERR(object_ids)) {
60656f0729aSDesmond Cheong Zhi Xi 		ret = PTR_ERR(object_ids);
60756f0729aSDesmond Cheong Zhi Xi 		goto out_lessor;
60856f0729aSDesmond Cheong Zhi Xi 	}
60962884cd3SKeith Packard 
61062884cd3SKeith Packard 	idr_init(&leases);
61162884cd3SKeith Packard 
61262884cd3SKeith Packard 	/* fill and validate the object idr */
61362884cd3SKeith Packard 	ret = fill_object_idr(dev, lessor_priv, &leases,
61462884cd3SKeith Packard 			      object_count, object_ids);
61562884cd3SKeith Packard 	kfree(object_ids);
61662884cd3SKeith Packard 	if (ret) {
617a0c1af46SDaniel Vetter 		DRM_DEBUG_LEASE("lease object lookup failed: %i\n", ret);
61862884cd3SKeith Packard 		idr_destroy(&leases);
61956f0729aSDesmond Cheong Zhi Xi 		goto out_lessor;
62062884cd3SKeith Packard 	}
62162884cd3SKeith Packard 
62262884cd3SKeith Packard 	/* Allocate a file descriptor for the lease */
62362884cd3SKeith Packard 	fd = get_unused_fd_flags(cl->flags & (O_CLOEXEC | O_NONBLOCK));
62462884cd3SKeith Packard 	if (fd < 0) {
62562884cd3SKeith Packard 		idr_destroy(&leases);
62656f0729aSDesmond Cheong Zhi Xi 		ret = fd;
62756f0729aSDesmond Cheong Zhi Xi 		goto out_lessor;
62862884cd3SKeith Packard 	}
62962884cd3SKeith Packard 
63062884cd3SKeith Packard 	DRM_DEBUG_LEASE("Creating lease\n");
631b216a8e7SQiujun Huang 	/* lessee will take the ownership of leases */
63262884cd3SKeith Packard 	lessee = drm_lease_create(lessor, &leases);
63362884cd3SKeith Packard 
63462884cd3SKeith Packard 	if (IS_ERR(lessee)) {
63562884cd3SKeith Packard 		ret = PTR_ERR(lessee);
636b216a8e7SQiujun Huang 		idr_destroy(&leases);
63762884cd3SKeith Packard 		goto out_leases;
63862884cd3SKeith Packard 	}
63962884cd3SKeith Packard 
64062884cd3SKeith Packard 	/* Clone the lessor file to create a new file for us */
64162884cd3SKeith Packard 	DRM_DEBUG_LEASE("Allocating lease file\n");
64219f391ebSAl Viro 	lessee_file = file_clone_open(lessor_file);
64362884cd3SKeith Packard 	if (IS_ERR(lessee_file)) {
64462884cd3SKeith Packard 		ret = PTR_ERR(lessee_file);
64562884cd3SKeith Packard 		goto out_lessee;
64662884cd3SKeith Packard 	}
64762884cd3SKeith Packard 
64862884cd3SKeith Packard 	lessee_priv = lessee_file->private_data;
64962884cd3SKeith Packard 	/* Change the file to a master one */
65062884cd3SKeith Packard 	drm_master_put(&lessee_priv->master);
65162884cd3SKeith Packard 	lessee_priv->master = lessee;
65262884cd3SKeith Packard 	lessee_priv->is_master = 1;
65362884cd3SKeith Packard 	lessee_priv->authenticated = 1;
65462884cd3SKeith Packard 
65562884cd3SKeith Packard 	/* Pass fd back to userspace */
65662884cd3SKeith Packard 	DRM_DEBUG_LEASE("Returning fd %d id %d\n", fd, lessee->lessee_id);
65762884cd3SKeith Packard 	cl->fd = fd;
65862884cd3SKeith Packard 	cl->lessee_id = lessee->lessee_id;
65962884cd3SKeith Packard 
66012d43debSJann Horn 	/* Hook up the fd */
66112d43debSJann Horn 	fd_install(fd, lessee_file);
66212d43debSJann Horn 
66356f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessor);
66462884cd3SKeith Packard 	DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl succeeded\n");
66562884cd3SKeith Packard 	return 0;
66662884cd3SKeith Packard 
66762884cd3SKeith Packard out_lessee:
66862884cd3SKeith Packard 	drm_master_put(&lessee);
66962884cd3SKeith Packard 
67062884cd3SKeith Packard out_leases:
67162884cd3SKeith Packard 	put_unused_fd(fd);
67262884cd3SKeith Packard 
67356f0729aSDesmond Cheong Zhi Xi out_lessor:
67456f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessor);
67562884cd3SKeith Packard 	DRM_DEBUG_LEASE("drm_mode_create_lease_ioctl failed: %d\n", ret);
67662884cd3SKeith Packard 	return ret;
67762884cd3SKeith Packard }
67862884cd3SKeith Packard 
67962884cd3SKeith Packard /**
68062884cd3SKeith Packard  * drm_mode_list_lessees_ioctl - list lessee ids
68162884cd3SKeith Packard  * @dev: the drm device
68262884cd3SKeith Packard  * @data: pointer to struct drm_mode_list_lessees
68362884cd3SKeith Packard  * @lessor_priv: the file being manipulated
68462884cd3SKeith Packard  *
68562884cd3SKeith Packard  * Starting from the master associated with the specified file,
68662884cd3SKeith Packard  * the master with the provided lessee_id is found, and then
68762884cd3SKeith Packard  * an array of lessee ids associated with leases from that master
68862884cd3SKeith Packard  * are returned.
68962884cd3SKeith Packard  */
69062884cd3SKeith Packard 
69162884cd3SKeith Packard int drm_mode_list_lessees_ioctl(struct drm_device *dev,
69262884cd3SKeith Packard 			       void *data, struct drm_file *lessor_priv)
69362884cd3SKeith Packard {
69462884cd3SKeith Packard 	struct drm_mode_list_lessees *arg = data;
69562884cd3SKeith Packard 	__u32 __user *lessee_ids = (__u32 __user *) (uintptr_t) (arg->lessees_ptr);
69662884cd3SKeith Packard 	__u32 count_lessees = arg->count_lessees;
69756f0729aSDesmond Cheong Zhi Xi 	struct drm_master *lessor, *lessee;
69862884cd3SKeith Packard 	int count;
69962884cd3SKeith Packard 	int ret = 0;
70062884cd3SKeith Packard 
70162884cd3SKeith Packard 	if (arg->pad)
70262884cd3SKeith Packard 		return -EINVAL;
70362884cd3SKeith Packard 
70462884cd3SKeith Packard 	/* Can't lease without MODESET */
70562884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
70669fdf420SChris Wilson 		return -EOPNOTSUPP;
70762884cd3SKeith Packard 
70856f0729aSDesmond Cheong Zhi Xi 	lessor = drm_file_get_master(lessor_priv);
70962884cd3SKeith Packard 	DRM_DEBUG_LEASE("List lessees for %d\n", lessor->lessee_id);
71062884cd3SKeith Packard 
71162884cd3SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
71262884cd3SKeith Packard 
71362884cd3SKeith Packard 	count = 0;
71462884cd3SKeith Packard 	drm_for_each_lessee(lessee, lessor) {
71562884cd3SKeith Packard 		/* Only list un-revoked leases */
71662884cd3SKeith Packard 		if (!idr_is_empty(&lessee->leases)) {
71762884cd3SKeith Packard 			if (count_lessees > count) {
71862884cd3SKeith Packard 				DRM_DEBUG_LEASE("Add lessee %d\n", lessee->lessee_id);
71962884cd3SKeith Packard 				ret = put_user(lessee->lessee_id, lessee_ids + count);
72062884cd3SKeith Packard 				if (ret)
72162884cd3SKeith Packard 					break;
72262884cd3SKeith Packard 			}
72362884cd3SKeith Packard 			count++;
72462884cd3SKeith Packard 		}
72562884cd3SKeith Packard 	}
72662884cd3SKeith Packard 
72762884cd3SKeith Packard 	DRM_DEBUG_LEASE("Lessor leases to %d\n", count);
72862884cd3SKeith Packard 	if (ret == 0)
72962884cd3SKeith Packard 		arg->count_lessees = count;
73062884cd3SKeith Packard 
73162884cd3SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
73256f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessor);
73362884cd3SKeith Packard 
73462884cd3SKeith Packard 	return ret;
73562884cd3SKeith Packard }
73662884cd3SKeith Packard 
73762884cd3SKeith Packard /**
73862884cd3SKeith Packard  * drm_mode_get_lease_ioctl - list leased objects
73962884cd3SKeith Packard  * @dev: the drm device
74062884cd3SKeith Packard  * @data: pointer to struct drm_mode_get_lease
7417591844cSChris Wilson  * @lessee_priv: the file being manipulated
74262884cd3SKeith Packard  *
74362884cd3SKeith Packard  * Return the list of leased objects for the specified lessee
74462884cd3SKeith Packard  */
74562884cd3SKeith Packard 
74662884cd3SKeith Packard int drm_mode_get_lease_ioctl(struct drm_device *dev,
74762884cd3SKeith Packard 			     void *data, struct drm_file *lessee_priv)
74862884cd3SKeith Packard {
74962884cd3SKeith Packard 	struct drm_mode_get_lease *arg = data;
75062884cd3SKeith Packard 	__u32 __user *object_ids = (__u32 __user *) (uintptr_t) (arg->objects_ptr);
75162884cd3SKeith Packard 	__u32 count_objects = arg->count_objects;
75256f0729aSDesmond Cheong Zhi Xi 	struct drm_master *lessee;
75362884cd3SKeith Packard 	struct idr *object_idr;
75462884cd3SKeith Packard 	int count;
75562884cd3SKeith Packard 	void *entry;
75662884cd3SKeith Packard 	int object;
75762884cd3SKeith Packard 	int ret = 0;
75862884cd3SKeith Packard 
75962884cd3SKeith Packard 	if (arg->pad)
76062884cd3SKeith Packard 		return -EINVAL;
76162884cd3SKeith Packard 
76262884cd3SKeith Packard 	/* Can't lease without MODESET */
76362884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
76469fdf420SChris Wilson 		return -EOPNOTSUPP;
76562884cd3SKeith Packard 
76656f0729aSDesmond Cheong Zhi Xi 	lessee = drm_file_get_master(lessee_priv);
76762884cd3SKeith Packard 	DRM_DEBUG_LEASE("get lease for %d\n", lessee->lessee_id);
76862884cd3SKeith Packard 
76962884cd3SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
77062884cd3SKeith Packard 
77162884cd3SKeith Packard 	if (lessee->lessor == NULL)
77262884cd3SKeith Packard 		/* owner can use all objects */
773b5f06893SShayenne da Luz Moura 		object_idr = &lessee->dev->mode_config.object_idr;
77462884cd3SKeith Packard 	else
77562884cd3SKeith Packard 		/* lessee can only use allowed object */
77662884cd3SKeith Packard 		object_idr = &lessee->leases;
77762884cd3SKeith Packard 
77862884cd3SKeith Packard 	count = 0;
77962884cd3SKeith Packard 	idr_for_each_entry(object_idr, entry, object) {
78062884cd3SKeith Packard 		if (count_objects > count) {
78162884cd3SKeith Packard 			DRM_DEBUG_LEASE("adding object %d\n", object);
78262884cd3SKeith Packard 			ret = put_user(object, object_ids + count);
78362884cd3SKeith Packard 			if (ret)
78462884cd3SKeith Packard 				break;
78562884cd3SKeith Packard 		}
78662884cd3SKeith Packard 		count++;
78762884cd3SKeith Packard 	}
78862884cd3SKeith Packard 
78962884cd3SKeith Packard 	DRM_DEBUG("lease holds %d objects\n", count);
79062884cd3SKeith Packard 	if (ret == 0)
79162884cd3SKeith Packard 		arg->count_objects = count;
79262884cd3SKeith Packard 
79362884cd3SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
79456f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessee);
79562884cd3SKeith Packard 
79662884cd3SKeith Packard 	return ret;
79762884cd3SKeith Packard }
79862884cd3SKeith Packard 
79962884cd3SKeith Packard /**
80062884cd3SKeith Packard  * drm_mode_revoke_lease_ioctl - revoke lease
80162884cd3SKeith Packard  * @dev: the drm device
80262884cd3SKeith Packard  * @data: pointer to struct drm_mode_revoke_lease
8037591844cSChris Wilson  * @lessor_priv: the file being manipulated
80462884cd3SKeith Packard  *
80562884cd3SKeith Packard  * This removes all of the objects from the lease without
80662884cd3SKeith Packard  * actually getting rid of the lease itself; that way all
80762884cd3SKeith Packard  * references to it still work correctly
80862884cd3SKeith Packard  */
80962884cd3SKeith Packard int drm_mode_revoke_lease_ioctl(struct drm_device *dev,
81062884cd3SKeith Packard 				void *data, struct drm_file *lessor_priv)
81162884cd3SKeith Packard {
81262884cd3SKeith Packard 	struct drm_mode_revoke_lease *arg = data;
81356f0729aSDesmond Cheong Zhi Xi 	struct drm_master *lessor;
81462884cd3SKeith Packard 	struct drm_master *lessee;
81562884cd3SKeith Packard 	int ret = 0;
81662884cd3SKeith Packard 
81762884cd3SKeith Packard 	DRM_DEBUG_LEASE("revoke lease for %d\n", arg->lessee_id);
81862884cd3SKeith Packard 
81962884cd3SKeith Packard 	/* Can't lease without MODESET */
82062884cd3SKeith Packard 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
82169fdf420SChris Wilson 		return -EOPNOTSUPP;
82262884cd3SKeith Packard 
82356f0729aSDesmond Cheong Zhi Xi 	lessor = drm_file_get_master(lessor_priv);
82462884cd3SKeith Packard 	mutex_lock(&dev->mode_config.idr_mutex);
82562884cd3SKeith Packard 
82662884cd3SKeith Packard 	lessee = _drm_find_lessee(lessor, arg->lessee_id);
82762884cd3SKeith Packard 
82862884cd3SKeith Packard 	/* No such lessee */
82962884cd3SKeith Packard 	if (!lessee) {
83062884cd3SKeith Packard 		ret = -ENOENT;
83162884cd3SKeith Packard 		goto fail;
83262884cd3SKeith Packard 	}
83362884cd3SKeith Packard 
83462884cd3SKeith Packard 	/* Lease is not held by lessor */
83562884cd3SKeith Packard 	if (lessee->lessor != lessor) {
83662884cd3SKeith Packard 		ret = -EACCES;
83762884cd3SKeith Packard 		goto fail;
83862884cd3SKeith Packard 	}
83962884cd3SKeith Packard 
84062884cd3SKeith Packard 	_drm_lease_revoke(lessee);
84162884cd3SKeith Packard 
84262884cd3SKeith Packard fail:
84362884cd3SKeith Packard 	mutex_unlock(&dev->mode_config.idr_mutex);
84456f0729aSDesmond Cheong Zhi Xi 	drm_master_put(&lessor);
84562884cd3SKeith Packard 
84662884cd3SKeith Packard 	return ret;
84762884cd3SKeith Packard }
848