Keith Packard | 2ed077e | 2017-03-14 22:26:41 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright © 2017 Keith Packard <keithp@keithp.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <drm/drmP.h> |
| 16 | #include "drm_internal.h" |
| 17 | #include "drm_legacy.h" |
| 18 | #include "drm_crtc_internal.h" |
| 19 | #include <drm/drm_lease.h> |
| 20 | #include <drm/drm_auth.h> |
| 21 | #include <drm/drm_crtc_helper.h> |
| 22 | |
| 23 | #define drm_for_each_lessee(lessee, lessor) \ |
| 24 | list_for_each_entry((lessee), &(lessor)->lessees, lessee_list) |
| 25 | |
| 26 | /** |
| 27 | * drm_lease_owner - return ancestor owner drm_master |
| 28 | * @master: drm_master somewhere within tree of lessees and lessors |
| 29 | * |
| 30 | * RETURN: |
| 31 | * |
| 32 | * drm_master at the top of the tree (i.e, with lessor NULL |
| 33 | */ |
| 34 | struct drm_master *drm_lease_owner(struct drm_master *master) |
| 35 | { |
| 36 | while (master->lessor != NULL) |
| 37 | master = master->lessor; |
| 38 | return master; |
| 39 | } |
| 40 | EXPORT_SYMBOL(drm_lease_owner); |
| 41 | |
| 42 | /** |
| 43 | * _drm_find_lessee - find lessee by id (idr_mutex held) |
| 44 | * @master: drm_master of lessor |
| 45 | * @id: lessee_id |
| 46 | * |
| 47 | * RETURN: |
| 48 | * |
| 49 | * drm_master of the lessee if valid, NULL otherwise |
| 50 | */ |
| 51 | |
| 52 | static struct drm_master* |
| 53 | _drm_find_lessee(struct drm_master *master, int lessee_id) |
| 54 | { |
| 55 | lockdep_assert_held(&master->dev->mode_config.idr_mutex); |
| 56 | return idr_find(&drm_lease_owner(master)->lessee_idr, lessee_id); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * _drm_lease_held_master - check to see if an object is leased (or owned) by master (idr_mutex held) |
| 61 | * @master: the master to check the lease status of |
| 62 | * @id: the id to check |
| 63 | * |
| 64 | * Checks if the specified master holds a lease on the object. Return |
| 65 | * value: |
| 66 | * |
| 67 | * true 'master' holds a lease on (or owns) the object |
| 68 | * false 'master' does not hold a lease. |
| 69 | */ |
| 70 | static int _drm_lease_held_master(struct drm_master *master, int id) |
| 71 | { |
| 72 | lockdep_assert_held(&master->dev->mode_config.idr_mutex); |
| 73 | if (master->lessor) |
| 74 | return idr_find(&master->leases, id) != NULL; |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * _drm_has_leased - check to see if an object has been leased (idr_mutex held) |
| 80 | * @master: the master to check the lease status of |
| 81 | * @id: the id to check |
| 82 | * |
| 83 | * Checks if any lessee of 'master' holds a lease on 'id'. Return |
| 84 | * value: |
| 85 | * |
| 86 | * true Some lessee holds a lease on the object. |
| 87 | * false No lessee has a lease on the object. |
| 88 | */ |
| 89 | static bool _drm_has_leased(struct drm_master *master, int id) |
| 90 | { |
| 91 | struct drm_master *lessee; |
| 92 | |
| 93 | lockdep_assert_held(&master->dev->mode_config.idr_mutex); |
| 94 | drm_for_each_lessee(lessee, master) |
| 95 | if (_drm_lease_held_master(lessee, id)) |
| 96 | return true; |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * _drm_lease_held - check drm_mode_object lease status (idr_mutex held) |
| 102 | * @master: the drm_master |
| 103 | * @id: the object id |
| 104 | * |
| 105 | * Checks if the specified master holds a lease on the object. Return |
| 106 | * value: |
| 107 | * |
| 108 | * true 'master' holds a lease on (or owns) the object |
| 109 | * false 'master' does not hold a lease. |
| 110 | */ |
| 111 | bool _drm_lease_held(struct drm_file *file_priv, int id) |
| 112 | { |
| 113 | if (file_priv == NULL || file_priv->master == NULL) |
| 114 | return true; |
| 115 | |
| 116 | return _drm_lease_held_master(file_priv->master, id); |
| 117 | } |
| 118 | EXPORT_SYMBOL(_drm_lease_held); |
| 119 | |
| 120 | /** |
| 121 | * drm_lease_held - check drm_mode_object lease status (idr_mutex not held) |
| 122 | * @master: the drm_master |
| 123 | * @id: the object id |
| 124 | * |
| 125 | * Checks if the specified master holds a lease on the object. Return |
| 126 | * value: |
| 127 | * |
| 128 | * true 'master' holds a lease on (or owns) the object |
| 129 | * false 'master' does not hold a lease. |
| 130 | */ |
| 131 | bool drm_lease_held(struct drm_file *file_priv, int id) |
| 132 | { |
| 133 | struct drm_master *master; |
| 134 | bool ret; |
| 135 | |
| 136 | if (file_priv == NULL || file_priv->master == NULL) |
| 137 | return true; |
| 138 | |
| 139 | master = file_priv->master; |
| 140 | mutex_lock(&master->dev->mode_config.idr_mutex); |
| 141 | ret = _drm_lease_held_master(master, id); |
| 142 | mutex_unlock(&master->dev->mode_config.idr_mutex); |
| 143 | return ret; |
| 144 | } |
| 145 | EXPORT_SYMBOL(drm_lease_held); |
| 146 | |
| 147 | /** |
| 148 | * drm_lease_filter_crtcs - restricted crtc set to leased values (idr_mutex not held) |
| 149 | * @file_priv: requestor file |
| 150 | * @crtcs: bitmask of crtcs to check |
| 151 | * |
| 152 | * Reconstructs a crtc mask based on the crtcs which are visible |
| 153 | * through the specified file. |
| 154 | */ |
| 155 | uint32_t drm_lease_filter_crtcs(struct drm_file *file_priv, uint32_t crtcs_in) |
| 156 | { |
| 157 | struct drm_master *master; |
| 158 | struct drm_device *dev; |
| 159 | struct drm_crtc *crtc; |
| 160 | int count_in, count_out; |
| 161 | uint32_t crtcs_out = 0; |
| 162 | |
| 163 | if (file_priv == NULL || file_priv->master == NULL) |
| 164 | return crtcs_in; |
| 165 | |
| 166 | master = file_priv->master; |
| 167 | dev = master->dev; |
| 168 | |
| 169 | count_in = count_out = 0; |
| 170 | mutex_lock(&master->dev->mode_config.idr_mutex); |
| 171 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 172 | if (_drm_lease_held_master(master, crtc->base.id)) { |
| 173 | uint32_t mask_in = 1ul << count_in; |
| 174 | if ((crtcs_in & mask_in) != 0) { |
| 175 | uint32_t mask_out = 1ul << count_out; |
| 176 | crtcs_out |= mask_out; |
| 177 | } |
| 178 | count_out++; |
| 179 | } |
| 180 | count_in++; |
| 181 | } |
| 182 | mutex_unlock(&master->dev->mode_config.idr_mutex); |
| 183 | return crtcs_out; |
| 184 | } |
| 185 | EXPORT_SYMBOL(drm_lease_filter_crtcs); |
| 186 | |
| 187 | /* |
| 188 | * drm_lease_create - create a new drm_master with leased objects (idr_mutex not held) |
| 189 | * @lessor: lease holder (or owner) of objects |
| 190 | * @leases: objects to lease to the new drm_master |
| 191 | * |
| 192 | * Uses drm_master_create to allocate a new drm_master, then checks to |
| 193 | * make sure all of the desired objects can be leased, atomically |
| 194 | * leasing them to the new drmmaster. |
| 195 | * |
| 196 | * ERR_PTR(-EACCESS) some other master holds the title to any object |
| 197 | * ERR_PTR(-ENOENT) some object is not a valid DRM object for this device |
| 198 | * ERR_PTR(-EBUSY) some other lessee holds title to this object |
| 199 | * ERR_PTR(-EEXIST) same object specified more than once in the provided list |
| 200 | * ERR_PTR(-ENOMEM) allocation failed |
| 201 | */ |
| 202 | static struct drm_master *drm_lease_create(struct drm_master *lessor, struct idr *leases) |
| 203 | { |
| 204 | struct drm_device *dev = lessor->dev; |
| 205 | int error; |
| 206 | struct drm_master *lessee; |
| 207 | int object; |
| 208 | int id; |
| 209 | void *entry; |
| 210 | |
| 211 | DRM_DEBUG_LEASE("lessor %d\n", lessor->lessee_id); |
| 212 | |
| 213 | lessee = drm_master_create(lessor->dev); |
| 214 | if (!lessee) { |
| 215 | DRM_DEBUG_LEASE("drm_master_create failed\n"); |
| 216 | return ERR_PTR(-ENOMEM); |
| 217 | } |
| 218 | |
| 219 | mutex_lock(&dev->mode_config.idr_mutex); |
| 220 | |
| 221 | /* Insert the new lessee into the tree */ |
| 222 | id = idr_alloc(&(drm_lease_owner(lessor)->lessee_idr), lessee, 1, 0, GFP_KERNEL); |
| 223 | if (id < 0) { |
| 224 | error = id; |
| 225 | goto out_lessee; |
| 226 | } |
| 227 | |
| 228 | lessee->lessee_id = id; |
| 229 | lessee->lessor = drm_master_get(lessor); |
| 230 | list_add_tail(&lessee->lessee_list, &lessor->lessees); |
| 231 | |
| 232 | idr_for_each_entry(leases, entry, object) { |
| 233 | error = 0; |
| 234 | if (!idr_find(&dev->mode_config.crtc_idr, object)) |
| 235 | error = -ENOENT; |
| 236 | else if (!_drm_lease_held_master(lessor, object)) |
| 237 | error = -EACCES; |
| 238 | else if (_drm_has_leased(lessor, object)) |
| 239 | error = -EBUSY; |
| 240 | |
| 241 | if (error != 0) { |
| 242 | DRM_DEBUG_LEASE("object %d failed %d\n", object, error); |
| 243 | goto out_lessee; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /* Move the leases over */ |
| 248 | lessee->leases = *leases; |
| 249 | DRM_DEBUG_LEASE("new lessee %d %p, lessor %d %p\n", lessee->lessee_id, lessee, lessor->lessee_id, lessor); |
| 250 | |
| 251 | mutex_unlock(&dev->mode_config.idr_mutex); |
| 252 | return lessee; |
| 253 | |
| 254 | out_lessee: |
| 255 | drm_master_put(&lessee); |
| 256 | |
| 257 | mutex_unlock(&dev->mode_config.idr_mutex); |
| 258 | |
| 259 | return ERR_PTR(error); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * drm_lease_destroy - a master is going away (idr_mutex not held) |
| 264 | * @master: the drm_master being destroyed |
| 265 | * |
| 266 | * All lessees will have been destroyed as they |
| 267 | * hold a reference on their lessor. Notify any |
| 268 | * lessor for this master so that it can check |
| 269 | * the list of lessees. |
| 270 | */ |
| 271 | void drm_lease_destroy(struct drm_master *master) |
| 272 | { |
| 273 | struct drm_device *dev = master->dev; |
| 274 | |
| 275 | mutex_lock(&dev->mode_config.idr_mutex); |
| 276 | |
| 277 | DRM_DEBUG_LEASE("drm_lease_destroy %d\n", master->lessee_id); |
| 278 | |
| 279 | /* This master is referenced by all lessees, hence it cannot be destroyed |
| 280 | * until all of them have been |
| 281 | */ |
| 282 | WARN_ON(!list_empty(&master->lessees)); |
| 283 | |
| 284 | /* Remove this master from the lessee idr in the owner */ |
| 285 | if (master->lessee_id != 0) { |
| 286 | DRM_DEBUG_LEASE("remove master %d from device list of lessees\n", master->lessee_id); |
| 287 | idr_remove(&(drm_lease_owner(master)->lessee_idr), master->lessee_id); |
| 288 | } |
| 289 | |
| 290 | /* Remove this master from any lessee list it may be on */ |
| 291 | list_del(&master->lessee_list); |
| 292 | |
| 293 | mutex_unlock(&dev->mode_config.idr_mutex); |
| 294 | |
| 295 | if (master->lessor) { |
| 296 | /* Tell the master to check the lessee list */ |
| 297 | drm_sysfs_hotplug_event(dev); |
| 298 | drm_master_put(&master->lessor); |
| 299 | } |
| 300 | |
| 301 | DRM_DEBUG_LEASE("drm_lease_destroy done %d\n", master->lessee_id); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * _drm_lease_revoke - revoke access to all leased objects (idr_mutex held) |
| 306 | * @master: the master losing its lease |
| 307 | */ |
| 308 | static void _drm_lease_revoke(struct drm_master *top) |
| 309 | { |
| 310 | int object; |
| 311 | void *entry; |
| 312 | struct drm_master *master = top; |
| 313 | |
| 314 | lockdep_assert_held(&top->dev->mode_config.idr_mutex); |
| 315 | |
| 316 | /* |
| 317 | * Walk the tree starting at 'top' emptying all leases. Because |
| 318 | * the tree is fully connected, we can do this without recursing |
| 319 | */ |
| 320 | for (;;) { |
| 321 | DRM_DEBUG_LEASE("revoke leases for %p %d\n", master, master->lessee_id); |
| 322 | |
| 323 | /* Evacuate the lease */ |
| 324 | idr_for_each_entry(&master->leases, entry, object) |
| 325 | idr_remove(&master->leases, object); |
| 326 | |
| 327 | /* Depth-first list walk */ |
| 328 | |
| 329 | /* Down */ |
| 330 | if (!list_empty(&master->lessees)) { |
| 331 | master = list_first_entry(&master->lessees, struct drm_master, lessee_list); |
| 332 | } else { |
| 333 | /* Up */ |
| 334 | while (master != top && master == list_last_entry(&master->lessor->lessees, struct drm_master, lessee_list)) |
| 335 | master = master->lessor; |
| 336 | |
| 337 | if (master == top) |
| 338 | break; |
| 339 | |
| 340 | /* Over */ |
| 341 | master = list_entry(master->lessee_list.next, struct drm_master, lessee_list); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * drm_lease_revoke - revoke access to all leased objects (idr_mutex not held) |
| 348 | * @top: the master losing its lease |
| 349 | */ |
| 350 | void drm_lease_revoke(struct drm_master *top) |
| 351 | { |
| 352 | mutex_lock(&top->dev->mode_config.idr_mutex); |
| 353 | _drm_lease_revoke(top); |
| 354 | mutex_unlock(&top->dev->mode_config.idr_mutex); |
| 355 | } |