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 struct drm_ttm_backend;
238 typedef struct drm_ttm_backend_func {
239 int (*needs_ub_cache_adjust) (struct drm_ttm_backend * backend);
240 int (*populate) (struct drm_ttm_backend * backend,
241 unsigned long num_pages, struct page ** pages);
242 void (*clear) (struct drm_ttm_backend * backend);
243 int (*bind) (struct drm_ttm_backend * backend,
244 unsigned long offset, int cached);
245 int (*unbind) (struct drm_ttm_backend * backend);
246 void (*destroy) (struct drm_ttm_backend * backend);
247 } drm_ttm_backend_func_t;
250 typedef struct drm_ttm_backend {
253 drm_ttm_backend_func_t *func;
256 typedef struct drm_ttm {
259 unsigned long num_pages;
260 unsigned long aper_offset;
262 struct drm_device *dev;
264 uint32_t mapping_offset;
265 drm_ttm_backend_t *be;
275 extern drm_ttm_t *drm_ttm_init(struct drm_device *dev, unsigned long size);
276 extern int drm_bind_ttm(drm_ttm_t * ttm, int cached, unsigned long aper_offset);
277 extern void drm_ttm_unbind(drm_ttm_t * ttm);
278 extern void drm_ttm_evict(drm_ttm_t * ttm);
279 extern void drm_ttm_fixup_caching(drm_ttm_t * ttm);
280 extern struct page *drm_ttm_get_page(drm_ttm_t * ttm, int index);
283 * Destroy a ttm. The user normally calls drmRmMap or a similar IOCTL to do this,
284 * which calls this function iff there are no vmas referencing it anymore. Otherwise it is called
285 * when the last vma exits.
288 extern int drm_destroy_ttm(drm_ttm_t * ttm);
290 #define DRM_FLAG_MASKED(_old, _new, _mask) {\
291 (_old) ^= (((_old) ^ (_new)) & (_mask)); \
294 #define DRM_TTM_MASK_FLAGS ((1 << PAGE_SHIFT) - 1)
295 #define DRM_TTM_MASK_PFN (0xFFFFFFFFU - DRM_TTM_MASK_FLAGS)
301 #define DRM_TTM_PAGE_UNCACHED 0x01
302 #define DRM_TTM_PAGE_USED 0x02
303 #define DRM_TTM_PAGE_BOUND 0x04
304 #define DRM_TTM_PAGE_PRESENT 0x08
305 #define DRM_TTM_PAGE_VMALLOC 0x10
307 /***************************************************
308 * Buffer objects. (drm_bo.c, drm_bo_move.c)
311 typedef struct drm_bo_mem_reg {
312 drm_mm_node_t *mm_node;
314 unsigned long num_pages;
315 uint32_t page_alignment;
321 typedef struct drm_buffer_object {
322 struct drm_device *dev;
323 drm_user_object_t base;
326 * If there is a possibility that the usage variable is zero,
327 * then dev->struct_mutext should be locked before incrementing it.
331 unsigned long buffer_start;
333 unsigned long offset;
335 drm_bo_mem_reg_t mem;
337 struct list_head lru;
338 struct list_head ddestroy;
341 uint32_t fence_class;
342 drm_fence_object_t *fence;
344 wait_queue_head_t event_queue;
347 /* For pinned buffers */
348 drm_mm_node_t *pinned_node;
349 uint32_t pinned_mem_type;
350 struct list_head pinned_lru;
355 drm_map_list_t map_list;
356 uint32_t memory_type;
357 unsigned long bus_offset;
361 #ifdef DRM_ODD_MM_COMPAT
362 /* dev->struct_mutex only protected. */
363 struct list_head vma_list;
364 struct list_head p_mm_list;
367 } drm_buffer_object_t;
369 #define _DRM_BO_FLAG_UNFENCED 0x00000001
370 #define _DRM_BO_FLAG_EVICTED 0x00000002
372 typedef struct drm_mem_type_manager {
376 struct list_head lru;
377 struct list_head pinned;
379 uint32_t drm_bus_maptype;
380 unsigned long io_offset;
381 unsigned long io_size;
383 } drm_mem_type_manager_t;
385 #define _DRM_FLAG_MEMTYPE_FIXED 0x00000001 /* Fixed (on-card) PCI memory */
386 #define _DRM_FLAG_MEMTYPE_MAPPABLE 0x00000002 /* Memory mappable */
387 #define _DRM_FLAG_MEMTYPE_CACHED 0x00000004 /* Cached binding */
388 #define _DRM_FLAG_NEEDS_IOREMAP 0x00000008 /* Fixed memory needs ioremap
389 before kernel access. */
390 #define _DRM_FLAG_MEMTYPE_CMA 0x00000010 /* Can't map aperture */
391 #define _DRM_FLAG_MEMTYPE_CSELECT 0x00000020 /* Select caching */
393 typedef struct drm_buffer_manager {
394 struct mutex init_mutex;
395 struct mutex evict_mutex;
398 drm_file_t *last_to_validate;
399 drm_mem_type_manager_t man[DRM_BO_MEM_TYPES];
400 struct list_head unfenced;
401 struct list_head ddestroy;
402 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
403 struct work_struct wq;
405 struct delayed_work wq;
408 unsigned long cur_pages;
410 } drm_buffer_manager_t;
412 typedef struct drm_bo_driver {
413 const uint32_t *mem_type_prio;
414 const uint32_t *mem_busy_prio;
415 uint32_t num_mem_type_prio;
416 uint32_t num_mem_busy_prio;
417 drm_ttm_backend_t *(*create_ttm_backend_entry)
418 (struct drm_device * dev);
419 int (*fence_type) (struct drm_buffer_object *bo, uint32_t * class, uint32_t * type);
420 int (*invalidate_caches) (struct drm_device * dev, uint32_t flags);
421 int (*init_mem_type) (struct drm_device * dev, uint32_t type,
422 drm_mem_type_manager_t * man);
423 uint32_t(*evict_mask) (struct drm_buffer_object *bo);
424 int (*move) (struct drm_buffer_object * bo,
425 int evict, int no_wait, struct drm_bo_mem_reg * new_mem);
429 * buffer objects (drm_bo.c)
432 extern int drm_bo_ioctl(DRM_IOCTL_ARGS);
433 extern int drm_mm_init_ioctl(DRM_IOCTL_ARGS);
434 extern int drm_bo_driver_finish(struct drm_device *dev);
435 extern int drm_bo_driver_init(struct drm_device *dev);
436 extern int drm_bo_pci_offset(struct drm_device *dev,
437 drm_bo_mem_reg_t * mem,
438 unsigned long *bus_base,
439 unsigned long *bus_offset,
440 unsigned long *bus_size);
441 extern int drm_mem_reg_is_pci(struct drm_device *dev, drm_bo_mem_reg_t * mem);
443 extern void drm_bo_usage_deref_locked(drm_buffer_object_t * bo);
444 extern int drm_fence_buffer_objects(drm_file_t * priv,
445 struct list_head *list,
446 uint32_t fence_flags,
447 drm_fence_object_t * fence,
448 drm_fence_object_t ** used_fence);
449 extern void drm_bo_add_to_lru(drm_buffer_object_t * bo);
450 extern int drm_bo_wait(drm_buffer_object_t * bo, int lazy, int ignore_signals,
452 extern int drm_bo_mem_space(drm_buffer_object_t * bo,
453 drm_bo_mem_reg_t * mem, int no_wait);
454 extern int drm_bo_move_buffer(drm_buffer_object_t * bo, uint32_t new_mem_flags,
455 int no_wait, int move_unfenced);
458 * Buffer object memory move helpers.
462 extern int drm_bo_move_ttm(drm_buffer_object_t * bo,
463 int evict, int no_wait, drm_bo_mem_reg_t * new_mem);
464 extern int drm_bo_move_memcpy(drm_buffer_object_t * bo,
466 int no_wait, drm_bo_mem_reg_t * new_mem);
467 extern int drm_bo_move_accel_cleanup(drm_buffer_object_t * bo,
470 uint32_t fence_class,
472 uint32_t fence_flags,
473 drm_bo_mem_reg_t * new_mem);