Merge branch 'master' of ssh+git://git.freedesktop.org/git/mesa/drm into xgi-0-0-2
[profile/ivi/libdrm.git] / linux-core / drm_objects.h
1 /**************************************************************************
2  *
3  * Copyright (c) 2006-2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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.
25  *
26  **************************************************************************/
27 /*
28  * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
29  */
30
31 #ifndef _DRM_OBJECTS_H
32 #define _DRM_OBJECTS_H
33
34 struct drm_device;
35
36 /***************************************************
37  * User space objects. (drm_object.c)
38  */
39
40 #define drm_user_object_entry(_ptr, _type, _member) container_of(_ptr, _type, _member)
41
42 enum drm_object_type {
43         drm_fence_type,
44         drm_buffer_type,
45         drm_ttm_type
46             /*
47              * Add other user space object types here.
48              */
49 };
50
51 /*
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.
56  */
57
58 struct drm_user_object {
59         struct drm_hash_item hash;
60         struct list_head list;
61         enum drm_object_type type;
62         atomic_t refcount;
63         int shareable;
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);
71 };
72
73 /*
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.
78  */
79
80 struct drm_ref_object {
81         struct drm_hash_item hash;
82         struct list_head list;
83         atomic_t refcount;
84         enum drm_ref_type unref_action;
85 };
86
87 /**
88  * Must be called with the struct_mutex held.
89  */
90
91 extern int drm_add_user_object(struct drm_file * priv, struct drm_user_object * item,
92                                int shareable);
93 /**
94  * Must be called with the struct_mutex held.
95  */
96
97 extern struct drm_user_object *drm_lookup_user_object(struct drm_file * priv,
98                                                  uint32_t key);
99
100 /*
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.
105  */
106
107 extern int drm_remove_user_object(struct drm_file * priv, struct drm_user_object * item);
108
109 /*
110  * Must be called with the struct_mutex held. May temporarily release it.
111  */
112
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);
116
117 /*
118  * Must be called with the struct_mutex held.
119  */
120
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);
124 /*
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.
129  */
130
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);
137
138 /***************************************************
139  * Fence objects. (drm_fence.c)
140  */
141
142 struct drm_fence_object {
143         struct drm_user_object base;
144         struct drm_device *dev;
145         atomic_t usage;
146
147         /*
148          * The below three fields are protected by the fence manager spinlock.
149          */
150
151         struct list_head ring;
152         int class;
153         uint32_t native_type;
154         uint32_t type;
155         uint32_t signaled;
156         uint32_t sequence;
157         uint32_t flush_mask;
158         uint32_t submitted_flush;
159 };
160
161 #define _DRM_FENCE_CLASSES 8
162 #define _DRM_FENCE_TYPE_EXE 0x00
163
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;
171 };
172
173 struct drm_fence_manager {
174         int initialized;
175         rwlock_t lock;
176         struct drm_fence_class_manager class[_DRM_FENCE_CLASSES];
177         uint32_t num_classes;
178         atomic_t count;
179 };
180
181 struct drm_fence_driver {
182         uint32_t num_classes;
183         uint32_t wrap_diff;
184         uint32_t flush_diff;
185         uint32_t sequence_mask;
186         int lazy_capable;
187         int (*has_irq) (struct drm_device * dev, uint32_t class,
188                         uint32_t flags);
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 };
193
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,
199                                 uint32_t sequence);
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);
215
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 /**************************************************
235  *TTMs
236  */
237
238 /*
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.
249  */
250
251 #define DRM_BE_FLAG_NEEDS_FREE     0x00000001
252 #define DRM_BE_FLAG_BOUND_CACHED   0x00000002
253
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);
264 };
265
266
267 struct drm_ttm_backend {
268         uint32_t flags;
269         int mem_type;
270         struct drm_ttm_backend_func *func;
271 };
272
273 struct drm_ttm {
274         struct page **pages;
275         uint32_t page_flags;
276         unsigned long num_pages;
277         unsigned long aper_offset;
278         atomic_t vma_count;
279         struct drm_device *dev;
280         int destroy;
281         uint32_t mapping_offset;
282         struct drm_ttm_backend *be;
283         enum {
284                 ttm_bound,
285                 ttm_evicted,
286                 ttm_unbound,
287                 ttm_unpopulated,
288         } state;
289
290 };
291
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);
298
299 /*
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.
303  */
304
305 extern int drm_destroy_ttm(struct drm_ttm * ttm);
306
307 #define DRM_FLAG_MASKED(_old, _new, _mask) {\
308 (_old) ^= (((_old) ^ (_new)) & (_mask)); \
309 }
310
311 #define DRM_TTM_MASK_FLAGS ((1 << PAGE_SHIFT) - 1)
312 #define DRM_TTM_MASK_PFN (0xFFFFFFFFU - DRM_TTM_MASK_FLAGS)
313
314 /*
315  * Page flags.
316  */
317
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
323
324 /***************************************************
325  * Buffer objects. (drm_bo.c, drm_bo_move.c)
326  */
327
328 struct drm_bo_mem_reg {
329         struct drm_mm_node *mm_node;
330         unsigned long size;
331         unsigned long num_pages;
332         uint32_t page_alignment;
333         uint32_t mem_type;
334         uint64_t flags;
335         uint64_t mask;
336 };
337
338 struct drm_buffer_object {
339         struct drm_device *dev;
340         struct drm_user_object base;
341
342         /*
343          * If there is a possibility that the usage variable is zero,
344          * then dev->struct_mutext should be locked before incrementing it.
345          */
346
347         atomic_t usage;
348         unsigned long buffer_start;
349         enum drm_bo_type type;
350         unsigned long offset;
351         atomic_t mapped;
352         struct drm_bo_mem_reg mem;
353
354         struct list_head lru;
355         struct list_head ddestroy;
356
357         uint32_t fence_type;
358         uint32_t fence_class;
359         struct drm_fence_object *fence;
360         uint32_t priv_flags;
361         wait_queue_head_t event_queue;
362         struct mutex mutex;
363
364         /* For pinned buffers */
365         struct drm_mm_node *pinned_node;
366         uint32_t pinned_mem_type;
367         struct list_head pinned_lru;
368
369         /* For vm */
370
371         struct drm_ttm *ttm;
372         struct drm_map_list map_list;
373         uint32_t memory_type;
374         unsigned long bus_offset;
375         uint32_t vm_flags;
376         void *iomap;
377
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;
382 #endif
383
384 };
385
386 #define _DRM_BO_FLAG_UNFENCED 0x00000001
387 #define _DRM_BO_FLAG_EVICTED  0x00000002
388
389 struct drm_mem_type_manager {
390         int has_type;
391         int use_type;
392         struct drm_mm manager;
393         struct list_head lru;
394         struct list_head pinned;
395         uint32_t flags;
396         uint32_t drm_bus_maptype;
397         unsigned long io_offset;
398         unsigned long io_size;
399         void *io_addr;
400 };
401
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 */
409
410 struct drm_buffer_manager {
411         struct mutex init_mutex;
412         struct mutex evict_mutex;
413         int nice_mode;
414         int initialized;
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;
421 #else
422         struct delayed_work wq;
423 #endif
424         uint32_t fence_type;
425         unsigned long cur_pages;
426         atomic_t count;
427 };
428
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);
443 };
444
445 /*
446  * buffer objects (drm_bo.c)
447  */
448
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);
458
459
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);
472
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,
481                        int no_wait);
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);
486
487 /*
488  * Buffer object memory move helpers.
489  * drm_bo_move.c
490  */
491
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,
495                               int evict,
496                               int no_wait, struct drm_bo_mem_reg * new_mem);
497 extern int drm_bo_move_accel_cleanup(struct drm_buffer_object * bo,
498                                      int evict,
499                                      int no_wait,
500                                      uint32_t fence_class,
501                                      uint32_t fence_type,
502                                      uint32_t fence_flags,
503                                      struct drm_bo_mem_reg * new_mem);
504
505 #ifdef CONFIG_DEBUG_MUTEXES
506 #define DRM_ASSERT_LOCKED(_mutex)                                       \
507         BUG_ON(!mutex_is_locked(_mutex) ||                              \
508                ((_mutex)->owner != current_thread_info()))
509 #else
510 #define DRM_ASSERT_LOCKED(_mutex)
511 #endif
512
513 #endif