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_OBJECTS_H
36 /***************************************************
37 * User space objects. (drm_object.c)
40 #define drm_user_object_entry(_ptr, _type, _member) container_of(_ptr, _type, _member)
42 enum drm_object_type {
47 * Add other user space object types here.
52 * A user object is a structure that helps the drm give out user handles
53 * to kernel internal objects and to keep track of these objects so that
54 * they can be destroyed, for example when the user space process exits.
55 * Designed to be accessible using a user space 32-bit handle.
58 struct drm_user_object {
59 struct drm_hash_item hash;
60 struct list_head list;
61 enum drm_object_type type;
64 struct drm_file *owner;
65 void (*ref_struct_locked) (struct drm_file * priv,
66 struct drm_user_object * obj,
67 enum drm_ref_type ref_action);
68 void (*unref) (struct drm_file * priv, struct drm_user_object * obj,
69 enum drm_ref_type unref_action);
70 void (*remove) (struct drm_file * priv, struct drm_user_object * obj);
74 * A ref object is a structure which is used to
75 * keep track of references to user objects and to keep track of these
76 * references so that they can be destroyed for example when the user space
77 * process exits. Designed to be accessible using a pointer to the _user_ object.
80 struct drm_ref_object {
81 struct drm_hash_item hash;
82 struct list_head list;
84 enum drm_ref_type unref_action;
88 * Must be called with the struct_mutex held.
91 extern int drm_add_user_object(struct drm_file * priv, struct drm_user_object * item,
94 * Must be called with the struct_mutex held.
97 extern struct drm_user_object *drm_lookup_user_object(struct drm_file * priv,
101 * Must be called with the struct_mutex held.
102 * If "item" has been obtained by a call to drm_lookup_user_object. You may not
103 * release the struct_mutex before calling drm_remove_ref_object.
104 * This function may temporarily release the struct_mutex.
107 extern int drm_remove_user_object(struct drm_file * priv, struct drm_user_object * item);
110 * Must be called with the struct_mutex held. May temporarily release it.
113 extern int drm_add_ref_object(struct drm_file * priv,
114 struct drm_user_object * referenced_object,
115 enum drm_ref_type ref_action);
118 * Must be called with the struct_mutex held.
121 struct drm_ref_object *drm_lookup_ref_object(struct drm_file * priv,
122 struct drm_user_object * referenced_object,
123 enum drm_ref_type ref_action);
125 * Must be called with the struct_mutex held.
126 * If "item" has been obtained by a call to drm_lookup_ref_object. You may not
127 * release the struct_mutex before calling drm_remove_ref_object.
128 * This function may temporarily release the struct_mutex.
131 extern void drm_remove_ref_object(struct drm_file * priv, struct drm_ref_object * item);
132 extern int drm_user_object_ref(struct drm_file * priv, uint32_t user_token,
133 enum drm_object_type type,
134 struct drm_user_object ** object);
135 extern int drm_user_object_unref(struct drm_file * priv, uint32_t user_token,
136 enum drm_object_type type);
138 /***************************************************
139 * Fence objects. (drm_fence.c)
142 struct drm_fence_object {
143 struct drm_user_object base;
144 struct drm_device *dev;
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;
161 #define _DRM_FENCE_CLASSES 8
162 #define _DRM_FENCE_TYPE_EXE 0x00
164 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;
173 struct drm_fence_manager {
176 struct drm_fence_class_manager class[_DRM_FENCE_CLASSES];
177 uint32_t num_classes;
181 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);
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_fence_object * fence, uint32_t type);
201 extern int drm_fence_object_signaled(struct drm_fence_object * fence,
202 uint32_t type, int flush);
203 extern void drm_fence_usage_deref_locked(struct drm_fence_object ** fence);
204 extern void drm_fence_usage_deref_unlocked(struct drm_fence_object ** fence);
205 extern struct drm_fence_object *drm_fence_reference_locked(struct drm_fence_object *src);
206 extern void drm_fence_reference_unlocked(struct drm_fence_object **dst,
207 struct drm_fence_object *src);
208 extern int drm_fence_object_wait(struct drm_fence_object * 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 struct drm_fence_object ** c_fence);
213 extern int drm_fence_add_user_object(struct drm_file * priv,
214 struct drm_fence_object * fence, int shareable);
216 extern int drm_fence_create_ioctl(struct drm_device *dev, void *data,
217 struct drm_file *file_priv);
218 extern int drm_fence_destroy_ioctl(struct drm_device *dev, void *data,
219 struct drm_file *file_priv);
220 extern int drm_fence_reference_ioctl(struct drm_device *dev, void *data,
221 struct drm_file *file_priv);
222 extern int drm_fence_unreference_ioctl(struct drm_device *dev, void *data,
223 struct drm_file *file_priv);
224 extern int drm_fence_signaled_ioctl(struct drm_device *dev, void *data,
225 struct drm_file *file_priv);
226 extern int drm_fence_flush_ioctl(struct drm_device *dev, void *data,
227 struct drm_file *file_priv);
228 extern int drm_fence_wait_ioctl(struct drm_device *dev, void *data,
229 struct drm_file *file_priv);
230 extern int drm_fence_emit_ioctl(struct drm_device *dev, void *data,
231 struct drm_file *file_priv);
232 extern int drm_fence_buffers_ioctl(struct drm_device *dev, void *data,
233 struct drm_file *file_priv);
234 /**************************************************
239 * The ttm backend GTT interface. (In our case AGP).
240 * Any similar type of device (PCIE?)
241 * needs only to implement these functions to be usable with the "TTM" interface.
242 * The AGP backend implementation lives in drm_agpsupport.c
243 * basically maps these calls to available functions in agpgart.
244 * Each drm device driver gets an
245 * additional function pointer that creates these types,
246 * so that the device can choose the correct aperture.
247 * (Multiple AGP apertures, etc.)
248 * Most device drivers will let this point to the standard AGP implementation.
251 #define DRM_BE_FLAG_NEEDS_FREE 0x00000001
252 #define DRM_BE_FLAG_BOUND_CACHED 0x00000002
254 struct drm_ttm_backend;
255 struct drm_ttm_backend_func {
256 int (*needs_ub_cache_adjust) (struct drm_ttm_backend * backend);
257 int (*populate) (struct drm_ttm_backend * backend,
258 unsigned long num_pages, struct page ** pages);
259 void (*clear) (struct drm_ttm_backend * backend);
260 int (*bind) (struct drm_ttm_backend * backend,
261 unsigned long offset, int cached);
262 int (*unbind) (struct drm_ttm_backend * backend);
263 void (*destroy) (struct drm_ttm_backend * backend);
267 struct drm_ttm_backend {
270 struct drm_ttm_backend_func *func;
276 unsigned long num_pages;
277 unsigned long aper_offset;
279 struct drm_device *dev;
281 uint32_t mapping_offset;
282 struct drm_ttm_backend *be;
292 extern struct drm_ttm *drm_ttm_init(struct drm_device *dev, unsigned long size);
293 extern int drm_bind_ttm(struct drm_ttm * ttm, int cached, unsigned long aper_offset);
294 extern void drm_ttm_unbind(struct drm_ttm * ttm);
295 extern void drm_ttm_evict(struct drm_ttm * ttm);
296 extern void drm_ttm_fixup_caching(struct drm_ttm * ttm);
297 extern struct page *drm_ttm_get_page(struct drm_ttm * ttm, int index);
300 * Destroy a ttm. The user normally calls drmRmMap or a similar IOCTL to do this,
301 * which calls this function iff there are no vmas referencing it anymore. Otherwise it is called
302 * when the last vma exits.
305 extern int drm_destroy_ttm(struct drm_ttm * ttm);
307 #define DRM_FLAG_MASKED(_old, _new, _mask) {\
308 (_old) ^= (((_old) ^ (_new)) & (_mask)); \
311 #define DRM_TTM_MASK_FLAGS ((1 << PAGE_SHIFT) - 1)
312 #define DRM_TTM_MASK_PFN (0xFFFFFFFFU - DRM_TTM_MASK_FLAGS)
318 #define DRM_TTM_PAGE_UNCACHED 0x01
319 #define DRM_TTM_PAGE_USED 0x02
320 #define DRM_TTM_PAGE_BOUND 0x04
321 #define DRM_TTM_PAGE_PRESENT 0x08
322 #define DRM_TTM_PAGE_VMALLOC 0x10
324 /***************************************************
325 * Buffer objects. (drm_bo.c, drm_bo_move.c)
328 struct drm_bo_mem_reg {
329 struct drm_mm_node *mm_node;
331 unsigned long num_pages;
332 uint32_t page_alignment;
338 struct drm_buffer_object {
339 struct drm_device *dev;
340 struct drm_user_object base;
343 * If there is a possibility that the usage variable is zero,
344 * then dev->struct_mutext should be locked before incrementing it.
348 unsigned long buffer_start;
349 enum drm_bo_type type;
350 unsigned long offset;
352 struct drm_bo_mem_reg mem;
354 struct list_head lru;
355 struct list_head ddestroy;
358 uint32_t fence_class;
359 struct drm_fence_object *fence;
361 wait_queue_head_t event_queue;
364 /* For pinned buffers */
365 struct drm_mm_node *pinned_node;
366 uint32_t pinned_mem_type;
367 struct list_head pinned_lru;
372 struct drm_map_list map_list;
373 uint32_t memory_type;
374 unsigned long bus_offset;
378 #ifdef DRM_ODD_MM_COMPAT
379 /* dev->struct_mutex only protected. */
380 struct list_head vma_list;
381 struct list_head p_mm_list;
386 #define _DRM_BO_FLAG_UNFENCED 0x00000001
387 #define _DRM_BO_FLAG_EVICTED 0x00000002
389 struct drm_mem_type_manager {
392 struct drm_mm manager;
393 struct list_head lru;
394 struct list_head pinned;
396 uint32_t drm_bus_maptype;
397 unsigned long io_offset;
398 unsigned long io_size;
402 #define _DRM_FLAG_MEMTYPE_FIXED 0x00000001 /* Fixed (on-card) PCI memory */
403 #define _DRM_FLAG_MEMTYPE_MAPPABLE 0x00000002 /* Memory mappable */
404 #define _DRM_FLAG_MEMTYPE_CACHED 0x00000004 /* Cached binding */
405 #define _DRM_FLAG_NEEDS_IOREMAP 0x00000008 /* Fixed memory needs ioremap
406 before kernel access. */
407 #define _DRM_FLAG_MEMTYPE_CMA 0x00000010 /* Can't map aperture */
408 #define _DRM_FLAG_MEMTYPE_CSELECT 0x00000020 /* Select caching */
410 struct drm_buffer_manager {
411 struct mutex init_mutex;
412 struct mutex evict_mutex;
415 struct drm_file *last_to_validate;
416 struct drm_mem_type_manager man[DRM_BO_MEM_TYPES];
417 struct list_head unfenced;
418 struct list_head ddestroy;
419 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
420 struct work_struct wq;
422 struct delayed_work wq;
425 unsigned long cur_pages;
429 struct drm_bo_driver {
430 const uint32_t *mem_type_prio;
431 const uint32_t *mem_busy_prio;
432 uint32_t num_mem_type_prio;
433 uint32_t num_mem_busy_prio;
434 struct drm_ttm_backend *(*create_ttm_backend_entry)
435 (struct drm_device * dev);
436 int (*fence_type) (struct drm_buffer_object *bo, uint32_t * type);
437 int (*invalidate_caches) (struct drm_device * dev, uint64_t flags);
438 int (*init_mem_type) (struct drm_device * dev, uint32_t type,
439 struct drm_mem_type_manager * man);
440 uint32_t(*evict_mask) (struct drm_buffer_object *bo);
441 int (*move) (struct drm_buffer_object * bo,
442 int evict, int no_wait, struct drm_bo_mem_reg * new_mem);
446 * buffer objects (drm_bo.c)
449 extern int drm_bo_create_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
450 extern int drm_bo_destroy_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
451 extern int drm_bo_map_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
452 extern int drm_bo_unmap_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
453 extern int drm_bo_reference_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
454 extern int drm_bo_unreference_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
455 extern int drm_bo_wait_idle_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
456 extern int drm_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
457 extern int drm_bo_op_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
460 extern int drm_mm_init_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
461 extern int drm_mm_takedown_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
462 extern int drm_mm_lock_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
463 extern int drm_mm_unlock_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
464 extern int drm_bo_driver_finish(struct drm_device *dev);
465 extern int drm_bo_driver_init(struct drm_device *dev);
466 extern int drm_bo_pci_offset(struct drm_device *dev,
467 struct drm_bo_mem_reg * mem,
468 unsigned long *bus_base,
469 unsigned long *bus_offset,
470 unsigned long *bus_size);
471 extern int drm_mem_reg_is_pci(struct drm_device *dev, struct drm_bo_mem_reg * mem);
473 extern void drm_bo_usage_deref_locked(struct drm_buffer_object ** bo);
474 extern int drm_fence_buffer_objects(struct drm_file * priv,
475 struct list_head *list,
476 uint32_t fence_flags,
477 struct drm_fence_object * fence,
478 struct drm_fence_object ** used_fence);
479 extern void drm_bo_add_to_lru(struct drm_buffer_object * bo);
480 extern int drm_bo_wait(struct drm_buffer_object * bo, int lazy, int ignore_signals,
482 extern int drm_bo_mem_space(struct drm_buffer_object * bo,
483 struct drm_bo_mem_reg * mem, int no_wait);
484 extern int drm_bo_move_buffer(struct drm_buffer_object * bo, uint32_t new_mem_flags,
485 int no_wait, int move_unfenced);
488 * Buffer object memory move helpers.
492 extern int drm_bo_move_ttm(struct drm_buffer_object * bo,
493 int evict, int no_wait, struct drm_bo_mem_reg * new_mem);
494 extern int drm_bo_move_memcpy(struct drm_buffer_object * bo,
496 int no_wait, struct drm_bo_mem_reg * new_mem);
497 extern int drm_bo_move_accel_cleanup(struct drm_buffer_object * bo,
500 uint32_t fence_class,
502 uint32_t fence_flags,
503 struct drm_bo_mem_reg * new_mem);
505 #ifdef CONFIG_DEBUG_MUTEXES
506 #define DRM_ASSERT_LOCKED(_mutex) \
507 BUG_ON(!mutex_is_locked(_mutex) || \
508 ((_mutex)->owner != current_thread_info()))
510 #define DRM_ASSERT_LOCKED(_mutex)