drm_gem.h revision 1.3 1 /* $NetBSD: drm_gem.h,v 1.3 2018/08/27 07:19:01 riastradh Exp $ */
2
3 #ifndef __DRM_GEM_H__
4 #define __DRM_GEM_H__
5
6 /*
7 * GEM Graphics Execution Manager Driver Interfaces
8 *
9 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
10 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
11 * Copyright (c) 2009-2010, Code Aurora Forum.
12 * All rights reserved.
13 * Copyright 2014 Intel Corporation
14 * Daniel Vetter <daniel.vetter (at) ffwll.ch>
15 *
16 * Author: Rickard E. (Rik) Faith <faith (at) valinux.com>
17 * Author: Gareth Hughes <gareth (at) valinux.com>
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining a
20 * copy of this software and associated documentation files (the "Software"),
21 * to deal in the Software without restriction, including without limitation
22 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
23 * and/or sell copies of the Software, and to permit persons to whom the
24 * Software is furnished to do so, subject to the following conditions:
25 *
26 * The above copyright notice and this permission notice (including the next
27 * paragraph) shall be included in all copies or substantial portions of the
28 * Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
33 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
34 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
35 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
36 * OTHER DEALINGS IN THE SOFTWARE.
37 */
38
39 /**
40 * This structure defines the drm_mm memory object, which will be used by the
41 * DRM for its buffer objects.
42 */
43 struct drm_gem_object {
44 /** Reference count of this object */
45 struct kref refcount;
46
47 /**
48 * handle_count - gem file_priv handle count of this object
49 *
50 * Each handle also holds a reference. Note that when the handle_count
51 * drops to 0 any global names (e.g. the id in the flink namespace) will
52 * be cleared.
53 *
54 * Protected by dev->object_name_lock.
55 * */
56 unsigned handle_count;
57
58 /** Related drm device */
59 struct drm_device *dev;
60
61 #ifdef __NetBSD__
62 /* UVM anonymous object for shared memory mappings. */
63 struct uvm_object *filp;
64
65 /* UVM object with custom pager ops for device memory mappings. */
66 struct uvm_object gemo_uvmobj;
67 #else
68 /** File representing the shmem storage */
69 struct file *filp;
70 #endif
71
72 /* Mapping info for this object */
73 struct drm_vma_offset_node vma_node;
74
75 /**
76 * Size of the object, in bytes. Immutable over the object's
77 * lifetime.
78 */
79 size_t size;
80
81 /**
82 * Global name for this object, starts at 1. 0 means unnamed.
83 * Access is covered by the object_name_lock in the related drm_device
84 */
85 int name;
86
87 /**
88 * Memory domains. These monitor which caches contain read/write data
89 * related to the object. When transitioning from one set of domains
90 * to another, the driver is called to ensure that caches are suitably
91 * flushed and invalidated
92 */
93 uint32_t read_domains;
94 uint32_t write_domain;
95
96 /**
97 * While validating an exec operation, the
98 * new read/write domain values are computed here.
99 * They will be transferred to the above values
100 * at the point that any cache flushing occurs
101 */
102 uint32_t pending_read_domains;
103 uint32_t pending_write_domain;
104
105 #ifndef __NetBSD__ /* XXX drm prime */
106 /**
107 * dma_buf - dma buf associated with this GEM object
108 *
109 * Pointer to the dma-buf associated with this gem object (either
110 * through importing or exporting). We break the resulting reference
111 * loop when the last gem handle for this object is released.
112 *
113 * Protected by obj->object_name_lock
114 */
115 struct dma_buf *dma_buf;
116
117 /**
118 * import_attach - dma buf attachment backing this object
119 *
120 * Any foreign dma_buf imported as a gem object has this set to the
121 * attachment point for the device. This is invariant over the lifetime
122 * of a gem object.
123 *
124 * The driver's ->gem_free_object callback is responsible for cleaning
125 * up the dma_buf attachment and references acquired at import time.
126 *
127 * Note that the drm gem/prime core does not depend upon drivers setting
128 * this field any more. So for drivers where this doesn't make sense
129 * (e.g. virtual devices or a displaylink behind an usb bus) they can
130 * simply leave it as NULL.
131 */
132 struct dma_buf_attachment *import_attach;
133 #endif
134 };
135
136 void drm_gem_object_release(struct drm_gem_object *obj);
137 void drm_gem_object_free(struct kref *kref);
138 int drm_gem_object_init(struct drm_device *dev,
139 struct drm_gem_object *obj, size_t size);
140 void drm_gem_private_object_init(struct drm_device *dev,
141 struct drm_gem_object *obj, size_t size);
142 #ifdef __NetBSD__
143 void drm_gem_pager_reference(struct uvm_object *);
144 void drm_gem_pager_detach(struct uvm_object *);
145 int drm_gem_mmap_object(struct drm_device *, off_t, size_t, int,
146 struct uvm_object **, voff_t *, struct file *);
147 int drm_gem_or_legacy_mmap_object(struct drm_device *, off_t, size_t, int,
148 struct uvm_object **, voff_t *, struct file *);
149 #else
150 void drm_gem_vm_open(struct vm_area_struct *vma);
151 void drm_gem_vm_close(struct vm_area_struct *vma);
152 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
153 struct vm_area_struct *vma);
154 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
155 #endif
156
157 static inline void
158 drm_gem_object_reference(struct drm_gem_object *obj)
159 {
160 kref_get(&obj->refcount);
161 }
162
163 static inline void
164 drm_gem_object_unreference(struct drm_gem_object *obj)
165 {
166 if (obj != NULL) {
167 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
168
169 kref_put(&obj->refcount, drm_gem_object_free);
170 }
171 }
172
173 static inline void
174 drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
175 {
176 struct drm_device *dev;
177
178 if (!obj)
179 return;
180
181 dev = obj->dev;
182 if (kref_put_mutex(&obj->refcount, drm_gem_object_free, &dev->struct_mutex))
183 mutex_unlock(&dev->struct_mutex);
184 else
185 might_lock(&dev->struct_mutex);
186 }
187
188 int drm_gem_handle_create(struct drm_file *file_priv,
189 struct drm_gem_object *obj,
190 u32 *handlep);
191 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
192
193
194 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
195 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
196 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
197
198 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
199 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
200 bool dirty, bool accessed);
201
202 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
203 struct drm_file *filp,
204 u32 handle);
205 int drm_gem_dumb_destroy(struct drm_file *file,
206 struct drm_device *dev,
207 uint32_t handle);
208
209 #endif /* __DRM_GEM_H__ */
210