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> 9*cde3d37bSThomas Zimmermann #include <drm/drm_crtc.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 18d793b8f7SDesmond Cheong Zhi Xi /** 19d793b8f7SDesmond Cheong Zhi Xi * DOC: drm leasing 20d793b8f7SDesmond Cheong Zhi Xi * 21d793b8f7SDesmond Cheong Zhi Xi * DRM leases provide information about whether a DRM master may control a DRM 22d793b8f7SDesmond Cheong Zhi Xi * mode setting object. This enables the creation of multiple DRM masters that 23d793b8f7SDesmond Cheong Zhi Xi * manage subsets of display resources. 24d793b8f7SDesmond Cheong Zhi Xi * 25d793b8f7SDesmond Cheong Zhi Xi * The original DRM master of a device 'owns' the available drm resources. It 26d793b8f7SDesmond Cheong Zhi Xi * may create additional DRM masters and 'lease' resources which it controls 27d793b8f7SDesmond Cheong Zhi Xi * to the new DRM master. This gives the new DRM master control over the 28d793b8f7SDesmond Cheong Zhi Xi * leased resources until the owner revokes the lease, or the new DRM master 29d793b8f7SDesmond Cheong Zhi Xi * is closed. Some helpful terminology: 30d793b8f7SDesmond Cheong Zhi Xi * 31d793b8f7SDesmond Cheong Zhi Xi * - An 'owner' is a &struct drm_master that is not leasing objects from 32d793b8f7SDesmond Cheong Zhi Xi * another &struct drm_master, and hence 'owns' the objects. The owner can be 33d793b8f7SDesmond Cheong Zhi Xi * identified as the &struct drm_master for which &drm_master.lessor is NULL. 34d793b8f7SDesmond Cheong Zhi Xi * 35d793b8f7SDesmond Cheong Zhi Xi * - A 'lessor' is a &struct drm_master which is leasing objects to one or more 36d793b8f7SDesmond Cheong Zhi Xi * other &struct drm_master. Currently, lessees are not allowed to 37d793b8f7SDesmond Cheong Zhi Xi * create sub-leases, hence the lessor is the same as the owner. 38d793b8f7SDesmond Cheong Zhi Xi * 39d793b8f7SDesmond Cheong Zhi Xi * - A 'lessee' is a &struct drm_master which is leasing objects from some 40d793b8f7SDesmond Cheong Zhi Xi * other &struct drm_master. Each lessee only leases resources from a single 41d793b8f7SDesmond Cheong Zhi Xi * lessor recorded in &drm_master.lessor, and holds the set of objects that 42d793b8f7SDesmond Cheong Zhi Xi * it is leasing in &drm_master.leases. 43d793b8f7SDesmond Cheong Zhi Xi * 44d793b8f7SDesmond Cheong Zhi Xi * - A 'lease' is a contract between the lessor and lessee that identifies 45d793b8f7SDesmond Cheong Zhi Xi * which resources may be controlled by the lessee. All of the resources 46d793b8f7SDesmond Cheong Zhi Xi * that are leased must be owned by or leased to the lessor, and lessors are 47d793b8f7SDesmond Cheong Zhi Xi * not permitted to lease the same object to multiple lessees. 48d793b8f7SDesmond Cheong Zhi Xi * 49d793b8f7SDesmond Cheong Zhi Xi * The set of objects any &struct drm_master 'controls' is limited to the set 50d793b8f7SDesmond Cheong Zhi Xi * of objects it leases (for lessees) or all objects (for owners). 51d793b8f7SDesmond Cheong Zhi Xi * 52d793b8f7SDesmond Cheong Zhi Xi * Objects not controlled by a &struct drm_master cannot be modified through 53d793b8f7SDesmond Cheong Zhi Xi * the various state manipulating ioctls, and any state reported back to user 54d793b8f7SDesmond Cheong Zhi Xi * space will be edited to make them appear idle and/or unusable. For 55d793b8f7SDesmond Cheong Zhi Xi * instance, connectors always report 'disconnected', while encoders 56d793b8f7SDesmond Cheong Zhi Xi * report no possible crtcs or clones. 57d793b8f7SDesmond Cheong Zhi Xi * 58d793b8f7SDesmond Cheong Zhi Xi * Since each lessee may lease objects from a single lessor, display resource 59d793b8f7SDesmond Cheong Zhi Xi * leases form a tree of &struct drm_master. As lessees are currently not 60d793b8f7SDesmond Cheong Zhi Xi * allowed to create sub-leases, the tree depth is limited to 1. All of 61d793b8f7SDesmond Cheong Zhi Xi * these get activated simultaneously when the top level device owner changes 62d793b8f7SDesmond Cheong Zhi Xi * through the SETMASTER or DROPMASTER IOCTL, so &drm_device.master points to 63d793b8f7SDesmond Cheong Zhi Xi * the owner at the top of the lease tree (i.e. the &struct drm_master for which 64d793b8f7SDesmond Cheong Zhi Xi * &drm_master.lessor is NULL). The full list of lessees that are leasing 65d793b8f7SDesmond Cheong Zhi Xi * objects from the owner can be searched via the owner's 66d793b8f7SDesmond Cheong Zhi Xi * &drm_master.lessee_idr. 67d793b8f7SDesmond Cheong Zhi Xi */ 68d793b8f7SDesmond 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 struct drm_master *drm_lease_owner(struct drm_master *master) 752ed077e4SKeith Packard { 762ed077e4SKeith Packard while (master->lessor != NULL) 772ed077e4SKeith Packard master = master->lessor; 782ed077e4SKeith Packard return master; 792ed077e4SKeith Packard } 802ed077e4SKeith Packard 812ed077e4SKeith Packard static struct drm_master* 822ed077e4SKeith Packard _drm_find_lessee(struct drm_master *master, int lessee_id) 832ed077e4SKeith Packard { 842ed077e4SKeith Packard lockdep_assert_held(&master->dev->mode_config.idr_mutex); 852ed077e4SKeith Packard return idr_find(&drm_lease_owner(master)->lessee_idr, lessee_id); 862ed077e4SKeith Packard } 872ed077e4SKeith Packard 882ed077e4SKeith Packard static int _drm_lease_held_master(struct drm_master *master, int id) 892ed077e4SKeith Packard { 902ed077e4SKeith Packard lockdep_assert_held(&master->dev->mode_config.idr_mutex); 912ed077e4SKeith Packard if (master->lessor) 922ed077e4SKeith Packard return idr_find(&master->leases, id) != NULL; 932ed077e4SKeith Packard return true; 942ed077e4SKeith Packard } 952ed077e4SKeith Packard 96676f11b5SDesmond Cheong Zhi Xi /* Checks if the given object has been leased to some lessee of drm_master */ 972ed077e4SKeith Packard static bool _drm_has_leased(struct drm_master *master, int id) 982ed077e4SKeith Packard { 992ed077e4SKeith Packard struct drm_master *lessee; 1002ed077e4SKeith Packard 1012ed077e4SKeith Packard lockdep_assert_held(&master->dev->mode_config.idr_mutex); 1022ed077e4SKeith Packard drm_for_each_lessee(lessee, master) 1032ed077e4SKeith Packard if (_drm_lease_held_master(lessee, id)) 1042ed077e4SKeith Packard return true; 1052ed077e4SKeith Packard return false; 1062ed077e4SKeith Packard } 1072ed077e4SKeith Packard 108676f11b5SDesmond Cheong Zhi Xi /* Called with idr_mutex held */ 1092ed077e4SKeith Packard bool _drm_lease_held(struct drm_file *file_priv, int id) 1102ed077e4SKeith Packard { 11156f0729aSDesmond Cheong Zhi Xi bool ret; 11256f0729aSDesmond Cheong Zhi Xi struct drm_master *master; 11356f0729aSDesmond Cheong Zhi Xi 11456f0729aSDesmond Cheong Zhi Xi if (!file_priv) 1152ed077e4SKeith Packard return true; 1162ed077e4SKeith Packard 11756f0729aSDesmond Cheong Zhi Xi master = drm_file_get_master(file_priv); 11856f0729aSDesmond Cheong Zhi Xi if (!master) 11956f0729aSDesmond Cheong Zhi Xi return true; 12056f0729aSDesmond Cheong Zhi Xi ret = _drm_lease_held_master(master, id); 12156f0729aSDesmond Cheong Zhi Xi drm_master_put(&master); 12256f0729aSDesmond Cheong Zhi Xi 12356f0729aSDesmond Cheong Zhi Xi return ret; 1242ed077e4SKeith Packard } 1252ed077e4SKeith Packard 1262ed077e4SKeith Packard bool drm_lease_held(struct drm_file *file_priv, int id) 1272ed077e4SKeith Packard { 1282ed077e4SKeith Packard struct drm_master *master; 1292ed077e4SKeith Packard bool ret; 1302ed077e4SKeith Packard 13156f0729aSDesmond Cheong Zhi Xi if (!file_priv) 1322ed077e4SKeith Packard return true; 1332ed077e4SKeith Packard 13456f0729aSDesmond Cheong Zhi Xi master = drm_file_get_master(file_priv); 13556f0729aSDesmond Cheong Zhi Xi if (!master) 13656f0729aSDesmond Cheong Zhi Xi return true; 13756f0729aSDesmond Cheong Zhi Xi if (!master->lessor) { 13856f0729aSDesmond Cheong Zhi Xi ret = true; 13956f0729aSDesmond Cheong Zhi Xi goto out; 14056f0729aSDesmond Cheong Zhi Xi } 1412ed077e4SKeith Packard mutex_lock(&master->dev->mode_config.idr_mutex); 1422ed077e4SKeith Packard ret = _drm_lease_held_master(master, id); 1432ed077e4SKeith Packard mutex_unlock(&master->dev->mode_config.idr_mutex); 14456f0729aSDesmond Cheong Zhi Xi 14556f0729aSDesmond Cheong Zhi Xi out: 14656f0729aSDesmond Cheong Zhi Xi drm_master_put(&master); 1472ed077e4SKeith Packard return ret; 1482ed077e4SKeith Packard } 1492ed077e4SKeith Packard 150676f11b5SDesmond Cheong Zhi Xi /* 151676f11b5SDesmond Cheong Zhi Xi * Given a bitmask of crtcs to check, reconstructs a crtc mask based on the 152676f11b5SDesmond Cheong Zhi Xi * crtcs which are visible through the specified file. 1532ed077e4SKeith Packard */ 1542ed077e4SKeith Packard uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in) 1552ed077e4SKeith Packard { 1562ed077e4SKeith Packard struct drm_master *master; 1572ed077e4SKeith Packard struct drm_device *dev; 1582ed077e4SKeith Packard struct drm_crtc *crtc; 1592ed077e4SKeith Packard int count_in, count_out; 1602ed077e4SKeith Packard uint32_t crtcs_out = 0; 1612ed077e4SKeith Packard 16256f0729aSDesmond Cheong Zhi Xi if (!file_priv) 1632ed077e4SKeith Packard return crtcs_in; 1642ed077e4SKeith Packard 16556f0729aSDesmond Cheong Zhi Xi master = drm_file_get_master(file_priv); 16656f0729aSDesmond Cheong Zhi Xi if (!master) 16756f0729aSDesmond Cheong Zhi Xi return crtcs_in; 16856f0729aSDesmond Cheong Zhi Xi if (!master->lessor) { 16956f0729aSDesmond Cheong Zhi Xi crtcs_out = crtcs_in; 17056f0729aSDesmond Cheong Zhi Xi goto out; 17156f0729aSDesmond Cheong Zhi Xi } 1722ed077e4SKeith Packard dev = master->dev; 1732ed077e4SKeith Packard 1742ed077e4SKeith Packard count_in = count_out = 0; 1752ed077e4SKeith Packard mutex_lock(&master->dev->mode_config.idr_mutex); 1762ed077e4SKeith Packard list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 1772ed077e4SKeith Packard if (_drm_lease_held_master(master, crtc->base.id)) { 1782ed077e4SKeith Packard uint32_t mask_in = 1ul << count_in; 179948de842SSuraj Upadhyay 1802ed077e4SKeith Packard if ((crtcs_in & mask_in) != 0) { 1812ed077e4SKeith Packard uint32_t mask_out = 1ul << count_out; 182948de842SSuraj Upadhyay 1832ed077e4SKeith Packard crtcs_out |= mask_out; 1842ed077e4SKeith Packard } 1852ed077e4SKeith Packard count_out++; 1862ed077e4SKeith Packard } 1872ed077e4SKeith Packard count_in++; 1882ed077e4SKeith Packard } 1892ed077e4SKeith Packard mutex_unlock(&master->dev->mode_config.idr_mutex); 19056f0729aSDesmond Cheong Zhi Xi 19156f0729aSDesmond Cheong Zhi Xi out: 19256f0729aSDesmond Cheong Zhi Xi drm_master_put(&master); 1932ed077e4SKeith Packard return crtcs_out; 1942ed077e4SKeith Packard } 1952ed077e4SKeith Packard 1962ed077e4SKeith Packard /* 1972ed077e4SKeith Packard * Uses drm_master_create to allocate a new drm_master, then checks to 1982ed077e4SKeith Packard * make sure all of the desired objects can be leased, atomically 1992ed077e4SKeith Packard * leasing them to the new drmmaster. 2002ed077e4SKeith Packard * 201918d89bbSColin Ian King * ERR_PTR(-EACCES) some other master holds the title to any object 2022ed077e4SKeith Packard * ERR_PTR(-ENOENT) some object is not a valid DRM object for this device 2032ed077e4SKeith Packard * ERR_PTR(-EBUSY) some other lessee holds title to this object 2042ed077e4SKeith Packard * ERR_PTR(-EEXIST) same object specified more than once in the provided list 2052ed077e4SKeith Packard * ERR_PTR(-ENOMEM) allocation failed 2062ed077e4SKeith Packard */ 2072ed077e4SKeith Packard static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idr *leases) 2082ed077e4SKeith Packard { 2092ed077e4SKeith Packard struct drm_device *dev = lessor->dev; 2102ed077e4SKeith Packard int error; 2112ed077e4SKeith Packard struct drm_master *lessee; 2122ed077e4SKeith Packard int object; 2132ed077e4SKeith Packard int id; 2142ed077e4SKeith Packard void *entry; 2152ed077e4SKeith Packard 2167bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "lessor %d\n", lessor->lessee_id); 2172ed077e4SKeith Packard 2182ed077e4SKeith Packard lessee = drm_master_create(lessor->dev); 2192ed077e4SKeith Packard if (!lessee) { 2207bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "drm_master_create failed\n"); 2212ed077e4SKeith Packard return ERR_PTR(-ENOMEM); 2222ed077e4SKeith Packard } 2232ed077e4SKeith Packard 2242ed077e4SKeith Packard mutex_lock(&dev->mode_config.idr_mutex); 2252ed077e4SKeith Packard 2262ed077e4SKeith Packard idr_for_each_entry(leases, entry, object) { 2272ed077e4SKeith Packard error = 0; 228b5f06893SShayenne da Luz Moura if (!idr_find(&dev->mode_config.object_idr, object)) 2292ed077e4SKeith Packard error = -ENOENT; 2302ed077e4SKeith Packard else if (_drm_has_leased(lessor, object)) 2312ed077e4SKeith Packard error = -EBUSY; 2322ed077e4SKeith Packard 2332ed077e4SKeith Packard if (error != 0) { 2347bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "object %d failed %d\n", object, error); 2352ed077e4SKeith Packard goto out_lessee; 2362ed077e4SKeith Packard } 2372ed077e4SKeith Packard } 2382ed077e4SKeith Packard 239d2a48e52SKeith Packard /* Insert the new lessee into the tree */ 240d2a48e52SKeith Packard id = idr_alloc(&(drm_lease_owner(lessor)->lessee_idr), lessee, 1, 0, GFP_KERNEL); 241d2a48e52SKeith Packard if (id < 0) { 242d2a48e52SKeith Packard error = id; 243d2a48e52SKeith Packard goto out_lessee; 244d2a48e52SKeith Packard } 245d2a48e52SKeith Packard 246d2a48e52SKeith Packard lessee->lessee_id = id; 247d2a48e52SKeith Packard lessee->lessor = drm_master_get(lessor); 248d2a48e52SKeith Packard list_add_tail(&lessee->lessee_list, &lessor->lessees); 249d2a48e52SKeith Packard 2502ed077e4SKeith Packard /* Move the leases over */ 2512ed077e4SKeith Packard lessee->leases = *leases; 2527bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "new lessee %d %p, lessor %d %p\n", 2537bd224b6SSiddh Raman Pant lessee->lessee_id, lessee, lessor->lessee_id, lessor); 2542ed077e4SKeith Packard 2552ed077e4SKeith Packard mutex_unlock(&dev->mode_config.idr_mutex); 2562ed077e4SKeith Packard return lessee; 2572ed077e4SKeith Packard 2582ed077e4SKeith Packard out_lessee: 2592ed077e4SKeith Packard mutex_unlock(&dev->mode_config.idr_mutex); 2602ed077e4SKeith Packard 261bd36d3baSMarius Vlad drm_master_put(&lessee); 262bd36d3baSMarius Vlad 2632ed077e4SKeith Packard return ERR_PTR(error); 2642ed077e4SKeith Packard } 2652ed077e4SKeith Packard 2662ed077e4SKeith Packard void drm_lease_destroy(struct drm_master *master) 2672ed077e4SKeith Packard { 2682ed077e4SKeith Packard struct drm_device *dev = master->dev; 2692ed077e4SKeith Packard 2702ed077e4SKeith Packard mutex_lock(&dev->mode_config.idr_mutex); 2712ed077e4SKeith Packard 2727bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "drm_lease_destroy %d\n", master->lessee_id); 2732ed077e4SKeith Packard 2742ed077e4SKeith Packard /* This master is referenced by all lessees, hence it cannot be destroyed 2752ed077e4SKeith Packard * until all of them have been 2762ed077e4SKeith Packard */ 2772ed077e4SKeith Packard WARN_ON(!list_empty(&master->lessees)); 2782ed077e4SKeith Packard 2792ed077e4SKeith Packard /* Remove this master from the lessee idr in the owner */ 2802ed077e4SKeith Packard if (master->lessee_id != 0) { 2817bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "remove master %d from device list of lessees\n", 2827bd224b6SSiddh Raman Pant master->lessee_id); 2832ed077e4SKeith Packard idr_remove(&(drm_lease_owner(master)->lessee_idr), master->lessee_id); 2842ed077e4SKeith Packard } 2852ed077e4SKeith Packard 2862ed077e4SKeith Packard /* Remove this master from any lessee list it may be on */ 2872ed077e4SKeith Packard list_del(&master->lessee_list); 2882ed077e4SKeith Packard 2892ed077e4SKeith Packard mutex_unlock(&dev->mode_config.idr_mutex); 2902ed077e4SKeith Packard 2912ed077e4SKeith Packard if (master->lessor) { 2922ed077e4SKeith Packard /* Tell the master to check the lessee list */ 293ce858828SDaniel Vetter drm_sysfs_lease_event(dev); 2942ed077e4SKeith Packard drm_master_put(&master->lessor); 2952ed077e4SKeith Packard } 2962ed077e4SKeith Packard 2977bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "drm_lease_destroy done %d\n", master->lessee_id); 2982ed077e4SKeith Packard } 2992ed077e4SKeith Packard 3002ed077e4SKeith Packard static void _drm_lease_revoke(struct drm_master *top) 3012ed077e4SKeith Packard { 3022ed077e4SKeith Packard int object; 3032ed077e4SKeith Packard void *entry; 3042ed077e4SKeith Packard struct drm_master *master = top; 3052ed077e4SKeith Packard 3062ed077e4SKeith Packard lockdep_assert_held(&top->dev->mode_config.idr_mutex); 3072ed077e4SKeith Packard 3082ed077e4SKeith Packard /* 3092ed077e4SKeith Packard * Walk the tree starting at 'top' emptying all leases. Because 3102ed077e4SKeith Packard * the tree is fully connected, we can do this without recursing 3112ed077e4SKeith Packard */ 3122ed077e4SKeith Packard for (;;) { 3137bd224b6SSiddh Raman Pant drm_dbg_lease(master->dev, "revoke leases for %p %d\n", 3147bd224b6SSiddh Raman Pant master, master->lessee_id); 3152ed077e4SKeith Packard 3162ed077e4SKeith Packard /* Evacuate the lease */ 3172ed077e4SKeith Packard idr_for_each_entry(&master->leases, entry, object) 3182ed077e4SKeith Packard idr_remove(&master->leases, object); 3192ed077e4SKeith Packard 3202ed077e4SKeith Packard /* Depth-first list walk */ 3212ed077e4SKeith Packard 3222ed077e4SKeith Packard /* Down */ 3232ed077e4SKeith Packard if (!list_empty(&master->lessees)) { 3242ed077e4SKeith Packard master = list_first_entry(&master->lessees, struct drm_master, lessee_list); 3252ed077e4SKeith Packard } else { 3262ed077e4SKeith Packard /* Up */ 3272ed077e4SKeith Packard while (master != top && master == list_last_entry(&master->lessor->lessees, struct drm_master, lessee_list)) 3282ed077e4SKeith Packard master = master->lessor; 3292ed077e4SKeith Packard 3302ed077e4SKeith Packard if (master == top) 3312ed077e4SKeith Packard break; 3322ed077e4SKeith Packard 3332ed077e4SKeith Packard /* Over */ 334aec06c76SArushi Singhal master = list_next_entry(master, lessee_list); 3352ed077e4SKeith Packard } 3362ed077e4SKeith Packard } 3372ed077e4SKeith Packard } 3382ed077e4SKeith Packard 3392ed077e4SKeith Packard void drm_lease_revoke(struct drm_master *top) 3402ed077e4SKeith Packard { 3412ed077e4SKeith Packard mutex_lock(&top->dev->mode_config.idr_mutex); 3422ed077e4SKeith Packard _drm_lease_revoke(top); 3432ed077e4SKeith Packard mutex_unlock(&top->dev->mode_config.idr_mutex); 3442ed077e4SKeith Packard } 34562884cd3SKeith Packard 34662884cd3SKeith Packard static int validate_lease(struct drm_device *dev, 34762884cd3SKeith Packard int object_count, 34896802905SDaniel Vetter struct drm_mode_object **objects, 34996802905SDaniel Vetter bool universal_planes) 35062884cd3SKeith Packard { 35162884cd3SKeith Packard int o; 35262884cd3SKeith Packard int has_crtc = -1; 35362884cd3SKeith Packard int has_connector = -1; 35462884cd3SKeith Packard int has_plane = -1; 35562884cd3SKeith Packard 35662884cd3SKeith Packard /* we want to confirm that there is at least one crtc, plane 35762884cd3SKeith Packard connector object. */ 35862884cd3SKeith Packard 35962884cd3SKeith Packard for (o = 0; o < object_count; o++) { 36062884cd3SKeith Packard if (objects[o]->type == DRM_MODE_OBJECT_CRTC && has_crtc == -1) { 36162884cd3SKeith Packard has_crtc = o; 36262884cd3SKeith Packard } 36362884cd3SKeith Packard if (objects[o]->type == DRM_MODE_OBJECT_CONNECTOR && has_connector == -1) 36462884cd3SKeith Packard has_connector = o; 36562884cd3SKeith Packard 36696802905SDaniel Vetter if (universal_planes) { 36762884cd3SKeith Packard if (objects[o]->type == DRM_MODE_OBJECT_PLANE && has_plane == -1) 36862884cd3SKeith Packard has_plane = o; 36962884cd3SKeith Packard } 37062884cd3SKeith Packard } 37162884cd3SKeith Packard if (has_crtc == -1 || has_connector == -1) 37262884cd3SKeith Packard return -EINVAL; 37396802905SDaniel Vetter if (universal_planes && has_plane == -1) 37462884cd3SKeith Packard return -EINVAL; 37562884cd3SKeith Packard return 0; 37662884cd3SKeith Packard } 37762884cd3SKeith Packard 37862884cd3SKeith Packard static int fill_object_idr(struct drm_device *dev, 37962884cd3SKeith Packard struct drm_file *lessor_priv, 38062884cd3SKeith Packard struct idr *leases, 38162884cd3SKeith Packard int object_count, 38262884cd3SKeith Packard u32 *object_ids) 38362884cd3SKeith Packard { 38462884cd3SKeith Packard struct drm_mode_object **objects; 38562884cd3SKeith Packard u32 o; 38662884cd3SKeith Packard int ret; 38796802905SDaniel Vetter bool universal_planes = READ_ONCE(lessor_priv->universal_planes); 38896802905SDaniel Vetter 38962884cd3SKeith Packard objects = kcalloc(object_count, sizeof(struct drm_mode_object *), 39062884cd3SKeith Packard GFP_KERNEL); 39162884cd3SKeith Packard if (!objects) 39262884cd3SKeith Packard return -ENOMEM; 39362884cd3SKeith Packard 39462884cd3SKeith Packard /* step one - get references to all the mode objects 39562884cd3SKeith Packard and check for validity. */ 39662884cd3SKeith Packard for (o = 0; o < object_count; o++) { 39762884cd3SKeith Packard objects[o] = drm_mode_object_find(dev, lessor_priv, 39862884cd3SKeith Packard object_ids[o], 39962884cd3SKeith Packard DRM_MODE_OBJECT_ANY); 40062884cd3SKeith Packard if (!objects[o]) { 40162884cd3SKeith Packard ret = -ENOENT; 40262884cd3SKeith Packard goto out_free_objects; 40362884cd3SKeith Packard } 40462884cd3SKeith Packard 40562884cd3SKeith Packard if (!drm_mode_object_lease_required(objects[o]->type)) { 406a0c1af46SDaniel Vetter DRM_DEBUG_KMS("invalid object for lease\n"); 40762884cd3SKeith Packard ret = -EINVAL; 40862884cd3SKeith Packard goto out_free_objects; 40962884cd3SKeith Packard } 41062884cd3SKeith Packard } 41162884cd3SKeith Packard 41296802905SDaniel Vetter ret = validate_lease(dev, object_count, objects, universal_planes); 413a0c1af46SDaniel Vetter if (ret) { 4147bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "lease validation failed\n"); 41562884cd3SKeith Packard goto out_free_objects; 416a0c1af46SDaniel Vetter } 41762884cd3SKeith Packard 41862884cd3SKeith Packard /* add their IDs to the lease request - taking into account 41962884cd3SKeith Packard universal planes */ 42062884cd3SKeith Packard for (o = 0; o < object_count; o++) { 42162884cd3SKeith Packard struct drm_mode_object *obj = objects[o]; 42262884cd3SKeith Packard u32 object_id = objects[o]->id; 423948de842SSuraj Upadhyay 4247bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "Adding object %d to lease\n", object_id); 42562884cd3SKeith Packard 42662884cd3SKeith Packard /* 42762884cd3SKeith Packard * We're using an IDR to hold the set of leased 42862884cd3SKeith Packard * objects, but we don't need to point at the object's 429b5f06893SShayenne da Luz Moura * data structure from the lease as the main object_idr 43062884cd3SKeith Packard * will be used to actually find that. Instead, all we 43162884cd3SKeith Packard * really want is a 'leased/not-leased' result, for 43262884cd3SKeith Packard * which any non-NULL pointer will work fine. 43362884cd3SKeith Packard */ 43462884cd3SKeith Packard ret = idr_alloc(leases, &drm_lease_idr_object , object_id, object_id + 1, GFP_KERNEL); 43562884cd3SKeith Packard if (ret < 0) { 4367bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "Object %d cannot be inserted into leases (%d)\n", 43762884cd3SKeith Packard object_id, ret); 43862884cd3SKeith Packard goto out_free_objects; 43962884cd3SKeith Packard } 44096802905SDaniel Vetter if (obj->type == DRM_MODE_OBJECT_CRTC && !universal_planes) { 44162884cd3SKeith Packard struct drm_crtc *crtc = obj_to_crtc(obj); 442948de842SSuraj Upadhyay 44362884cd3SKeith Packard ret = idr_alloc(leases, &drm_lease_idr_object, crtc->primary->base.id, crtc->primary->base.id + 1, GFP_KERNEL); 44462884cd3SKeith Packard if (ret < 0) { 4457bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "Object primary plane %d cannot be inserted into leases (%d)\n", 44662884cd3SKeith Packard object_id, ret); 44762884cd3SKeith Packard goto out_free_objects; 44862884cd3SKeith Packard } 44962884cd3SKeith Packard if (crtc->cursor) { 45062884cd3SKeith Packard ret = idr_alloc(leases, &drm_lease_idr_object, crtc->cursor->base.id, crtc->cursor->base.id + 1, GFP_KERNEL); 45162884cd3SKeith Packard if (ret < 0) { 4527bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "Object cursor plane %d cannot be inserted into leases (%d)\n", 45362884cd3SKeith Packard object_id, ret); 45462884cd3SKeith Packard goto out_free_objects; 45562884cd3SKeith Packard } 45662884cd3SKeith Packard } 45762884cd3SKeith Packard } 45862884cd3SKeith Packard } 45962884cd3SKeith Packard 46062884cd3SKeith Packard ret = 0; 46162884cd3SKeith Packard out_free_objects: 46262884cd3SKeith Packard for (o = 0; o < object_count; o++) { 46362884cd3SKeith Packard if (objects[o]) 46462884cd3SKeith Packard drm_mode_object_put(objects[o]); 46562884cd3SKeith Packard } 46662884cd3SKeith Packard kfree(objects); 46762884cd3SKeith Packard return ret; 46862884cd3SKeith Packard } 46962884cd3SKeith Packard 470676f11b5SDesmond Cheong Zhi Xi /* 47162884cd3SKeith Packard * The master associated with the specified file will have a lease 47262884cd3SKeith Packard * created containing the objects specified in the ioctl structure. 47362884cd3SKeith Packard * A file descriptor will be allocated for that and returned to the 47462884cd3SKeith Packard * application. 47562884cd3SKeith Packard */ 47662884cd3SKeith Packard int drm_mode_create_lease_ioctl(struct drm_device *dev, 47762884cd3SKeith Packard void *data, struct drm_file *lessor_priv) 47862884cd3SKeith Packard { 47962884cd3SKeith Packard struct drm_mode_create_lease *cl = data; 48062884cd3SKeith Packard size_t object_count; 48162884cd3SKeith Packard int ret = 0; 48262884cd3SKeith Packard struct idr leases; 48356f0729aSDesmond Cheong Zhi Xi struct drm_master *lessor; 48462884cd3SKeith Packard struct drm_master *lessee = NULL; 48562884cd3SKeith Packard struct file *lessee_file = NULL; 48662884cd3SKeith Packard struct file *lessor_file = lessor_priv->filp; 48762884cd3SKeith Packard struct drm_file *lessee_priv; 48862884cd3SKeith Packard int fd = -1; 48962884cd3SKeith Packard uint32_t *object_ids; 49062884cd3SKeith Packard 49162884cd3SKeith Packard /* Can't lease without MODESET */ 49262884cd3SKeith Packard if (!drm_core_check_feature(dev, DRIVER_MODESET)) 49369fdf420SChris Wilson return -EOPNOTSUPP; 49462884cd3SKeith Packard 495a0c1af46SDaniel Vetter if (cl->flags && (cl->flags & ~(O_CLOEXEC | O_NONBLOCK))) { 4967bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "invalid flags\n"); 49762884cd3SKeith Packard return -EINVAL; 498a0c1af46SDaniel Vetter } 49962884cd3SKeith Packard 50056f0729aSDesmond Cheong Zhi Xi lessor = drm_file_get_master(lessor_priv); 50156f0729aSDesmond Cheong Zhi Xi /* Do not allow sub-leases */ 50256f0729aSDesmond Cheong Zhi Xi if (lessor->lessor) { 5037bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "recursive leasing not allowed\n"); 50456f0729aSDesmond Cheong Zhi Xi ret = -EINVAL; 50556f0729aSDesmond Cheong Zhi Xi goto out_lessor; 50656f0729aSDesmond Cheong Zhi Xi } 50756f0729aSDesmond Cheong Zhi Xi 50862884cd3SKeith Packard object_count = cl->object_count; 50962884cd3SKeith Packard 5104bb2d367SSimon Ser /* Handle leased objects, if any */ 5114bb2d367SSimon Ser idr_init(&leases); 5124bb2d367SSimon Ser if (object_count != 0) { 51369ef943dSMatthew Wilcox object_ids = memdup_user(u64_to_user_ptr(cl->object_ids), 51469ef943dSMatthew Wilcox array_size(object_count, sizeof(__u32))); 51556f0729aSDesmond Cheong Zhi Xi if (IS_ERR(object_ids)) { 51656f0729aSDesmond Cheong Zhi Xi ret = PTR_ERR(object_ids); 5174bb2d367SSimon Ser idr_destroy(&leases); 51856f0729aSDesmond Cheong Zhi Xi goto out_lessor; 51956f0729aSDesmond Cheong Zhi Xi } 52062884cd3SKeith Packard 52162884cd3SKeith Packard /* fill and validate the object idr */ 52262884cd3SKeith Packard ret = fill_object_idr(dev, lessor_priv, &leases, 52362884cd3SKeith Packard object_count, object_ids); 52462884cd3SKeith Packard kfree(object_ids); 52562884cd3SKeith Packard if (ret) { 5267bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "lease object lookup failed: %i\n", ret); 52762884cd3SKeith Packard idr_destroy(&leases); 52856f0729aSDesmond Cheong Zhi Xi goto out_lessor; 52962884cd3SKeith Packard } 5304bb2d367SSimon Ser } 53162884cd3SKeith Packard 53262884cd3SKeith Packard /* Allocate a file descriptor for the lease */ 53362884cd3SKeith Packard fd = get_unused_fd_flags(cl->flags & (O_CLOEXEC | O_NONBLOCK)); 53462884cd3SKeith Packard if (fd < 0) { 53562884cd3SKeith Packard idr_destroy(&leases); 53656f0729aSDesmond Cheong Zhi Xi ret = fd; 53756f0729aSDesmond Cheong Zhi Xi goto out_lessor; 53862884cd3SKeith Packard } 53962884cd3SKeith Packard 5407bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "Creating lease\n"); 541b216a8e7SQiujun Huang /* lessee will take the ownership of leases */ 54262884cd3SKeith Packard lessee = drm_lease_create(lessor, &leases); 54362884cd3SKeith Packard 54462884cd3SKeith Packard if (IS_ERR(lessee)) { 54562884cd3SKeith Packard ret = PTR_ERR(lessee); 546b216a8e7SQiujun Huang idr_destroy(&leases); 54762884cd3SKeith Packard goto out_leases; 54862884cd3SKeith Packard } 54962884cd3SKeith Packard 55062884cd3SKeith Packard /* Clone the lessor file to create a new file for us */ 5517bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "Allocating lease file\n"); 55219f391ebSAl Viro lessee_file = file_clone_open(lessor_file); 55362884cd3SKeith Packard if (IS_ERR(lessee_file)) { 55462884cd3SKeith Packard ret = PTR_ERR(lessee_file); 55562884cd3SKeith Packard goto out_lessee; 55662884cd3SKeith Packard } 55762884cd3SKeith Packard 55862884cd3SKeith Packard lessee_priv = lessee_file->private_data; 55962884cd3SKeith Packard /* Change the file to a master one */ 56062884cd3SKeith Packard drm_master_put(&lessee_priv->master); 56162884cd3SKeith Packard lessee_priv->master = lessee; 56262884cd3SKeith Packard lessee_priv->is_master = 1; 56362884cd3SKeith Packard lessee_priv->authenticated = 1; 56462884cd3SKeith Packard 56562884cd3SKeith Packard /* Pass fd back to userspace */ 5667bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "Returning fd %d id %d\n", fd, lessee->lessee_id); 56762884cd3SKeith Packard cl->fd = fd; 56862884cd3SKeith Packard cl->lessee_id = lessee->lessee_id; 56962884cd3SKeith Packard 57012d43debSJann Horn /* Hook up the fd */ 57112d43debSJann Horn fd_install(fd, lessee_file); 57212d43debSJann Horn 57356f0729aSDesmond Cheong Zhi Xi drm_master_put(&lessor); 5747bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "drm_mode_create_lease_ioctl succeeded\n"); 57562884cd3SKeith Packard return 0; 57662884cd3SKeith Packard 57762884cd3SKeith Packard out_lessee: 57862884cd3SKeith Packard drm_master_put(&lessee); 57962884cd3SKeith Packard 58062884cd3SKeith Packard out_leases: 58162884cd3SKeith Packard put_unused_fd(fd); 58262884cd3SKeith Packard 58356f0729aSDesmond Cheong Zhi Xi out_lessor: 58456f0729aSDesmond Cheong Zhi Xi drm_master_put(&lessor); 5857bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "drm_mode_create_lease_ioctl failed: %d\n", ret); 58662884cd3SKeith Packard return ret; 58762884cd3SKeith Packard } 58862884cd3SKeith Packard 58962884cd3SKeith Packard int drm_mode_list_lessees_ioctl(struct drm_device *dev, 59062884cd3SKeith Packard void *data, struct drm_file *lessor_priv) 59162884cd3SKeith Packard { 59262884cd3SKeith Packard struct drm_mode_list_lessees *arg = data; 59362884cd3SKeith Packard __u32 __user *lessee_ids = (__u32 __user *) (uintptr_t) (arg->lessees_ptr); 59462884cd3SKeith Packard __u32 count_lessees = arg->count_lessees; 59556f0729aSDesmond Cheong Zhi Xi struct drm_master *lessor, *lessee; 59662884cd3SKeith Packard int count; 59762884cd3SKeith Packard int ret = 0; 59862884cd3SKeith Packard 59962884cd3SKeith Packard if (arg->pad) 60062884cd3SKeith Packard return -EINVAL; 60162884cd3SKeith Packard 60262884cd3SKeith Packard /* Can't lease without MODESET */ 60362884cd3SKeith Packard if (!drm_core_check_feature(dev, DRIVER_MODESET)) 60469fdf420SChris Wilson return -EOPNOTSUPP; 60562884cd3SKeith Packard 60656f0729aSDesmond Cheong Zhi Xi lessor = drm_file_get_master(lessor_priv); 6077bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "List lessees for %d\n", lessor->lessee_id); 60862884cd3SKeith Packard 60962884cd3SKeith Packard mutex_lock(&dev->mode_config.idr_mutex); 61062884cd3SKeith Packard 61162884cd3SKeith Packard count = 0; 61262884cd3SKeith Packard drm_for_each_lessee(lessee, lessor) { 61362884cd3SKeith Packard /* Only list un-revoked leases */ 61462884cd3SKeith Packard if (!idr_is_empty(&lessee->leases)) { 61562884cd3SKeith Packard if (count_lessees > count) { 6167bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "Add lessee %d\n", 6177bd224b6SSiddh Raman Pant lessee->lessee_id); 61862884cd3SKeith Packard ret = put_user(lessee->lessee_id, lessee_ids + count); 61962884cd3SKeith Packard if (ret) 62062884cd3SKeith Packard break; 62162884cd3SKeith Packard } 62262884cd3SKeith Packard count++; 62362884cd3SKeith Packard } 62462884cd3SKeith Packard } 62562884cd3SKeith Packard 6267bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "Lessor leases to %d\n", count); 62762884cd3SKeith Packard if (ret == 0) 62862884cd3SKeith Packard arg->count_lessees = count; 62962884cd3SKeith Packard 63062884cd3SKeith Packard mutex_unlock(&dev->mode_config.idr_mutex); 63156f0729aSDesmond Cheong Zhi Xi drm_master_put(&lessor); 63262884cd3SKeith Packard 63362884cd3SKeith Packard return ret; 63462884cd3SKeith Packard } 63562884cd3SKeith Packard 636676f11b5SDesmond Cheong Zhi Xi /* Return the list of leased objects for the specified lessee */ 63762884cd3SKeith Packard int drm_mode_get_lease_ioctl(struct drm_device *dev, 63862884cd3SKeith Packard void *data, struct drm_file *lessee_priv) 63962884cd3SKeith Packard { 64062884cd3SKeith Packard struct drm_mode_get_lease *arg = data; 64162884cd3SKeith Packard __u32 __user *object_ids = (__u32 __user *) (uintptr_t) (arg->objects_ptr); 64262884cd3SKeith Packard __u32 count_objects = arg->count_objects; 64356f0729aSDesmond Cheong Zhi Xi struct drm_master *lessee; 64462884cd3SKeith Packard struct idr *object_idr; 64562884cd3SKeith Packard int count; 64662884cd3SKeith Packard void *entry; 64762884cd3SKeith Packard int object; 64862884cd3SKeith Packard int ret = 0; 64962884cd3SKeith Packard 65062884cd3SKeith Packard if (arg->pad) 65162884cd3SKeith Packard return -EINVAL; 65262884cd3SKeith Packard 65362884cd3SKeith Packard /* Can't lease without MODESET */ 65462884cd3SKeith Packard if (!drm_core_check_feature(dev, DRIVER_MODESET)) 65569fdf420SChris Wilson return -EOPNOTSUPP; 65662884cd3SKeith Packard 65756f0729aSDesmond Cheong Zhi Xi lessee = drm_file_get_master(lessee_priv); 6587bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "get lease for %d\n", lessee->lessee_id); 65962884cd3SKeith Packard 66062884cd3SKeith Packard mutex_lock(&dev->mode_config.idr_mutex); 66162884cd3SKeith Packard 66262884cd3SKeith Packard if (lessee->lessor == NULL) 66362884cd3SKeith Packard /* owner can use all objects */ 664b5f06893SShayenne da Luz Moura object_idr = &lessee->dev->mode_config.object_idr; 66562884cd3SKeith Packard else 66662884cd3SKeith Packard /* lessee can only use allowed object */ 66762884cd3SKeith Packard object_idr = &lessee->leases; 66862884cd3SKeith Packard 66962884cd3SKeith Packard count = 0; 67062884cd3SKeith Packard idr_for_each_entry(object_idr, entry, object) { 67162884cd3SKeith Packard if (count_objects > count) { 6727bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "adding object %d\n", object); 67362884cd3SKeith Packard ret = put_user(object, object_ids + count); 67462884cd3SKeith Packard if (ret) 67562884cd3SKeith Packard break; 67662884cd3SKeith Packard } 67762884cd3SKeith Packard count++; 67862884cd3SKeith Packard } 67962884cd3SKeith Packard 68062884cd3SKeith Packard DRM_DEBUG("lease holds %d objects\n", count); 68162884cd3SKeith Packard if (ret == 0) 68262884cd3SKeith Packard arg->count_objects = count; 68362884cd3SKeith Packard 68462884cd3SKeith Packard mutex_unlock(&dev->mode_config.idr_mutex); 68556f0729aSDesmond Cheong Zhi Xi drm_master_put(&lessee); 68662884cd3SKeith Packard 68762884cd3SKeith Packard return ret; 68862884cd3SKeith Packard } 68962884cd3SKeith Packard 690676f11b5SDesmond Cheong Zhi Xi /* 69162884cd3SKeith Packard * This removes all of the objects from the lease without 69262884cd3SKeith Packard * actually getting rid of the lease itself; that way all 69362884cd3SKeith Packard * references to it still work correctly 69462884cd3SKeith Packard */ 69562884cd3SKeith Packard int drm_mode_revoke_lease_ioctl(struct drm_device *dev, 69662884cd3SKeith Packard void *data, struct drm_file *lessor_priv) 69762884cd3SKeith Packard { 69862884cd3SKeith Packard struct drm_mode_revoke_lease *arg = data; 69956f0729aSDesmond Cheong Zhi Xi struct drm_master *lessor; 70062884cd3SKeith Packard struct drm_master *lessee; 70162884cd3SKeith Packard int ret = 0; 70262884cd3SKeith Packard 7037bd224b6SSiddh Raman Pant drm_dbg_lease(dev, "revoke lease for %d\n", arg->lessee_id); 70462884cd3SKeith Packard 70562884cd3SKeith Packard /* Can't lease without MODESET */ 70662884cd3SKeith Packard if (!drm_core_check_feature(dev, DRIVER_MODESET)) 70769fdf420SChris Wilson return -EOPNOTSUPP; 70862884cd3SKeith Packard 70956f0729aSDesmond Cheong Zhi Xi lessor = drm_file_get_master(lessor_priv); 71062884cd3SKeith Packard mutex_lock(&dev->mode_config.idr_mutex); 71162884cd3SKeith Packard 71262884cd3SKeith Packard lessee = _drm_find_lessee(lessor, arg->lessee_id); 71362884cd3SKeith Packard 71462884cd3SKeith Packard /* No such lessee */ 71562884cd3SKeith Packard if (!lessee) { 71662884cd3SKeith Packard ret = -ENOENT; 71762884cd3SKeith Packard goto fail; 71862884cd3SKeith Packard } 71962884cd3SKeith Packard 72062884cd3SKeith Packard /* Lease is not held by lessor */ 72162884cd3SKeith Packard if (lessee->lessor != lessor) { 72262884cd3SKeith Packard ret = -EACCES; 72362884cd3SKeith Packard goto fail; 72462884cd3SKeith Packard } 72562884cd3SKeith Packard 72662884cd3SKeith Packard _drm_lease_revoke(lessee); 72762884cd3SKeith Packard 72862884cd3SKeith Packard fail: 72962884cd3SKeith Packard mutex_unlock(&dev->mode_config.idr_mutex); 73056f0729aSDesmond Cheong Zhi Xi drm_master_put(&lessor); 73162884cd3SKeith Packard 73262884cd3SKeith Packard return ret; 73362884cd3SKeith Packard } 734