drm/linux: add sg_pcopy_{from/to}_buffer()
[dragonfly.git] / sys / dev / drm / include / drm / drm_gem.h
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 /* BSD specific gem helpers. */
38 #define DRM_GEM_MAPPING_MASK    (3ULL << 62)
39 #define DRM_GEM_MAPPING_KEY     (2ULL << 62) /* Non-canonical address form */
40 #define DRM_GEM_MAX_IDX         0x3fffff
41 #define DRM_GEM_MAPPING_IDX(o)  (((o) >> 40) & DRM_GEM_MAX_IDX)
42 #define DRM_GEM_MAPPING_OFF(i)  (((uint64_t)(i)) << 40)
43 #define DRM_GEM_MAPPING_MAPOFF(o) \
44     ((o) & ~(DRM_GEM_MAPPING_OFF(DRM_GEM_MAX_IDX) | DRM_GEM_MAPPING_KEY))
45
46 /**
47  * This structure defines the drm_mm memory object, which will be used by the
48  * DRM for its buffer objects.
49  */
50 struct drm_gem_object {
51         /** Reference count of this object */
52         struct kref refcount;
53
54         /**
55          * handle_count - gem file_priv handle count of this object
56          *
57          * Each handle also holds a reference. Note that when the handle_count
58          * drops to 0 any global names (e.g. the id in the flink namespace) will
59          * be cleared.
60          *
61          * Protected by dev->object_name_lock.
62          * */
63         unsigned handle_count;
64
65         /** Related drm device */
66         struct drm_device *dev;
67
68         /** File representing the shmem storage: filp in Linux parlance */
69         vm_object_t vm_obj;
70
71         /* Mapping info for this object */
72         struct drm_vma_offset_node vma_node;
73         bool on_map;
74         struct drm_hash_item map_list;
75
76         /**
77          * Size of the object, in bytes.  Immutable over the object's
78          * lifetime.
79          */
80         size_t size;
81
82         /**
83          * Global name for this object, starts at 1. 0 means unnamed.
84          * Access is covered by the object_name_lock in the related drm_device
85          */
86         int name;
87
88         /**
89          * Memory domains. These monitor which caches contain read/write data
90          * related to the object. When transitioning from one set of domains
91          * to another, the driver is called to ensure that caches are suitably
92          * flushed and invalidated
93          */
94         uint32_t read_domains;
95         uint32_t write_domain;
96
97         /**
98          * While validating an exec operation, the
99          * new read/write domain values are computed here.
100          * They will be transferred to the above values
101          * at the point that any cache flushing occurs
102          */
103         uint32_t pending_read_domains;
104         uint32_t pending_write_domain;
105
106         /* dma buf exported from this GEM object */
107         struct dma_buf *export_dma_buf;
108
109         /* dma buf attachment backing this object */
110         struct dma_buf_attachment *import_attach;
111
112         /**
113          * dumb - created as dumb buffer
114          * Whether the gem object was created using the dumb buffer interface
115          * as such it may not be used for GPU rendering.
116          */
117         bool dumb;
118 };
119
120 void drm_gem_object_release(struct drm_gem_object *obj);
121 void drm_gem_object_free(struct kref *kref);
122 int drm_gem_object_init(struct drm_device *dev,
123                         struct drm_gem_object *obj, size_t size);
124 void drm_gem_private_object_init(struct drm_device *dev,
125                                  struct drm_gem_object *obj, size_t size);
126 void drm_gem_vm_open(struct vm_area_struct *vma);
127 void drm_gem_vm_close(struct vm_area_struct *vma);
128 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
129                      struct vm_area_struct *vma);
130 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
131
132 int i915_gem_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
133     vm_ooffset_t foff, struct ucred *cred, u_short *color);
134 void i915_gem_pager_dtor(void * handle);
135
136 static inline void
137 drm_gem_object_reference(struct drm_gem_object *obj)
138 {
139         kref_get(&obj->refcount);
140 }
141
142 static inline void
143 drm_gem_object_unreference(struct drm_gem_object *obj)
144 {
145         if (obj != NULL)
146                 kref_put(&obj->refcount, drm_gem_object_free);
147 }
148
149 static inline void
150 drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
151 {
152         if (obj != NULL) {
153                 struct drm_device *dev = obj->dev;
154                 DRM_LOCK(dev);
155                 kref_put(&obj->refcount, drm_gem_object_free);
156                 DRM_UNLOCK(dev);
157         }
158 }
159
160 int drm_gem_handle_create(struct drm_file *file_priv,
161                           struct drm_gem_object *obj,
162                           u32 *handlep);
163 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
164
165
166 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
167 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
168 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
169
170 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
171 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
172                 bool dirty, bool accessed);
173
174 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
175                                              struct drm_file *filp,
176                                              u32 handle);
177 int drm_gem_dumb_destroy(struct drm_file *file,
178                          struct drm_device *dev,
179                          uint32_t handle);
180
181 #endif /* __DRM_GEM_H__ */