drm: remove drm_ref_t
[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(DRM_IOCTL_ARGS);
217 extern int drm_fence_destroy_ioctl(DRM_IOCTL_ARGS);
218 extern int drm_fence_reference_ioctl(DRM_IOCTL_ARGS);
219 extern int drm_fence_unreference_ioctl(DRM_IOCTL_ARGS);
220 extern int drm_fence_signaled_ioctl(DRM_IOCTL_ARGS);
221 extern int drm_fence_flush_ioctl(DRM_IOCTL_ARGS);
222 extern int drm_fence_wait_ioctl(DRM_IOCTL_ARGS);
223 extern int drm_fence_emit_ioctl(DRM_IOCTL_ARGS);
224 extern int drm_fence_buffers_ioctl(DRM_IOCTL_ARGS);
225 /**************************************************
226  *TTMs
227  */
228
229 /*
230  * The ttm backend GTT interface. (In our case AGP).
231  * Any similar type of device (PCIE?)
232  * needs only to implement these functions to be usable with the "TTM" interface.
233  * The AGP backend implementation lives in drm_agpsupport.c
234  * basically maps these calls to available functions in agpgart.
235  * Each drm device driver gets an
236  * additional function pointer that creates these types,
237  * so that the device can choose the correct aperture.
238  * (Multiple AGP apertures, etc.)
239  * Most device drivers will let this point to the standard AGP implementation.
240  */
241
242 #define DRM_BE_FLAG_NEEDS_FREE     0x00000001
243 #define DRM_BE_FLAG_BOUND_CACHED   0x00000002
244
245 struct drm_ttm_backend;
246 struct drm_ttm_backend_func {
247         int (*needs_ub_cache_adjust) (struct drm_ttm_backend * backend);
248         int (*populate) (struct drm_ttm_backend * backend,
249                          unsigned long num_pages, struct page ** pages);
250         void (*clear) (struct drm_ttm_backend * backend);
251         int (*bind) (struct drm_ttm_backend * backend,
252                      unsigned long offset, int cached);
253         int (*unbind) (struct drm_ttm_backend * backend);
254         void (*destroy) (struct drm_ttm_backend * backend);
255 };
256
257
258 struct drm_ttm_backend {
259         uint32_t flags;
260         int mem_type;
261         struct drm_ttm_backend_func *func;
262 };
263
264 struct drm_ttm {
265         struct page **pages;
266         uint32_t page_flags;
267         unsigned long num_pages;
268         unsigned long aper_offset;
269         atomic_t vma_count;
270         struct drm_device *dev;
271         int destroy;
272         uint32_t mapping_offset;
273         struct drm_ttm_backend *be;
274         enum {
275                 ttm_bound,
276                 ttm_evicted,
277                 ttm_unbound,
278                 ttm_unpopulated,
279         } state;
280
281 };
282
283 extern struct drm_ttm *drm_ttm_init(struct drm_device *dev, unsigned long size);
284 extern int drm_bind_ttm(struct drm_ttm * ttm, int cached, unsigned long aper_offset);
285 extern void drm_ttm_unbind(struct drm_ttm * ttm);
286 extern void drm_ttm_evict(struct drm_ttm * ttm);
287 extern void drm_ttm_fixup_caching(struct drm_ttm * ttm);
288 extern struct page *drm_ttm_get_page(struct drm_ttm * ttm, int index);
289
290 /*
291  * Destroy a ttm. The user normally calls drmRmMap or a similar IOCTL to do this,
292  * which calls this function iff there are no vmas referencing it anymore. Otherwise it is called
293  * when the last vma exits.
294  */
295
296 extern int drm_destroy_ttm(struct drm_ttm * ttm);
297
298 #define DRM_FLAG_MASKED(_old, _new, _mask) {\
299 (_old) ^= (((_old) ^ (_new)) & (_mask)); \
300 }
301
302 #define DRM_TTM_MASK_FLAGS ((1 << PAGE_SHIFT) - 1)
303 #define DRM_TTM_MASK_PFN (0xFFFFFFFFU - DRM_TTM_MASK_FLAGS)
304
305 /*
306  * Page flags.
307  */
308
309 #define DRM_TTM_PAGE_UNCACHED 0x01
310 #define DRM_TTM_PAGE_USED     0x02
311 #define DRM_TTM_PAGE_BOUND    0x04
312 #define DRM_TTM_PAGE_PRESENT  0x08
313 #define DRM_TTM_PAGE_VMALLOC  0x10
314
315 /***************************************************
316  * Buffer objects. (drm_bo.c, drm_bo_move.c)
317  */
318
319 struct drm_bo_mem_reg {
320         struct drm_mm_node *mm_node;
321         unsigned long size;
322         unsigned long num_pages;
323         uint32_t page_alignment;
324         uint32_t mem_type;
325         uint64_t flags;
326         uint64_t mask;
327 };
328
329 struct drm_buffer_object {
330         struct drm_device *dev;
331         struct drm_user_object base;
332
333         /*
334          * If there is a possibility that the usage variable is zero,
335          * then dev->struct_mutext should be locked before incrementing it.
336          */
337
338         atomic_t usage;
339         unsigned long buffer_start;
340         enum drm_bo_type type;
341         unsigned long offset;
342         atomic_t mapped;
343         struct drm_bo_mem_reg mem;
344
345         struct list_head lru;
346         struct list_head ddestroy;
347
348         uint32_t fence_type;
349         uint32_t fence_class;
350         struct drm_fence_object *fence;
351         uint32_t priv_flags;
352         wait_queue_head_t event_queue;
353         struct mutex mutex;
354
355         /* For pinned buffers */
356         struct drm_mm_node *pinned_node;
357         uint32_t pinned_mem_type;
358         struct list_head pinned_lru;
359
360         /* For vm */
361
362         struct drm_ttm *ttm;
363         struct drm_map_list map_list;
364         uint32_t memory_type;
365         unsigned long bus_offset;
366         uint32_t vm_flags;
367         void *iomap;
368
369 #ifdef DRM_ODD_MM_COMPAT
370         /* dev->struct_mutex only protected. */
371         struct list_head vma_list;
372         struct list_head p_mm_list;
373 #endif
374
375 };
376
377 #define _DRM_BO_FLAG_UNFENCED 0x00000001
378 #define _DRM_BO_FLAG_EVICTED  0x00000002
379
380 struct drm_mem_type_manager {
381         int has_type;
382         int use_type;
383         struct drm_mm manager;
384         struct list_head lru;
385         struct list_head pinned;
386         uint32_t flags;
387         uint32_t drm_bus_maptype;
388         unsigned long io_offset;
389         unsigned long io_size;
390         void *io_addr;
391 };
392
393 #define _DRM_FLAG_MEMTYPE_FIXED     0x00000001  /* Fixed (on-card) PCI memory */
394 #define _DRM_FLAG_MEMTYPE_MAPPABLE  0x00000002  /* Memory mappable */
395 #define _DRM_FLAG_MEMTYPE_CACHED    0x00000004  /* Cached binding */
396 #define _DRM_FLAG_NEEDS_IOREMAP     0x00000008  /* Fixed memory needs ioremap
397                                                    before kernel access. */
398 #define _DRM_FLAG_MEMTYPE_CMA       0x00000010  /* Can't map aperture */
399 #define _DRM_FLAG_MEMTYPE_CSELECT   0x00000020  /* Select caching */
400
401 struct drm_buffer_manager {
402         struct mutex init_mutex;
403         struct mutex evict_mutex;
404         int nice_mode;
405         int initialized;
406         struct drm_file *last_to_validate;
407         struct drm_mem_type_manager man[DRM_BO_MEM_TYPES];
408         struct list_head unfenced;
409         struct list_head ddestroy;
410 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
411         struct work_struct wq;
412 #else
413         struct delayed_work wq;
414 #endif
415         uint32_t fence_type;
416         unsigned long cur_pages;
417         atomic_t count;
418 };
419
420 struct drm_bo_driver {
421         const uint32_t *mem_type_prio;
422         const uint32_t *mem_busy_prio;
423         uint32_t num_mem_type_prio;
424         uint32_t num_mem_busy_prio;
425         struct drm_ttm_backend *(*create_ttm_backend_entry)
426          (struct drm_device * dev);
427         int (*fence_type) (struct drm_buffer_object *bo, uint32_t * type);
428         int (*invalidate_caches) (struct drm_device * dev, uint64_t flags);
429         int (*init_mem_type) (struct drm_device * dev, uint32_t type,
430                               struct drm_mem_type_manager * man);
431          uint32_t(*evict_mask) (struct drm_buffer_object *bo);
432         int (*move) (struct drm_buffer_object * bo,
433                      int evict, int no_wait, struct drm_bo_mem_reg * new_mem);
434 };
435
436 /*
437  * buffer objects (drm_bo.c)
438  */
439
440 extern int drm_bo_create_ioctl(DRM_IOCTL_ARGS);
441 extern int drm_bo_destroy_ioctl(DRM_IOCTL_ARGS);
442 extern int drm_bo_map_ioctl(DRM_IOCTL_ARGS);
443 extern int drm_bo_unmap_ioctl(DRM_IOCTL_ARGS);
444 extern int drm_bo_reference_ioctl(DRM_IOCTL_ARGS);
445 extern int drm_bo_unreference_ioctl(DRM_IOCTL_ARGS);
446 extern int drm_bo_wait_idle_ioctl(DRM_IOCTL_ARGS);
447 extern int drm_bo_info_ioctl(DRM_IOCTL_ARGS);
448 extern int drm_bo_op_ioctl(DRM_IOCTL_ARGS);
449
450
451 extern int drm_mm_init_ioctl(DRM_IOCTL_ARGS);
452 extern int drm_mm_takedown_ioctl(DRM_IOCTL_ARGS);
453 extern int drm_mm_lock_ioctl(DRM_IOCTL_ARGS);
454 extern int drm_mm_unlock_ioctl(DRM_IOCTL_ARGS);
455 extern int drm_bo_driver_finish(struct drm_device *dev);
456 extern int drm_bo_driver_init(struct drm_device *dev);
457 extern int drm_bo_pci_offset(struct drm_device *dev,
458                              struct drm_bo_mem_reg * mem,
459                              unsigned long *bus_base,
460                              unsigned long *bus_offset,
461                              unsigned long *bus_size);
462 extern int drm_mem_reg_is_pci(struct drm_device *dev, struct drm_bo_mem_reg * mem);
463
464 extern void drm_bo_usage_deref_locked(struct drm_buffer_object ** bo);
465 extern int drm_fence_buffer_objects(struct drm_file * priv,
466                                     struct list_head *list,
467                                     uint32_t fence_flags,
468                                     struct drm_fence_object * fence,
469                                     struct drm_fence_object ** used_fence);
470 extern void drm_bo_add_to_lru(struct drm_buffer_object * bo);
471 extern int drm_bo_wait(struct drm_buffer_object * bo, int lazy, int ignore_signals,
472                        int no_wait);
473 extern int drm_bo_mem_space(struct drm_buffer_object * bo,
474                             struct drm_bo_mem_reg * mem, int no_wait);
475 extern int drm_bo_move_buffer(struct drm_buffer_object * bo, uint32_t new_mem_flags,
476                               int no_wait, int move_unfenced);
477
478 /*
479  * Buffer object memory move helpers.
480  * drm_bo_move.c
481  */
482
483 extern int drm_bo_move_ttm(struct drm_buffer_object * bo,
484                            int evict, int no_wait, struct drm_bo_mem_reg * new_mem);
485 extern int drm_bo_move_memcpy(struct drm_buffer_object * bo,
486                               int evict,
487                               int no_wait, struct drm_bo_mem_reg * new_mem);
488 extern int drm_bo_move_accel_cleanup(struct drm_buffer_object * bo,
489                                      int evict,
490                                      int no_wait,
491                                      uint32_t fence_class,
492                                      uint32_t fence_type,
493                                      uint32_t fence_flags,
494                                      struct drm_bo_mem_reg * new_mem);
495
496 #ifdef CONFIG_DEBUG_MUTEXES
497 #define DRM_ASSERT_LOCKED(_mutex)                                       \
498         BUG_ON(!mutex_is_locked(_mutex) ||                              \
499                ((_mutex)->owner != current_thread_info()))
500 #else
501 #define DRM_ASSERT_LOCKED(_mutex)
502 #endif
503
504 #endif