1210 lines · 29.9 KB · C
RawDownload
/* * Copyright 2017 Red Hat * Parts ported from amdgpu (fence wait code). * Copyright 2016 Advanced Micro Devices, Inc. * * 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, sublicense, * 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 NONINFRINGEMENT.  IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS 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: * */ /** * DOC: Overview * * DRM synchronisation objects (syncobj, see struct &drm_syncobj) are * persistent objects that contain an optional fence. The fence can be updated * with a new fence, or be NULL. * * syncobj's can be waited upon, where it will wait for the underlying * fence. * * syncobj's can be export to fd's and back, these fd's are opaque and * have no other use case, except passing the syncobj between processes. * * Their primary use-case is to implement Vulkan fences and semaphores. * * syncobj have a kref reference count, but also have an optional file. * The file is only created once the syncobj is exported. * The file takes a reference on the kref. */ #include <drm/drmP.h>#include <linux/file.h>#include <linux/fs.h>#include <linux/anon_inodes.h>#include <linux/sync_file.h>#include <linux/sched/signal.h> #include "drm_internal.h"#include <drm/drm_syncobj.h> /* merge normal syncobj to timeline syncobj, the point interval is 1 */#define DRM_SYNCOBJ_BINARY_POINT 1 struct drm_syncobj_stub_fence {	struct dma_fence base;	spinlock_t lock;}; static const char *drm_syncobj_stub_fence_get_name(struct dma_fence *fence){        return "syncobjstub";} static const struct dma_fence_ops drm_syncobj_stub_fence_ops = {	.get_driver_name = drm_syncobj_stub_fence_get_name,	.get_timeline_name = drm_syncobj_stub_fence_get_name,}; struct drm_syncobj_signal_pt {	struct dma_fence_array *fence_array;	u64    value;	struct list_head list;}; static DEFINE_SPINLOCK(signaled_fence_lock);static struct dma_fence signaled_fence; static struct dma_fence *drm_syncobj_get_stub_fence(void){	spin_lock(&signaled_fence_lock);	if (!signaled_fence.ops) {		dma_fence_init(&signaled_fence,			       &drm_syncobj_stub_fence_ops,			       &signaled_fence_lock,			       0, 0);		dma_fence_signal_locked(&signaled_fence);	}	spin_unlock(&signaled_fence_lock); 	return dma_fence_get(&signaled_fence);}/** * drm_syncobj_find - lookup and reference a sync object. * @file_private: drm file private pointer * @handle: sync object handle to lookup. * * Returns a reference to the syncobj pointed to by handle or NULL. The * reference must be released by calling drm_syncobj_put(). */struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,				     u32 handle){	struct drm_syncobj *syncobj; 	spin_lock(&file_private->syncobj_table_lock); 	/* Check if we currently have a reference on the object */	syncobj = idr_find(&file_private->syncobj_idr, handle);	if (syncobj)		drm_syncobj_get(syncobj); 	spin_unlock(&file_private->syncobj_table_lock); 	return syncobj;}EXPORT_SYMBOL(drm_syncobj_find); static struct dma_fence *drm_syncobj_find_signal_pt_for_point(struct drm_syncobj *syncobj,				     uint64_t point){	struct drm_syncobj_signal_pt *signal_pt; 	if ((syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) &&	    (point <= syncobj->timeline))		return drm_syncobj_get_stub_fence(); 	list_for_each_entry(signal_pt, &syncobj->signal_pt_list, list) {		if (point > signal_pt->value)			continue;		if ((syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) &&		    (point != signal_pt->value))			continue;		return dma_fence_get(&signal_pt->fence_array->base);	}	return NULL;} static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj,					    struct drm_syncobj_cb *cb,					    drm_syncobj_func_t func){	cb->func = func;	list_add_tail(&cb->node, &syncobj->cb_list);} static void drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj,						  struct dma_fence **fence,						  struct drm_syncobj_cb *cb,						  drm_syncobj_func_t func){	u64 pt_value = 0; 	WARN_ON(*fence); 	if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) {		/*BINARY syncobj always wait on last pt */		pt_value = syncobj->signal_point; 		if (pt_value == 0)			pt_value += DRM_SYNCOBJ_BINARY_POINT;	} 	mutex_lock(&syncobj->cb_mutex);	spin_lock(&syncobj->pt_lock);	*fence = drm_syncobj_find_signal_pt_for_point(syncobj, pt_value);	spin_unlock(&syncobj->pt_lock);	if (!*fence)		drm_syncobj_add_callback_locked(syncobj, cb, func);	mutex_unlock(&syncobj->cb_mutex);} static void drm_syncobj_remove_callback(struct drm_syncobj *syncobj,					struct drm_syncobj_cb *cb){	mutex_lock(&syncobj->cb_mutex);	list_del_init(&cb->node);	mutex_unlock(&syncobj->cb_mutex);} static void drm_syncobj_init(struct drm_syncobj *syncobj){	spin_lock(&syncobj->pt_lock);	syncobj->timeline_context = dma_fence_context_alloc(1);	syncobj->timeline = 0;	syncobj->signal_point = 0;	init_waitqueue_head(&syncobj->wq); 	INIT_LIST_HEAD(&syncobj->signal_pt_list);	spin_unlock(&syncobj->pt_lock);} static void drm_syncobj_fini(struct drm_syncobj *syncobj){	struct drm_syncobj_signal_pt *signal_pt = NULL, *tmp; 	spin_lock(&syncobj->pt_lock);	list_for_each_entry_safe(signal_pt, tmp,				 &syncobj->signal_pt_list, list) {		list_del(&signal_pt->list);		dma_fence_put(&signal_pt->fence_array->base);		kfree(signal_pt);	}	spin_unlock(&syncobj->pt_lock);} static int drm_syncobj_create_signal_pt(struct drm_syncobj *syncobj,					struct dma_fence *fence,					u64 point){	struct drm_syncobj_signal_pt *signal_pt =		kzalloc(sizeof(struct drm_syncobj_signal_pt), GFP_KERNEL);	struct drm_syncobj_signal_pt *tail_pt;	struct dma_fence **fences;	int num_fences = 0;	int ret = 0, i; 	if (!signal_pt)		return -ENOMEM;	if (!fence)		goto out; 	fences = kmalloc_array(sizeof(void *), 2, GFP_KERNEL);	if (!fences) {		ret = -ENOMEM;		goto out;	}	fences[num_fences++] = dma_fence_get(fence);	/* timeline syncobj must take this dependency */	if (syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) {		spin_lock(&syncobj->pt_lock);		if (!list_empty(&syncobj->signal_pt_list)) {			tail_pt = list_last_entry(&syncobj->signal_pt_list,						  struct drm_syncobj_signal_pt, list);			fences[num_fences++] =				dma_fence_get(&tail_pt->fence_array->base);		}		spin_unlock(&syncobj->pt_lock);	}	signal_pt->fence_array = dma_fence_array_create(num_fences, fences,							syncobj->timeline_context,							point, false);	if (!signal_pt->fence_array) {		ret = -ENOMEM;		goto fail;	} 	spin_lock(&syncobj->pt_lock);	if (syncobj->signal_point >= point) {		DRM_WARN("A later signal is ready!");		spin_unlock(&syncobj->pt_lock);		goto exist;	}	signal_pt->value = point;	list_add_tail(&signal_pt->list, &syncobj->signal_pt_list);	syncobj->signal_point = point;	spin_unlock(&syncobj->pt_lock);	wake_up_all(&syncobj->wq); 	return 0;exist:	dma_fence_put(&signal_pt->fence_array->base);fail:	for (i = 0; i < num_fences; i++)		dma_fence_put(fences[i]);	kfree(fences);out:	kfree(signal_pt);	return ret;} static void drm_syncobj_garbage_collection(struct drm_syncobj *syncobj){	struct drm_syncobj_signal_pt *signal_pt, *tmp, *tail_pt; 	spin_lock(&syncobj->pt_lock);	tail_pt = list_last_entry(&syncobj->signal_pt_list,				  struct drm_syncobj_signal_pt,				  list);	list_for_each_entry_safe(signal_pt, tmp,				 &syncobj->signal_pt_list, list) {		if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY &&		    signal_pt == tail_pt)			continue;		if (dma_fence_is_signaled(&signal_pt->fence_array->base)) {			syncobj->timeline = signal_pt->value;			list_del(&signal_pt->list);			dma_fence_put(&signal_pt->fence_array->base);			kfree(signal_pt);		} else {			/*signal_pt is in order in list, from small to big, so			 * the later must not be signal either */			break;		}	} 	spin_unlock(&syncobj->pt_lock);}/** * drm_syncobj_replace_fence - replace fence in a sync object. * @syncobj: Sync object to replace fence in * @point: timeline point * @fence: fence to install in sync file. * * This replaces the fence on a sync object, or a timeline point fence. */void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,			       u64 point,			       struct dma_fence *fence){	u64 pt_value = point; 	drm_syncobj_garbage_collection(syncobj);	if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) {		if (!fence) {			drm_syncobj_fini(syncobj);			drm_syncobj_init(syncobj);			return;		}		pt_value = syncobj->signal_point +			DRM_SYNCOBJ_BINARY_POINT;	}	drm_syncobj_create_signal_pt(syncobj, fence, pt_value);	if (fence) {		struct drm_syncobj_cb *cur, *tmp;		LIST_HEAD(cb_list); 		mutex_lock(&syncobj->cb_mutex);		list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {			list_del_init(&cur->node);			cur->func(syncobj, cur);		}		mutex_unlock(&syncobj->cb_mutex);	}}EXPORT_SYMBOL(drm_syncobj_replace_fence); static int drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj){	struct drm_syncobj_stub_fence *fence;	fence = kzalloc(sizeof(*fence), GFP_KERNEL);	if (fence == NULL)		return -ENOMEM; 	spin_lock_init(&fence->lock);	dma_fence_init(&fence->base, &drm_syncobj_stub_fence_ops,		       &fence->lock, 0, 0);	dma_fence_signal(&fence->base); 	drm_syncobj_replace_fence(syncobj, 0, &fence->base); 	dma_fence_put(&fence->base); 	return 0;} static intdrm_syncobj_point_get(struct drm_syncobj *syncobj, u64 point, u64 flags,		      struct dma_fence **fence){	int ret = 0; 	if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {		ret = wait_event_interruptible(syncobj->wq,					       point <= syncobj->signal_point);		if (ret < 0)			return ret;	}	spin_lock(&syncobj->pt_lock);	*fence = drm_syncobj_find_signal_pt_for_point(syncobj, point);	if (!*fence)		ret = -EINVAL;	spin_unlock(&syncobj->pt_lock);	return ret;} /** * drm_syncobj_search_fence - lookup and reference the fence in a sync object or * in a timeline point * @syncobj: sync object pointer * @point: timeline point * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not * @fence: out parameter for the fence * * if flags is DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT, the function will block * here until specific timeline points is reached. * if not, you need a submit thread and block in userspace until all future * timeline points have materialized, only then you can submit to the kernel, * otherwise, function will fail to return fence. * * Returns 0 on success or a negative error value on failure. On success @fence * contains a reference to the fence, which must be released by calling * dma_fence_put(). */int drm_syncobj_search_fence(struct drm_syncobj *syncobj, u64 point,			     u64 flags, struct dma_fence **fence){	u64 pt_value = point; 	if (!syncobj)		return -ENOENT; 	drm_syncobj_garbage_collection(syncobj);	if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) {		/*BINARY syncobj always wait on last pt */		pt_value = syncobj->signal_point; 		if (pt_value == 0)			pt_value += DRM_SYNCOBJ_BINARY_POINT;	}	return drm_syncobj_point_get(syncobj, pt_value, flags, fence);}EXPORT_SYMBOL(drm_syncobj_search_fence); /** * drm_syncobj_find_fence - lookup and reference the fence in a sync object * @file_private: drm file private pointer * @handle: sync object handle to lookup. * @point: timeline point * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not * @fence: out parameter for the fence * * This is just a convenience function that combines drm_syncobj_find() and * drm_syncobj_lookup_fence(). * * Returns 0 on success or a negative error value on failure. On success @fence * contains a reference to the fence, which must be released by calling * dma_fence_put(). */int drm_syncobj_find_fence(struct drm_file *file_private,			   u32 handle, u64 point, u64 flags,			   struct dma_fence **fence){	struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);	int ret; 	ret = drm_syncobj_search_fence(syncobj, point, flags, fence);	drm_syncobj_put(syncobj);	return ret;}EXPORT_SYMBOL(drm_syncobj_find_fence); /** * drm_syncobj_free - free a sync object. * @kref: kref to free. * * Only to be called from kref_put in drm_syncobj_put. */void drm_syncobj_free(struct kref *kref){	struct drm_syncobj *syncobj = container_of(kref,						   struct drm_syncobj,						   refcount);	drm_syncobj_fini(syncobj);	kfree(syncobj);}EXPORT_SYMBOL(drm_syncobj_free); /** * drm_syncobj_create - create a new syncobj * @out_syncobj: returned syncobj * @flags: DRM_SYNCOBJ_* flags * @fence: if non-NULL, the syncobj will represent this fence * * This is the first function to create a sync object. After creating, drivers * probably want to make it available to userspace, either through * drm_syncobj_get_handle() or drm_syncobj_get_fd(). * * Returns 0 on success or a negative error value on failure. */int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,		       struct dma_fence *fence){	int ret;	struct drm_syncobj *syncobj; 	syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);	if (!syncobj)		return -ENOMEM; 	kref_init(&syncobj->refcount);	INIT_LIST_HEAD(&syncobj->cb_list);	spin_lock_init(&syncobj->pt_lock);	mutex_init(&syncobj->cb_mutex);	if (flags & DRM_SYNCOBJ_CREATE_TYPE_TIMELINE)		syncobj->type = DRM_SYNCOBJ_TYPE_TIMELINE;	else		syncobj->type = DRM_SYNCOBJ_TYPE_BINARY;	drm_syncobj_init(syncobj); 	if (flags & DRM_SYNCOBJ_CREATE_SIGNALED) {		ret = drm_syncobj_assign_null_handle(syncobj);

Lines 1–500 of 1210.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t