1 /**************************************************************************
3 * Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
31 #ifndef _DRM_OBJECTS_H
32 #define _DRM_OJBECTS_H
37 /***************************************************
38 * User space objects. (drm_object.c)
41 #define drm_user_object_entry(_ptr, _type, _member) container_of(_ptr, _type, _member)
48 * Add other user space object types here.
53 * A user object is a structure that helps the drm give out user handles
54 * to kernel internal objects and to keep track of these objects so that
55 * they can be destroyed, for example when the user space process exits.
56 * Designed to be accessible using a user space 32-bit handle.
59 typedef struct drm_user_object {
61 struct list_head list;
62 drm_object_type_t type;
66 void (*ref_struct_locked) (drm_file_t * priv,
67 struct drm_user_object * obj,
68 drm_ref_t ref_action);
69 void (*unref) (drm_file_t * priv, struct drm_user_object * obj,
70 drm_ref_t unref_action);
71 void (*remove) (drm_file_t * priv, struct drm_user_object * obj);
75 * A ref object is a structure which is used to
76 * keep track of references to user objects and to keep track of these
77 * references so that they can be destroyed for example when the user space
78 * process exits. Designed to be accessible using a pointer to the _user_ object.
81 typedef struct drm_ref_object {
83 struct list_head list;
85 drm_ref_t unref_action;
89 * Must be called with the struct_mutex held.
92 extern int drm_add_user_object(drm_file_t * priv, drm_user_object_t * item,
95 * Must be called with the struct_mutex held.
98 extern drm_user_object_t *drm_lookup_user_object(drm_file_t * priv,
102 * Must be called with the struct_mutex held.
103 * If "item" has been obtained by a call to drm_lookup_user_object. You may not
104 * release the struct_mutex before calling drm_remove_ref_object.
105 * This function may temporarily release the struct_mutex.
108 extern int drm_remove_user_object(drm_file_t * priv, drm_user_object_t * item);
111 * Must be called with the struct_mutex held. May temporarily release it.
114 extern int drm_add_ref_object(drm_file_t * priv,
115 drm_user_object_t * referenced_object,
116 drm_ref_t ref_action);
119 * Must be called with the struct_mutex held.
122 drm_ref_object_t *drm_lookup_ref_object(drm_file_t * priv,
123 drm_user_object_t * referenced_object,
124 drm_ref_t ref_action);
126 * Must be called with the struct_mutex held.
127 * If "item" has been obtained by a call to drm_lookup_ref_object. You may not
128 * release the struct_mutex before calling drm_remove_ref_object.
129 * This function may temporarily release the struct_mutex.
132 extern void drm_remove_ref_object(drm_file_t * priv, drm_ref_object_t * item);
133 extern int drm_user_object_ref(drm_file_t * priv, uint32_t user_token,
134 drm_object_type_t type,
135 drm_user_object_t ** object);
136 extern int drm_user_object_unref(drm_file_t * priv, uint32_t user_token,
137 drm_object_type_t type);
139 /***************************************************
140 * Fence objects. (drm_fence.c)
143 typedef struct drm_fence_object {
144 drm_user_object_t base;
148 * The below three fields are protected by the fence manager spinlock.
151 struct list_head ring;
153 uint32_t native_type;
158 uint32_t submitted_flush;
159 } drm_fence_object_t;
161 #define _DRM_FENCE_CLASSES 8
162 #define _DRM_FENCE_TYPE_EXE 0x00
164 typedef struct drm_fence_class_manager {
165 struct list_head ring;
166 uint32_t pending_flush;
167 wait_queue_head_t fence_queue;
168 int pending_exe_flush;
169 uint32_t last_exe_flush;
170 uint32_t exe_flush_sequence;
171 } drm_fence_class_manager_t;
173 typedef struct drm_fence_manager {
176 drm_fence_class_manager_t class[_DRM_FENCE_CLASSES];
177 uint32_t num_classes;
179 } drm_fence_manager_t;
181 typedef struct drm_fence_driver {
182 uint32_t num_classes;
185 uint32_t sequence_mask;
187 int (*has_irq) (struct drm_device * dev, uint32_t class,
189 int (*emit) (struct drm_device * dev, uint32_t class, uint32_t flags,
190 uint32_t * breadcrumb, uint32_t * native_type);
191 void (*poke_flush) (struct drm_device * dev, uint32_t class);
192 } drm_fence_driver_t;
194 extern void drm_fence_handler(struct drm_device *dev, uint32_t class,
195 uint32_t sequence, uint32_t type);
196 extern void drm_fence_manager_init(struct drm_device *dev);
197 extern void drm_fence_manager_takedown(struct drm_device *dev);
198 extern void drm_fence_flush_old(struct drm_device *dev, uint32_t class,
200 extern int drm_fence_object_flush(struct drm_device *dev,
201 drm_fence_object_t * fence, uint32_t type);
202 extern int drm_fence_object_signaled(drm_fence_object_t * fence, uint32_t type);
203 extern void drm_fence_usage_deref_locked(struct drm_device *dev,
204 drm_fence_object_t * fence);
205 extern void drm_fence_usage_deref_unlocked(struct drm_device *dev,
206 drm_fence_object_t * fence);
207 extern int drm_fence_object_wait(struct drm_device *dev,
208 drm_fence_object_t * fence,
209 int lazy, int ignore_signals, uint32_t mask);
210 extern int drm_fence_object_create(struct drm_device *dev, uint32_t type,
211 uint32_t fence_flags, uint32_t class,
212 drm_fence_object_t ** c_fence);
213 extern int drm_fence_add_user_object(drm_file_t * priv,
214 drm_fence_object_t * fence, int shareable);
215 extern int drm_fence_ioctl(DRM_IOCTL_ARGS);
217 /**************************************************
222 * The ttm backend GTT interface. (In our case AGP).
223 * Any similar type of device (PCIE?)
224 * needs only to implement these functions to be usable with the "TTM" interface.
225 * The AGP backend implementation lives in drm_agpsupport.c
226 * basically maps these calls to available functions in agpgart.
227 * Each drm device driver gets an
228 * additional function pointer that creates these types,
229 * so that the device can choose the correct aperture.
230 * (Multiple AGP apertures, etc.)
231 * Most device drivers will let this point to the standard AGP implementation.
234 #define DRM_BE_FLAG_NEEDS_FREE 0x00000001
235 #define DRM_BE_FLAG_BOUND_CACHED 0x00000002
237 typedef struct drm_ttm_backend {
240 uint32_t drm_map_type;
241 int (*needs_ub_cache_adjust) (struct drm_ttm_backend * backend);
242 int (*populate) (struct drm_ttm_backend * backend,
243 unsigned long num_pages, struct page ** pages);
244 void (*clear) (struct drm_ttm_backend * backend);
245 int (*bind) (struct drm_ttm_backend * backend,
246 unsigned long offset, int cached);
247 int (*unbind) (struct drm_ttm_backend * backend);
248 void (*destroy) (struct drm_ttm_backend * backend);
251 typedef struct drm_ttm {
254 unsigned long num_pages;
255 unsigned long aper_offset;
257 struct drm_device *dev;
259 uint32_t mapping_offset;
260 drm_ttm_backend_t *be;
270 extern drm_ttm_t *drm_ttm_init(struct drm_device *dev, unsigned long size);
271 extern int drm_bind_ttm(drm_ttm_t * ttm, int cached, unsigned long aper_offset);
272 extern void drm_ttm_unbind(drm_ttm_t * ttm);
273 extern void drm_ttm_evict(drm_ttm_t * ttm);
274 extern void drm_ttm_fixup_caching(drm_ttm_t * ttm);
275 extern struct page *drm_ttm_get_page(drm_ttm_t * ttm, int index);
278 * Destroy a ttm. The user normally calls drmRmMap or a similar IOCTL to do this,
279 * which calls this function iff there are no vmas referencing it anymore. Otherwise it is called
280 * when the last vma exits.
283 extern int drm_destroy_ttm(drm_ttm_t * ttm);
285 #define DRM_FLAG_MASKED(_old, _new, _mask) {\
286 (_old) ^= (((_old) ^ (_new)) & (_mask)); \
289 #define DRM_TTM_MASK_FLAGS ((1 << PAGE_SHIFT) - 1)
290 #define DRM_TTM_MASK_PFN (0xFFFFFFFFU - DRM_TTM_MASK_FLAGS)
296 #define DRM_TTM_PAGE_UNCACHED 0x01
297 #define DRM_TTM_PAGE_USED 0x02
298 #define DRM_TTM_PAGE_BOUND 0x04
299 #define DRM_TTM_PAGE_PRESENT 0x08
300 #define DRM_TTM_PAGE_VMALLOC 0x10
302 /***************************************************
303 * Buffer objects. (drm_bo.c, drm_bo_move.c)
306 typedef struct drm_bo_mem_reg {
307 drm_mm_node_t *mm_node;
309 unsigned long num_pages;
310 uint32_t page_alignment;
316 typedef struct drm_buffer_object {
317 struct drm_device *dev;
318 drm_user_object_t base;
321 * If there is a possibility that the usage variable is zero,
322 * then dev->struct_mutext should be locked before incrementing it.
326 unsigned long buffer_start;
328 unsigned long offset;
330 drm_bo_mem_reg_t mem;
332 struct list_head lru;
333 struct list_head ddestroy;
336 uint32_t fence_class;
337 drm_fence_object_t *fence;
339 wait_queue_head_t event_queue;
342 /* For pinned buffers */
343 drm_mm_node_t *pinned_node;
344 uint32_t pinned_mem_type;
345 struct list_head pinned_lru;
350 drm_map_list_t map_list;
351 uint32_t memory_type;
352 unsigned long bus_offset;
356 #ifdef DRM_ODD_MM_COMPAT
357 /* dev->struct_mutex only protected. */
358 struct list_head vma_list;
359 struct list_head p_mm_list;
362 } drm_buffer_object_t;
364 #define _DRM_BO_FLAG_UNFENCED 0x00000001
365 #define _DRM_BO_FLAG_EVICTED 0x00000002
367 typedef struct drm_mem_type_manager {
371 struct list_head lru;
372 struct list_head pinned;
374 uint32_t drm_bus_maptype;
375 unsigned long io_offset;
376 unsigned long io_size;
378 } drm_mem_type_manager_t;
380 #define _DRM_FLAG_MEMTYPE_FIXED 0x00000001 /* Fixed (on-card) PCI memory */
381 #define _DRM_FLAG_MEMTYPE_MAPPABLE 0x00000002 /* Memory mappable */
382 #define _DRM_FLAG_MEMTYPE_CACHED 0x00000004 /* Cached binding */
383 #define _DRM_FLAG_NEEDS_IOREMAP 0x00000008 /* Fixed memory needs ioremap
384 before kernel access. */
385 #define _DRM_FLAG_MEMTYPE_CMA 0x00000010 /* Can't map aperture */
386 #define _DRM_FLAG_MEMTYPE_CSELECT 0x00000020 /* Select caching */
388 typedef struct drm_buffer_manager {
389 struct mutex init_mutex;
390 struct mutex evict_mutex;
393 drm_file_t *last_to_validate;
394 drm_mem_type_manager_t man[DRM_BO_MEM_TYPES];
395 struct list_head unfenced;
396 struct list_head ddestroy;
397 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
398 struct work_struct wq;
400 struct delayed_work wq;
403 unsigned long cur_pages;
405 } drm_buffer_manager_t;
407 typedef struct drm_bo_driver {
408 const uint32_t *mem_type_prio;
409 const uint32_t *mem_busy_prio;
410 uint32_t num_mem_type_prio;
411 uint32_t num_mem_busy_prio;
412 drm_ttm_backend_t *(*create_ttm_backend_entry)
413 (struct drm_device * dev);
414 int (*fence_type) (struct drm_buffer_object *bo, uint32_t * class, uint32_t * type);
415 int (*invalidate_caches) (struct drm_device * dev, uint32_t flags);
416 int (*init_mem_type) (struct drm_device * dev, uint32_t type,
417 drm_mem_type_manager_t * man);
418 uint32_t(*evict_mask) (struct drm_buffer_object *bo);
419 int (*move) (struct drm_buffer_object * bo,
420 int evict, int no_wait, struct drm_bo_mem_reg * new_mem);
424 * buffer objects (drm_bo.c)
427 extern int drm_bo_ioctl(DRM_IOCTL_ARGS);
428 extern int drm_mm_init_ioctl(DRM_IOCTL_ARGS);
429 extern int drm_bo_driver_finish(struct drm_device *dev);
430 extern int drm_bo_driver_init(struct drm_device *dev);
431 extern int drm_bo_pci_offset(struct drm_device *dev,
432 drm_bo_mem_reg_t * mem,
433 unsigned long *bus_base,
434 unsigned long *bus_offset,
435 unsigned long *bus_size);
436 extern int drm_mem_reg_is_pci(struct drm_device *dev, drm_bo_mem_reg_t * mem);
438 extern void drm_bo_usage_deref_locked(drm_buffer_object_t * bo);
439 extern int drm_fence_buffer_objects(drm_file_t * priv,
440 struct list_head *list,
441 uint32_t fence_flags,
442 drm_fence_object_t * fence,
443 drm_fence_object_t ** used_fence);
444 extern void drm_bo_add_to_lru(drm_buffer_object_t * bo);
445 extern int drm_bo_wait(drm_buffer_object_t * bo, int lazy, int ignore_signals,
447 extern int drm_bo_mem_space(drm_buffer_object_t * bo,
448 drm_bo_mem_reg_t * mem, int no_wait);
449 extern int drm_bo_move_buffer(drm_buffer_object_t * bo, uint32_t new_mem_flags,
450 int no_wait, int move_unfenced);
453 * Buffer object memory move helpers.
457 extern int drm_bo_move_ttm(drm_buffer_object_t * bo,
458 int evict, int no_wait, drm_bo_mem_reg_t * new_mem);
459 extern int drm_bo_move_memcpy(drm_buffer_object_t * bo,
461 int no_wait, drm_bo_mem_reg_t * new_mem);
462 extern int drm_bo_move_accel_cleanup(drm_buffer_object_t * bo,
465 uint32_t fence_class,
467 uint32_t fence_flags,
468 drm_bo_mem_reg_t * new_mem);