Merge git://proxy01.pd.intel.com:9419/git/mesa/drm into crestline
[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_OJBECTS_H
33 #define DRM_HAS_TTM
34
35 struct drm_device;
36
37 /***************************************************
38  * User space objects. (drm_object.c)
39  */
40
41 #define drm_user_object_entry(_ptr, _type, _member) container_of(_ptr, _type, _member)
42
43 typedef enum {
44         drm_fence_type,
45         drm_buffer_type,
46         drm_ttm_type
47             /*
48              * Add other user space object types here.
49              */
50 } drm_object_type_t;
51
52 /*
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.
57  */
58
59 typedef struct drm_user_object {
60         drm_hash_item_t hash;
61         struct list_head list;
62         drm_object_type_t type;
63         atomic_t refcount;
64         int shareable;
65         drm_file_t *owner;
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);
72 } drm_user_object_t;
73
74 /*
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.
79  */
80
81 typedef struct drm_ref_object {
82         drm_hash_item_t hash;
83         struct list_head list;
84         atomic_t refcount;
85         drm_ref_t unref_action;
86 } drm_ref_object_t;
87
88 /**
89  * Must be called with the struct_mutex held.
90  */
91
92 extern int drm_add_user_object(drm_file_t * priv, drm_user_object_t * item,
93                                int shareable);
94 /**
95  * Must be called with the struct_mutex held.
96  */
97
98 extern drm_user_object_t *drm_lookup_user_object(drm_file_t * priv,
99                                                  uint32_t key);
100
101 /*
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.
106  */
107
108 extern int drm_remove_user_object(drm_file_t * priv, drm_user_object_t * item);
109
110 /*
111  * Must be called with the struct_mutex held. May temporarily release it.
112  */
113
114 extern int drm_add_ref_object(drm_file_t * priv,
115                               drm_user_object_t * referenced_object,
116                               drm_ref_t ref_action);
117
118 /*
119  * Must be called with the struct_mutex held.
120  */
121
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);
125 /*
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.
130  */
131
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);
138
139 /***************************************************
140  * Fence objects. (drm_fence.c)
141  */
142
143 typedef struct drm_fence_object {
144         drm_user_object_t base;
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 } drm_fence_object_t;
160
161 #define _DRM_FENCE_CLASSES 8
162 #define _DRM_FENCE_TYPE_EXE 0x00
163
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;
172
173 typedef struct drm_fence_manager {
174         int initialized;
175         rwlock_t lock;
176         drm_fence_class_manager_t class[_DRM_FENCE_CLASSES];
177         uint32_t num_classes;
178         atomic_t count;
179 } drm_fence_manager_t;
180
181 typedef 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 } drm_fence_driver_t;
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_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);
216
217 /**************************************************
218  *TTMs
219  */
220
221 /*
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.
232  */
233
234 #define DRM_BE_FLAG_NEEDS_FREE     0x00000001
235 #define DRM_BE_FLAG_BOUND_CACHED   0x00000002
236
237 typedef struct drm_ttm_backend {
238         void *private;
239         uint32_t flags;
240         uint32_t drm_map_type;
241         int (*needs_ub_cache_adjust) (struct drm_ttm_backend * backend);
242         int (*populate) (struct drm_ttm_backend * backend,
243                          unsigned long num_pages, struct page ** pages);
244         void (*clear) (struct drm_ttm_backend * backend);
245         int (*bind) (struct drm_ttm_backend * backend,
246                      unsigned long offset, int cached);
247         int (*unbind) (struct drm_ttm_backend * backend);
248         void (*destroy) (struct drm_ttm_backend * backend);
249 } drm_ttm_backend_t;
250
251 typedef struct drm_ttm {
252         struct page **pages;
253         uint32_t page_flags;
254         unsigned long num_pages;
255         unsigned long aper_offset;
256         atomic_t vma_count;
257         struct drm_device *dev;
258         int destroy;
259         uint32_t mapping_offset;
260         drm_ttm_backend_t *be;
261         enum {
262                 ttm_bound,
263                 ttm_evicted,
264                 ttm_unbound,
265                 ttm_unpopulated,
266         } state;
267
268 } drm_ttm_t;
269
270 extern drm_ttm_t *drm_ttm_init(struct drm_device *dev, unsigned long size);
271 extern int drm_bind_ttm(drm_ttm_t * ttm, int cached, unsigned long aper_offset);
272 extern void drm_ttm_unbind(drm_ttm_t * ttm);
273 extern void drm_ttm_evict(drm_ttm_t * ttm);
274 extern void drm_ttm_fixup_caching(drm_ttm_t * ttm);
275 extern struct page *drm_ttm_get_page(drm_ttm_t * ttm, int index);
276
277 /*
278  * Destroy a ttm. The user normally calls drmRmMap or a similar IOCTL to do this,
279  * which calls this function iff there are no vmas referencing it anymore. Otherwise it is called
280  * when the last vma exits.
281  */
282
283 extern int drm_destroy_ttm(drm_ttm_t * ttm);
284
285 #define DRM_FLAG_MASKED(_old, _new, _mask) {\
286 (_old) ^= (((_old) ^ (_new)) & (_mask)); \
287 }
288
289 #define DRM_TTM_MASK_FLAGS ((1 << PAGE_SHIFT) - 1)
290 #define DRM_TTM_MASK_PFN (0xFFFFFFFFU - DRM_TTM_MASK_FLAGS)
291
292 /*
293  * Page flags.
294  */
295
296 #define DRM_TTM_PAGE_UNCACHED 0x01
297 #define DRM_TTM_PAGE_USED     0x02
298 #define DRM_TTM_PAGE_BOUND    0x04
299 #define DRM_TTM_PAGE_PRESENT  0x08
300 #define DRM_TTM_PAGE_VMALLOC  0x10
301
302 /***************************************************
303  * Buffer objects. (drm_bo.c, drm_bo_move.c)
304  */
305
306 typedef struct drm_bo_mem_reg {
307         drm_mm_node_t *mm_node;
308         unsigned long size;
309         unsigned long num_pages;
310         uint32_t page_alignment;
311         uint32_t mem_type;
312         uint32_t flags;
313         uint32_t mask;
314 } drm_bo_mem_reg_t;
315
316 typedef struct drm_buffer_object {
317         struct drm_device *dev;
318         drm_user_object_t base;
319
320         /*
321          * If there is a possibility that the usage variable is zero,
322          * then dev->struct_mutext should be locked before incrementing it.
323          */
324
325         atomic_t usage;
326         unsigned long buffer_start;
327         drm_bo_type_t type;
328         unsigned long offset;
329         atomic_t mapped;
330         drm_bo_mem_reg_t mem;
331
332         struct list_head lru;
333         struct list_head ddestroy;
334
335         uint32_t fence_type;
336         uint32_t fence_class;
337         drm_fence_object_t *fence;
338         uint32_t priv_flags;
339         wait_queue_head_t event_queue;
340         struct mutex mutex;
341
342         /* For pinned buffers */
343         drm_mm_node_t *pinned_node;
344         uint32_t pinned_mem_type;
345         struct list_head pinned_lru;
346
347         /* For vm */
348
349         drm_ttm_t *ttm;
350         drm_map_list_t map_list;
351         uint32_t memory_type;
352         unsigned long bus_offset;
353         uint32_t vm_flags;
354         void *iomap;
355
356 #ifdef DRM_ODD_MM_COMPAT
357         /* dev->struct_mutex only protected. */
358         struct list_head vma_list;
359         struct list_head p_mm_list;
360 #endif
361
362 } drm_buffer_object_t;
363
364 #define _DRM_BO_FLAG_UNFENCED 0x00000001
365 #define _DRM_BO_FLAG_EVICTED  0x00000002
366
367 typedef struct drm_mem_type_manager {
368         int has_type;
369         int use_type;
370         drm_mm_t manager;
371         struct list_head lru;
372         struct list_head pinned;
373         uint32_t flags;
374         uint32_t drm_bus_maptype;
375         unsigned long io_offset;
376         unsigned long io_size;
377         void *io_addr;
378 } drm_mem_type_manager_t;
379
380 #define _DRM_FLAG_MEMTYPE_FIXED     0x00000001  /* Fixed (on-card) PCI memory */
381 #define _DRM_FLAG_MEMTYPE_MAPPABLE  0x00000002  /* Memory mappable */
382 #define _DRM_FLAG_MEMTYPE_CACHED    0x00000004  /* Cached binding */
383 #define _DRM_FLAG_NEEDS_IOREMAP     0x00000008  /* Fixed memory needs ioremap
384                                                    before kernel access. */
385 #define _DRM_FLAG_MEMTYPE_CMA       0x00000010  /* Can't map aperture */
386 #define _DRM_FLAG_MEMTYPE_CSELECT   0x00000020  /* Select caching */
387
388 typedef struct drm_buffer_manager {
389         struct mutex init_mutex;
390         struct mutex evict_mutex;
391         int nice_mode;
392         int initialized;
393         drm_file_t *last_to_validate;
394         drm_mem_type_manager_t man[DRM_BO_MEM_TYPES];
395         struct list_head unfenced;
396         struct list_head ddestroy;
397 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
398         struct work_struct wq;
399 #else
400         struct delayed_work wq;
401 #endif
402         uint32_t fence_type;
403         unsigned long cur_pages;
404         atomic_t count;
405 } drm_buffer_manager_t;
406
407 typedef struct drm_bo_driver {
408         const uint32_t *mem_type_prio;
409         const uint32_t *mem_busy_prio;
410         uint32_t num_mem_type_prio;
411         uint32_t num_mem_busy_prio;
412         drm_ttm_backend_t *(*create_ttm_backend_entry)
413          (struct drm_device * dev);
414         int (*fence_type) (struct drm_buffer_object *bo, uint32_t * class, uint32_t * type);
415         int (*invalidate_caches) (struct drm_device * dev, uint32_t flags);
416         int (*init_mem_type) (struct drm_device * dev, uint32_t type,
417                               drm_mem_type_manager_t * man);
418          uint32_t(*evict_mask) (struct drm_buffer_object *bo);
419         int (*move) (struct drm_buffer_object * bo,
420                      int evict, int no_wait, struct drm_bo_mem_reg * new_mem);
421 } drm_bo_driver_t;
422
423 /*
424  * buffer objects (drm_bo.c)
425  */
426
427 extern int drm_bo_ioctl(DRM_IOCTL_ARGS);
428 extern int drm_mm_init_ioctl(DRM_IOCTL_ARGS);
429 extern int drm_bo_driver_finish(struct drm_device *dev);
430 extern int drm_bo_driver_init(struct drm_device *dev);
431 extern int drm_bo_pci_offset(struct drm_device *dev,
432                              drm_bo_mem_reg_t * mem,
433                              unsigned long *bus_base,
434                              unsigned long *bus_offset,
435                              unsigned long *bus_size);
436 extern int drm_mem_reg_is_pci(struct drm_device *dev, drm_bo_mem_reg_t * mem);
437
438 extern void drm_bo_usage_deref_locked(drm_buffer_object_t * bo);
439 extern int drm_fence_buffer_objects(drm_file_t * priv,
440                                     struct list_head *list,
441                                     uint32_t fence_flags,
442                                     drm_fence_object_t * fence,
443                                     drm_fence_object_t ** used_fence);
444 extern void drm_bo_add_to_lru(drm_buffer_object_t * bo);
445 extern int drm_bo_wait(drm_buffer_object_t * bo, int lazy, int ignore_signals,
446                        int no_wait);
447 extern int drm_bo_mem_space(drm_buffer_object_t * bo,
448                             drm_bo_mem_reg_t * mem, int no_wait);
449 extern int drm_bo_move_buffer(drm_buffer_object_t * bo, uint32_t new_mem_flags,
450                               int no_wait, int move_unfenced);
451
452 /*
453  * Buffer object memory move helpers.
454  * drm_bo_move.c
455  */
456
457 extern int drm_bo_move_ttm(drm_buffer_object_t * bo,
458                            int evict, int no_wait, drm_bo_mem_reg_t * new_mem);
459 extern int drm_bo_move_memcpy(drm_buffer_object_t * bo,
460                               int evict,
461                               int no_wait, drm_bo_mem_reg_t * new_mem);
462 extern int drm_bo_move_accel_cleanup(drm_buffer_object_t * bo,
463                                      int evict,
464                                      int no_wait,
465                                      uint32_t fence_class,
466                                      uint32_t fence_type,
467                                      uint32_t fence_flags,
468                                      drm_bo_mem_reg_t * new_mem);
469
470 #endif