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
35 struct drm_bo_mem_reg;
37 /***************************************************
38 * User space objects. (drm_object.c)
41 #define drm_user_object_entry(_ptr, _type, _member) container_of(_ptr, _type, _member)
43 enum drm_object_type {
47 * Add other user space object types here.
49 drm_driver_type0 = 256,
57 * A user object is a structure that helps the drm give out user handles
58 * to kernel internal objects and to keep track of these objects so that
59 * they can be destroyed, for example when the user space process exits.
60 * Designed to be accessible using a user space 32-bit handle.
63 struct drm_user_object {
64 struct drm_hash_item hash;
65 struct list_head list;
66 enum drm_object_type type;
69 struct drm_file *owner;
70 void (*ref_struct_locked) (struct drm_file * priv,
71 struct drm_user_object * obj,
72 enum drm_ref_type ref_action);
73 void (*unref) (struct drm_file * priv, struct drm_user_object * obj,
74 enum drm_ref_type unref_action);
75 void (*remove) (struct drm_file * priv, struct drm_user_object * obj);
79 * A ref object is a structure which is used to
80 * keep track of references to user objects and to keep track of these
81 * references so that they can be destroyed for example when the user space
82 * process exits. Designed to be accessible using a pointer to the _user_ object.
85 struct drm_ref_object {
86 struct drm_hash_item hash;
87 struct list_head list;
89 enum drm_ref_type unref_action;
93 * Must be called with the struct_mutex held.
96 extern int drm_add_user_object(struct drm_file * priv, struct drm_user_object * item,
99 * Must be called with the struct_mutex held.
102 extern struct drm_user_object *drm_lookup_user_object(struct drm_file * priv,
106 * Must be called with the struct_mutex held. May temporarily release it.
109 extern int drm_add_ref_object(struct drm_file * priv,
110 struct drm_user_object * referenced_object,
111 enum drm_ref_type ref_action);
114 * Must be called with the struct_mutex held.
117 struct drm_ref_object *drm_lookup_ref_object(struct drm_file * priv,
118 struct drm_user_object * referenced_object,
119 enum drm_ref_type ref_action);
121 * Must be called with the struct_mutex held.
122 * If "item" has been obtained by a call to drm_lookup_ref_object. You may not
123 * release the struct_mutex before calling drm_remove_ref_object.
124 * This function may temporarily release the struct_mutex.
127 extern void drm_remove_ref_object(struct drm_file * priv, struct drm_ref_object * item);
128 extern int drm_user_object_ref(struct drm_file * priv, uint32_t user_token,
129 enum drm_object_type type,
130 struct drm_user_object ** object);
131 extern int drm_user_object_unref(struct drm_file * priv, uint32_t user_token,
132 enum drm_object_type type);
134 /***************************************************
135 * Fence objects. (drm_fence.c)
138 struct drm_fence_object {
139 struct drm_user_object base;
140 struct drm_device *dev;
144 * The below three fields are protected by the fence manager spinlock.
147 struct list_head ring;
149 uint32_t native_type;
154 uint32_t submitted_flush;
158 #define _DRM_FENCE_CLASSES 8
159 #define _DRM_FENCE_TYPE_EXE 0x00
161 struct drm_fence_class_manager {
162 struct list_head ring;
163 uint32_t pending_flush;
164 wait_queue_head_t fence_queue;
165 int pending_exe_flush;
166 uint32_t last_exe_flush;
167 uint32_t exe_flush_sequence;
170 struct drm_fence_manager {
173 struct drm_fence_class_manager fence_class[_DRM_FENCE_CLASSES];
174 uint32_t num_classes;
178 struct drm_fence_driver {
179 uint32_t num_classes;
182 uint32_t sequence_mask;
184 int (*has_irq) (struct drm_device * dev, uint32_t fence_class,
186 int (*emit) (struct drm_device * dev, uint32_t fence_class, uint32_t flags,
187 uint32_t * breadcrumb, uint32_t * native_type);
188 void (*poke_flush) (struct drm_device * dev, uint32_t fence_class);
191 extern void drm_fence_handler(struct drm_device *dev, uint32_t fence_class,
192 uint32_t sequence, uint32_t type, uint32_t error);
193 extern void drm_fence_manager_init(struct drm_device *dev);
194 extern void drm_fence_manager_takedown(struct drm_device *dev);
195 extern void drm_fence_flush_old(struct drm_device *dev, uint32_t fence_class,
197 extern int drm_fence_object_flush(struct drm_fence_object * fence, uint32_t type);
198 extern int drm_fence_object_signaled(struct drm_fence_object * fence,
199 uint32_t type, int flush);
200 extern void drm_fence_usage_deref_locked(struct drm_fence_object ** fence);
201 extern void drm_fence_usage_deref_unlocked(struct drm_fence_object ** fence);
202 extern struct drm_fence_object *drm_fence_reference_locked(struct drm_fence_object *src);
203 extern void drm_fence_reference_unlocked(struct drm_fence_object **dst,
204 struct drm_fence_object *src);
205 extern int drm_fence_object_wait(struct drm_fence_object * fence,
206 int lazy, int ignore_signals, uint32_t mask);
207 extern int drm_fence_object_create(struct drm_device *dev, uint32_t type,
208 uint32_t fence_flags, uint32_t fence_class,
209 struct drm_fence_object ** c_fence);
210 extern int drm_fence_object_emit(struct drm_fence_object * fence,
211 uint32_t fence_flags, uint32_t class,
213 extern void drm_fence_fill_arg(struct drm_fence_object *fence,
214 struct drm_fence_arg *arg);
216 extern int drm_fence_add_user_object(struct drm_file * priv,
217 struct drm_fence_object * fence, int shareable);
219 extern int drm_fence_create_ioctl(struct drm_device *dev, void *data,
220 struct drm_file *file_priv);
221 extern int drm_fence_destroy_ioctl(struct drm_device *dev, void *data,
222 struct drm_file *file_priv);
223 extern int drm_fence_reference_ioctl(struct drm_device *dev, void *data,
224 struct drm_file *file_priv);
225 extern int drm_fence_unreference_ioctl(struct drm_device *dev, void *data,
226 struct drm_file *file_priv);
227 extern int drm_fence_signaled_ioctl(struct drm_device *dev, void *data,
228 struct drm_file *file_priv);
229 extern int drm_fence_flush_ioctl(struct drm_device *dev, void *data,
230 struct drm_file *file_priv);
231 extern int drm_fence_wait_ioctl(struct drm_device *dev, void *data,
232 struct drm_file *file_priv);
233 extern int drm_fence_emit_ioctl(struct drm_device *dev, void *data,
234 struct drm_file *file_priv);
235 extern int drm_fence_buffers_ioctl(struct drm_device *dev, void *data,
236 struct drm_file *file_priv);
237 /**************************************************
242 * The ttm backend GTT interface. (In our case AGP).
243 * Any similar type of device (PCIE?)
244 * needs only to implement these functions to be usable with the "TTM" interface.
245 * The AGP backend implementation lives in drm_agpsupport.c
246 * basically maps these calls to available functions in agpgart.
247 * Each drm device driver gets an
248 * additional function pointer that creates these types,
249 * so that the device can choose the correct aperture.
250 * (Multiple AGP apertures, etc.)
251 * Most device drivers will let this point to the standard AGP implementation.
254 #define DRM_BE_FLAG_NEEDS_FREE 0x00000001
255 #define DRM_BE_FLAG_BOUND_CACHED 0x00000002
257 struct drm_ttm_backend;
258 struct drm_ttm_backend_func {
259 int (*needs_ub_cache_adjust) (struct drm_ttm_backend * backend);
260 int (*populate) (struct drm_ttm_backend * backend,
261 unsigned long num_pages, struct page ** pages);
262 void (*clear) (struct drm_ttm_backend * backend);
263 int (*bind) (struct drm_ttm_backend * backend,
264 struct drm_bo_mem_reg * bo_mem);
265 int (*unbind) (struct drm_ttm_backend * backend);
266 void (*destroy) (struct drm_ttm_backend * backend);
270 typedef struct drm_ttm_backend {
271 struct drm_device *dev;
273 struct drm_ttm_backend_func *func;
279 unsigned long num_pages;
281 struct drm_device *dev;
283 uint32_t mapping_offset;
284 struct drm_ttm_backend *be;
294 extern struct drm_ttm *drm_ttm_init(struct drm_device *dev, unsigned long size);
295 extern int drm_bind_ttm(struct drm_ttm * ttm, struct drm_bo_mem_reg *bo_mem);
296 extern void drm_ttm_unbind(struct drm_ttm * ttm);
297 extern void drm_ttm_evict(struct drm_ttm * ttm);
298 extern void drm_ttm_fixup_caching(struct drm_ttm * ttm);
299 extern struct page *drm_ttm_get_page(struct drm_ttm * ttm, int index);
300 extern void drm_ttm_cache_flush(void);
301 extern int drm_ttm_populate(struct drm_ttm * ttm);
304 * Destroy a ttm. The user normally calls drmRmMap or a similar IOCTL to do this,
305 * which calls this function iff there are no vmas referencing it anymore. Otherwise it is called
306 * when the last vma exits.
309 extern int drm_destroy_ttm(struct drm_ttm * ttm);
311 #define DRM_FLAG_MASKED(_old, _new, _mask) {\
312 (_old) ^= (((_old) ^ (_new)) & (_mask)); \
315 #define DRM_TTM_MASK_FLAGS ((1 << PAGE_SHIFT) - 1)
316 #define DRM_TTM_MASK_PFN (0xFFFFFFFFU - DRM_TTM_MASK_FLAGS)
322 #define DRM_TTM_PAGE_UNCACHED 0x01
323 #define DRM_TTM_PAGE_USED 0x02
324 #define DRM_TTM_PAGE_BOUND 0x04
325 #define DRM_TTM_PAGE_PRESENT 0x08
326 #define DRM_TTM_PAGE_VMALLOC 0x10
328 /***************************************************
329 * Buffer objects. (drm_bo.c, drm_bo_move.c)
332 struct drm_bo_mem_reg {
333 struct drm_mm_node *mm_node;
335 unsigned long num_pages;
336 uint32_t page_alignment;
340 uint32_t desired_tile_stride;
341 uint32_t hw_tile_stride;
347 drm_bo_type_kernel, /* for initial kernel allocations */
350 struct drm_buffer_object {
351 struct drm_device *dev;
352 struct drm_user_object base;
355 * If there is a possibility that the usage variable is zero,
356 * then dev->struct_mutext should be locked before incrementing it.
360 unsigned long buffer_start;
361 enum drm_bo_type type;
362 unsigned long offset;
364 struct drm_bo_mem_reg mem;
366 struct list_head lru;
367 struct list_head ddestroy;
370 uint32_t fence_class;
371 uint32_t new_fence_type;
372 uint32_t new_fence_class;
373 struct drm_fence_object *fence;
375 wait_queue_head_t event_queue;
377 unsigned long num_pages;
379 /* For pinned buffers */
380 struct drm_mm_node *pinned_node;
381 uint32_t pinned_mem_type;
382 struct list_head pinned_lru;
386 struct drm_map_list map_list;
387 uint32_t memory_type;
388 unsigned long bus_offset;
392 #ifdef DRM_ODD_MM_COMPAT
393 /* dev->struct_mutex only protected. */
394 struct list_head vma_list;
395 struct list_head p_mm_list;
400 #define _DRM_BO_FLAG_UNFENCED 0x00000001
401 #define _DRM_BO_FLAG_EVICTED 0x00000002
403 struct drm_mem_type_manager {
406 struct drm_mm manager;
407 struct list_head lru;
408 struct list_head pinned;
410 uint32_t drm_bus_maptype;
411 unsigned long gpu_offset;
412 unsigned long io_offset;
413 unsigned long io_size;
417 #define _DRM_FLAG_MEMTYPE_FIXED 0x00000001 /* Fixed (on-card) PCI memory */
418 #define _DRM_FLAG_MEMTYPE_MAPPABLE 0x00000002 /* Memory mappable */
419 #define _DRM_FLAG_MEMTYPE_CACHED 0x00000004 /* Cached binding */
420 #define _DRM_FLAG_NEEDS_IOREMAP 0x00000008 /* Fixed memory needs ioremap
421 before kernel access. */
422 #define _DRM_FLAG_MEMTYPE_CMA 0x00000010 /* Can't map aperture */
423 #define _DRM_FLAG_MEMTYPE_CSELECT 0x00000020 /* Select caching */
425 struct drm_buffer_manager {
426 struct mutex init_mutex;
427 struct mutex evict_mutex;
430 struct drm_file *last_to_validate;
431 struct drm_mem_type_manager man[DRM_BO_MEM_TYPES];
432 struct list_head unfenced;
433 struct list_head ddestroy;
434 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
435 struct work_struct wq;
437 struct delayed_work wq;
440 unsigned long cur_pages;
444 struct drm_bo_driver {
445 const uint32_t *mem_type_prio;
446 const uint32_t *mem_busy_prio;
447 uint32_t num_mem_type_prio;
448 uint32_t num_mem_busy_prio;
449 struct drm_ttm_backend *(*create_ttm_backend_entry)
450 (struct drm_device * dev);
451 int (*fence_type) (struct drm_buffer_object *bo, uint32_t *fclass,
453 int (*invalidate_caches) (struct drm_device * dev, uint64_t flags);
454 int (*init_mem_type) (struct drm_device * dev, uint32_t type,
455 struct drm_mem_type_manager * man);
456 uint32_t(*evict_mask) (struct drm_buffer_object *bo);
457 int (*move) (struct drm_buffer_object * bo,
458 int evict, int no_wait, struct drm_bo_mem_reg * new_mem);
462 * buffer objects (drm_bo.c)
465 extern int drm_bo_create_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
466 extern int drm_bo_destroy_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
467 extern int drm_bo_map_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
468 extern int drm_bo_unmap_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
469 extern int drm_bo_reference_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
470 extern int drm_bo_unreference_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
471 extern int drm_bo_wait_idle_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
472 extern int drm_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
473 extern int drm_bo_setstatus_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
474 extern int drm_mm_init_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
475 extern int drm_mm_takedown_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
476 extern int drm_mm_lock_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
477 extern int drm_mm_unlock_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
478 extern int drm_bo_driver_finish(struct drm_device *dev);
479 extern int drm_bo_driver_init(struct drm_device *dev);
480 extern int drm_bo_pci_offset(struct drm_device *dev,
481 struct drm_bo_mem_reg * mem,
482 unsigned long *bus_base,
483 unsigned long *bus_offset,
484 unsigned long *bus_size);
485 extern int drm_mem_reg_is_pci(struct drm_device *dev, struct drm_bo_mem_reg * mem);
487 extern void drm_bo_usage_deref_locked(struct drm_buffer_object ** bo);
488 extern void drm_bo_usage_deref_unlocked(struct drm_buffer_object ** bo);
489 extern void drm_putback_buffer_objects(struct drm_device *dev);
490 extern int drm_fence_buffer_objects(struct drm_device * dev,
491 struct list_head *list,
492 uint32_t fence_flags,
493 struct drm_fence_object * fence,
494 struct drm_fence_object ** used_fence);
495 extern void drm_bo_add_to_lru(struct drm_buffer_object * bo);
496 extern int drm_buffer_object_create(struct drm_device *dev, unsigned long size,
497 enum drm_bo_type type, uint64_t mask,
498 uint32_t hint, uint32_t page_alignment,
499 unsigned long buffer_start,
500 struct drm_buffer_object **bo);
501 extern int drm_bo_wait(struct drm_buffer_object * bo, int lazy, int ignore_signals,
503 extern int drm_bo_mem_space(struct drm_buffer_object * bo,
504 struct drm_bo_mem_reg * mem, int no_wait);
505 extern int drm_bo_move_buffer(struct drm_buffer_object * bo, uint32_t new_mem_flags,
506 int no_wait, int move_unfenced);
507 extern int drm_bo_clean_mm(struct drm_device * dev, unsigned mem_type);
508 extern int drm_bo_init_mm(struct drm_device * dev, unsigned type,
509 unsigned long p_offset, unsigned long p_size);
510 extern int drm_bo_handle_validate(struct drm_file * file_priv, uint32_t handle,
511 uint32_t fence_class, uint64_t flags,
512 uint64_t mask, uint32_t hint,
513 int use_old_fence_class,
514 struct drm_bo_info_rep * rep,
515 struct drm_buffer_object **bo_rep);
516 extern struct drm_buffer_object *drm_lookup_buffer_object(struct drm_file * file_priv,
519 extern int drm_bo_do_validate(struct drm_buffer_object *bo,
520 uint64_t flags, uint64_t mask, uint32_t hint,
521 uint32_t fence_class,
523 struct drm_bo_info_rep *rep);
526 * Buffer object memory move- and map helpers.
530 extern int drm_bo_move_ttm(struct drm_buffer_object * bo,
531 int evict, int no_wait, struct drm_bo_mem_reg * new_mem);
532 extern int drm_bo_move_memcpy(struct drm_buffer_object * bo,
534 int no_wait, struct drm_bo_mem_reg * new_mem);
535 extern int drm_bo_move_accel_cleanup(struct drm_buffer_object * bo,
538 uint32_t fence_class,
540 uint32_t fence_flags,
541 struct drm_bo_mem_reg * new_mem);
542 extern int drm_bo_same_page(unsigned long offset, unsigned long offset2);
543 extern unsigned long drm_bo_offset_end(unsigned long offset,
546 struct drm_bo_kmap_obj {
557 static inline void *drm_bmo_virtual(struct drm_bo_kmap_obj *map, int *is_iomem)
559 *is_iomem = (map->bo_kmap_type == bo_map_iomap ||
560 map->bo_kmap_type == bo_map_premapped);
563 extern void drm_bo_kunmap(struct drm_bo_kmap_obj *map);
564 extern int drm_bo_kmap(struct drm_buffer_object *bo, unsigned long start_page,
565 unsigned long num_pages, struct drm_bo_kmap_obj *map);
573 struct list_head head;
574 struct drm_fence_object *fence;
576 uint32_t new_fence_type;
579 struct drm_reg_manager {
580 struct list_head free;
581 struct list_head lru;
582 struct list_head unfenced;
584 int (*reg_reusable)(const struct drm_reg *reg, const void *data);
585 void (*reg_destroy)(struct drm_reg *reg);
588 extern int drm_regs_alloc(struct drm_reg_manager *manager,
590 uint32_t fence_class,
594 struct drm_reg **reg);
596 extern void drm_regs_fence(struct drm_reg_manager *regs,
597 struct drm_fence_object *fence);
599 extern void drm_regs_free(struct drm_reg_manager *manager);
600 extern void drm_regs_add(struct drm_reg_manager *manager, struct drm_reg *reg);
601 extern void drm_regs_init(struct drm_reg_manager *manager,
602 int (*reg_reusable)(const struct drm_reg *,
604 void (*reg_destroy)(struct drm_reg *));
606 #ifdef CONFIG_DEBUG_MUTEXES
607 #define DRM_ASSERT_LOCKED(_mutex) \
608 BUG_ON(!mutex_is_locked(_mutex) || \
609 ((_mutex)->owner != current_thread_info()))
611 #define DRM_ASSERT_LOCKED(_mutex)