drm_gem.h revision 1.1 1 /* $NetBSD: drm_gem.h,v 1.1 2018/08/27 01:35:00 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 /** File representing the shmem storage */
62 struct file *filp;
63
64 /* Mapping info for this object */
65 struct drm_vma_offset_node vma_node;
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 /**
98 * dma_buf - dma buf associated with this GEM object
99 *
100 * Pointer to the dma-buf associated with this gem object (either
101 * through importing or exporting). We break the resulting reference
102 * loop when the last gem handle for this object is released.
103 *
104 * Protected by obj->object_name_lock
105 */
106 struct dma_buf *dma_buf;
107
108 /**
109 * import_attach - dma buf attachment backing this object
110 *
111 * Any foreign dma_buf imported as a gem object has this set to the
112 * attachment point for the device. This is invariant over the lifetime
113 * of a gem object.
114 *
115 * The driver's ->gem_free_object callback is responsible for cleaning
116 * up the dma_buf attachment and references acquired at import time.
117 *
118 * Note that the drm gem/prime core does not depend upon drivers setting
119 * this field any more. So for drivers where this doesn't make sense
120 * (e.g. virtual devices or a displaylink behind an usb bus) they can
121 * simply leave it as NULL.
122 */
123 struct dma_buf_attachment *import_attach;
124 };
125
126 void drm_gem_object_release(struct drm_gem_object *obj);
127 void drm_gem_object_free(struct kref *kref);
128 int drm_gem_object_init(struct drm_device *dev,
129 struct drm_gem_object *obj, size_t size);
130 void drm_gem_private_object_init(struct drm_device *dev,
131 struct drm_gem_object *obj, size_t size);
132 void drm_gem_vm_open(struct vm_area_struct *vma);
133 void drm_gem_vm_close(struct vm_area_struct *vma);
134 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
135 struct vm_area_struct *vma);
136 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
137
138 static inline void
139 drm_gem_object_reference(struct drm_gem_object *obj)
140 {
141 kref_get(&obj->refcount);
142 }
143
144 static inline void
145 drm_gem_object_unreference(struct drm_gem_object *obj)
146 {
147 if (obj != NULL) {
148 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
149
150 kref_put(&obj->refcount, drm_gem_object_free);
151 }
152 }
153
154 static inline void
155 drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
156 {
157 struct drm_device *dev;
158
159 if (!obj)
160 return;
161
162 dev = obj->dev;
163 if (kref_put_mutex(&obj->refcount, drm_gem_object_free, &dev->struct_mutex))
164 mutex_unlock(&dev->struct_mutex);
165 else
166 might_lock(&dev->struct_mutex);
167 }
168
169 int drm_gem_handle_create(struct drm_file *file_priv,
170 struct drm_gem_object *obj,
171 u32 *handlep);
172 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
173
174
175 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
176 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
177 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
178
179 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
180 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
181 bool dirty, bool accessed);
182
183 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
184 struct drm_file *filp,
185 u32 handle);
186 int drm_gem_dumb_destroy(struct drm_file *file,
187 struct drm_device *dev,
188 uint32_t handle);
189
190 #endif /* __DRM_GEM_H__ */
191