xref: /dragonfly/sys/dev/drm/include/drm/drm_gem.h (revision 0ca59c34)
1 #ifndef __DRM_GEM_H__
2 #define __DRM_GEM_H__
3 
4 /*
5  * GEM Graphics Execution Manager Driver Interfaces
6  *
7  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
8  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
9  * Copyright (c) 2009-2010, Code Aurora Forum.
10  * All rights reserved.
11  * Copyright © 2014 Intel Corporation
12  *   Daniel Vetter <daniel.vetter@ffwll.ch>
13  *
14  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
15  * Author: Gareth Hughes <gareth@valinux.com>
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining a
18  * copy of this software and associated documentation files (the "Software"),
19  * to deal in the Software without restriction, including without limitation
20  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21  * and/or sell copies of the Software, and to permit persons to whom the
22  * Software is furnished to do so, subject to the following conditions:
23  *
24  * The above copyright notice and this permission notice (including the next
25  * paragraph) shall be included in all copies or substantial portions of the
26  * Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
31  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  * OTHER DEALINGS IN THE SOFTWARE.
35  */
36 
37 /**
38  * This structure defines the drm_mm memory object, which will be used by the
39  * DRM for its buffer objects.
40  */
41 struct drm_gem_object {
42 	/** Reference count of this object */
43 	struct kref refcount;
44 
45 	/**
46 	 * handle_count - gem file_priv handle count of this object
47 	 *
48 	 * Each handle also holds a reference. Note that when the handle_count
49 	 * drops to 0 any global names (e.g. the id in the flink namespace) will
50 	 * be cleared.
51 	 *
52 	 * Protected by dev->object_name_lock.
53 	 * */
54 	unsigned handle_count;
55 
56 	/** Related drm device */
57 	struct drm_device *dev;
58 
59 	/** File representing the shmem storage: filp in Linux parlance */
60 	vm_object_t vm_obj;
61 
62 	/* Mapping info for this object */
63 	struct drm_vma_offset_node vma_node;
64 	bool on_map;
65 	struct drm_hash_item map_list;
66 
67 	/**
68 	 * Size of the object, in bytes.  Immutable over the object's
69 	 * lifetime.
70 	 */
71 	size_t size;
72 
73 	/**
74 	 * Global name for this object, starts at 1. 0 means unnamed.
75 	 * Access is covered by the object_name_lock in the related drm_device
76 	 */
77 	int name;
78 
79 	/**
80 	 * Memory domains. These monitor which caches contain read/write data
81 	 * related to the object. When transitioning from one set of domains
82 	 * to another, the driver is called to ensure that caches are suitably
83 	 * flushed and invalidated
84 	 */
85 	uint32_t read_domains;
86 	uint32_t write_domain;
87 
88 	/**
89 	 * While validating an exec operation, the
90 	 * new read/write domain values are computed here.
91 	 * They will be transferred to the above values
92 	 * at the point that any cache flushing occurs
93 	 */
94 	uint32_t pending_read_domains;
95 	uint32_t pending_write_domain;
96 
97 #ifdef DUMBBELL_WIP
98 	/* dma buf exported from this GEM object */
99 	struct dma_buf *export_dma_buf;
100 
101 	/* dma buf attachment backing this object */
102 	struct dma_buf_attachment *import_attach;
103 #endif /* DUMBBELL_WIP */
104 };
105 
106 void drm_gem_object_release(struct drm_gem_object *obj);
107 void drm_gem_object_free(struct kref *kref);
108 int drm_gem_object_init(struct drm_device *dev,
109 			struct drm_gem_object *obj, size_t size);
110 void drm_gem_private_object_init(struct drm_device *dev,
111 				 struct drm_gem_object *obj, size_t size);
112 void drm_gem_vm_open(struct vm_area_struct *vma);
113 void drm_gem_vm_close(struct vm_area_struct *vma);
114 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
115 		     struct vm_area_struct *vma);
116 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
117 
118 int i915_gem_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
119     vm_ooffset_t foff, struct ucred *cred, u_short *color);
120 void i915_gem_pager_dtor(void * handle);
121 
122 static inline void
123 drm_gem_object_reference(struct drm_gem_object *obj)
124 {
125 	kref_get(&obj->refcount);
126 }
127 
128 static inline void
129 drm_gem_object_unreference(struct drm_gem_object *obj)
130 {
131 	if (obj != NULL)
132 		kref_put(&obj->refcount, drm_gem_object_free);
133 }
134 
135 static inline void
136 drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
137 {
138 	if (obj != NULL) {
139 		struct drm_device *dev = obj->dev;
140 		DRM_LOCK(dev);
141 		kref_put(&obj->refcount, drm_gem_object_free);
142 		DRM_UNLOCK(dev);
143 	}
144 }
145 
146 int drm_gem_handle_create(struct drm_file *file_priv,
147 			  struct drm_gem_object *obj,
148 			  u32 *handlep);
149 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
150 
151 
152 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
153 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
154 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
155 
156 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
157 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
158 		bool dirty, bool accessed);
159 
160 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
161 					     struct drm_file *filp,
162 					     u32 handle);
163 int drm_gem_dumb_destroy(struct drm_file *file,
164 			 struct drm_device *dev,
165 			 uint32_t handle);
166 
167 #endif /* __DRM_GEM_H__ */
168