/* SPDX-License-Identifier: GPL-2.0 OR MIT */ /************************************************************************** * * Copyright (c) 2009-2023 VMware, Inc., Palo Alto, CA., USA * All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sub license, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice (including the * next paragraph) shall be included in all copies or substantial portions * of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * USE OR OTHER DEALINGS IN THE SOFTWARE. * **************************************************************************/ /* * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> * * While no substantial code is shared, the prime code is inspired by * drm_prime.c, with * Authors: * Dave Airlie <[email protected]> * Rob Clark <[email protected]> */ /** @file ttm_ref_object.c * * Base- and reference object implementation for the various * ttm objects. Implements reference counting, minimal security checks * and release on file close. */ #define pr_fmt(fmt) … #include "ttm_object.h" #include "vmwgfx_drv.h" #include <linux/list.h> #include <linux/spinlock.h> #include <linux/slab.h> #include <linux/atomic.h> #include <linux/module.h> #include <linux/hashtable.h> MODULE_IMPORT_NS(…); #define VMW_TTM_OBJECT_REF_HT_ORDER … /** * struct ttm_object_file * * @tdev: Pointer to the ttm_object_device. * * @lock: Lock that protects the ref_list list and the * ref_hash hash tables. * * @ref_list: List of ttm_ref_objects to be destroyed at * file release. * * @ref_hash: Hash tables of ref objects, one per ttm_ref_type, * for fast lookup of ref objects given a base object. * * @refcount: reference/usage count */ struct ttm_object_file { … }; /* * struct ttm_object_device * * @object_lock: lock that protects idr. * * This is the per-device data structure needed for ttm object management. */ struct ttm_object_device { … }; /* * struct ttm_ref_object * * @hash: Hash entry for the per-file object reference hash. * * @head: List entry for the per-file list of ref-objects. * * @kref: Ref count. * * @obj: Base object this ref object is referencing. * * @ref_type: Type of ref object. * * This is similar to an idr object, but it also has a hash table entry * that allows lookup with a pointer to the referenced object as a key. In * that way, one can easily detect whether a base object is referenced by * a particular ttm_object_file. It also carries a ref count to avoid creating * multiple ref objects if a ttm_object_file references the same base * object more than once. */ struct ttm_ref_object { … }; static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf); static inline struct ttm_object_file * ttm_object_file_ref(struct ttm_object_file *tfile) { … } static int ttm_tfile_find_ref_rcu(struct ttm_object_file *tfile, uint64_t key, struct vmwgfx_hash_item **p_hash) { … } static int ttm_tfile_find_ref(struct ttm_object_file *tfile, uint64_t key, struct vmwgfx_hash_item **p_hash) { … } static void ttm_object_file_destroy(struct kref *kref) { … } static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile) { … } int ttm_base_object_init(struct ttm_object_file *tfile, struct ttm_base_object *base, bool shareable, enum ttm_object_type object_type, void (*refcount_release) (struct ttm_base_object **)) { … } static void ttm_release_base(struct kref *kref) { … } void ttm_base_object_unref(struct ttm_base_object **p_base) { … } struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile, uint64_t key) { … } struct ttm_base_object * ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint64_t key) { … } int ttm_ref_object_add(struct ttm_object_file *tfile, struct ttm_base_object *base, bool *existed, bool require_existed) { … } static void __releases(tfile->lock) __acquires(tfile->lock) ttm_ref_object_release(struct kref *kref) { … } int ttm_ref_object_base_unref(struct ttm_object_file *tfile, unsigned long key) { … } void ttm_object_file_release(struct ttm_object_file **p_tfile) { … } struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev) { … } struct ttm_object_device * ttm_object_device_init(const struct dma_buf_ops *ops) { … } void ttm_object_device_release(struct ttm_object_device **p_tdev) { … } /** * get_dma_buf_unless_doomed - get a dma_buf reference if possible. * * @dmabuf: Non-refcounted pointer to a struct dma-buf. * * Obtain a file reference from a lookup structure that doesn't refcount * the file, but synchronizes with its release method to make sure it has * not been freed yet. See for example kref_get_unless_zero documentation. * Returns true if refcounting succeeds, false otherwise. * * Nobody really wants this as a public API yet, so let it mature here * for some time... */ static bool __must_check get_dma_buf_unless_doomed(struct dma_buf *dmabuf) { … } /** * ttm_prime_refcount_release - refcount release method for a prime object. * * @p_base: Pointer to ttm_base_object pointer. * * This is a wrapper that calls the refcount_release founction of the * underlying object. At the same time it cleans up the prime object. * This function is called when all references to the base object we * derive from are gone. */ static void ttm_prime_refcount_release(struct ttm_base_object **p_base) { … } /** * ttm_prime_dmabuf_release - Release method for the dma-bufs we export * * @dma_buf: * * This function first calls the dma_buf release method the driver * provides. Then it cleans up our dma_buf pointer used for lookup, * and finally releases the reference the dma_buf has on our base * object. */ static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf) { … } /** * ttm_prime_fd_to_handle - Get a base object handle from a prime fd * * @tfile: A struct ttm_object_file identifying the caller. * @fd: The prime / dmabuf fd. * @handle: The returned handle. * * This function returns a handle to an object that previously exported * a dma-buf. Note that we don't handle imports yet, because we simply * have no consumers of that implementation. */ int ttm_prime_fd_to_handle(struct ttm_object_file *tfile, int fd, u32 *handle) { … } /** * ttm_prime_handle_to_fd - Return a dma_buf fd from a ttm prime object * * @tfile: Struct ttm_object_file identifying the caller. * @handle: Handle to the object we're exporting from. * @flags: flags for dma-buf creation. We just pass them on. * @prime_fd: The returned file descriptor. * */ int ttm_prime_handle_to_fd(struct ttm_object_file *tfile, uint32_t handle, uint32_t flags, int *prime_fd) { … } /** * ttm_prime_object_init - Initialize a ttm_prime_object * * @tfile: struct ttm_object_file identifying the caller * @size: The size of the dma_bufs we export. * @prime: The object to be initialized. * @type: See ttm_base_object_init * @refcount_release: See ttm_base_object_init * * Initializes an object which is compatible with the drm_prime model * for data sharing between processes and devices. */ int ttm_prime_object_init(struct ttm_object_file *tfile, size_t size, struct ttm_prime_object *prime, enum ttm_object_type type, void (*refcount_release) (struct ttm_base_object **)) { … }