remove some whitespace violation
[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 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;
248
249
250 typedef struct drm_ttm_backend {
251         uint32_t flags;
252         int mem_type;
253         drm_ttm_backend_func_t *func;
254 } drm_ttm_backend_t;
255
256 typedef struct drm_ttm {
257         struct page **pages;
258         uint32_t page_flags;
259         unsigned long num_pages;
260         unsigned long aper_offset;
261         atomic_t vma_count;
262         struct drm_device *dev;
263         int destroy;
264         uint32_t mapping_offset;
265         drm_ttm_backend_t *be;
266         enum {
267                 ttm_bound,
268                 ttm_evicted,
269                 ttm_unbound,
270                 ttm_unpopulated,
271         } state;
272
273 } drm_ttm_t;
274
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);
281
282 /*
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.
286  */
287
288 extern int drm_destroy_ttm(drm_ttm_t * ttm);
289
290 #define DRM_FLAG_MASKED(_old, _new, _mask) {\
291 (_old) ^= (((_old) ^ (_new)) & (_mask)); \
292 }
293
294 #define DRM_TTM_MASK_FLAGS ((1 << PAGE_SHIFT) - 1)
295 #define DRM_TTM_MASK_PFN (0xFFFFFFFFU - DRM_TTM_MASK_FLAGS)
296
297 /*
298  * Page flags.
299  */
300
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
306
307 /***************************************************
308  * Buffer objects. (drm_bo.c, drm_bo_move.c)
309  */
310
311 typedef struct drm_bo_mem_reg {
312         drm_mm_node_t *mm_node;
313         unsigned long size;
314         unsigned long num_pages;
315         uint32_t page_alignment;
316         uint32_t mem_type;
317         uint32_t flags;
318         uint32_t mask;
319 } drm_bo_mem_reg_t;
320
321 typedef struct drm_buffer_object {
322         struct drm_device *dev;
323         drm_user_object_t base;
324
325         /*
326          * If there is a possibility that the usage variable is zero,
327          * then dev->struct_mutext should be locked before incrementing it.
328          */
329
330         atomic_t usage;
331         unsigned long buffer_start;
332         drm_bo_type_t type;
333         unsigned long offset;
334         atomic_t mapped;
335         drm_bo_mem_reg_t mem;
336
337         struct list_head lru;
338         struct list_head ddestroy;
339
340         uint32_t fence_type;
341         uint32_t fence_class;
342         drm_fence_object_t *fence;
343         uint32_t priv_flags;
344         wait_queue_head_t event_queue;
345         struct mutex mutex;
346
347         /* For pinned buffers */
348         drm_mm_node_t *pinned_node;
349         uint32_t pinned_mem_type;
350         struct list_head pinned_lru;
351
352         /* For vm */
353
354         drm_ttm_t *ttm;
355         drm_map_list_t map_list;
356         uint32_t memory_type;
357         unsigned long bus_offset;
358         uint32_t vm_flags;
359         void *iomap;
360
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;
365 #endif
366
367 } drm_buffer_object_t;
368
369 #define _DRM_BO_FLAG_UNFENCED 0x00000001
370 #define _DRM_BO_FLAG_EVICTED  0x00000002
371
372 typedef struct drm_mem_type_manager {
373         int has_type;
374         int use_type;
375         drm_mm_t manager;
376         struct list_head lru;
377         struct list_head pinned;
378         uint32_t flags;
379         uint32_t drm_bus_maptype;
380         unsigned long io_offset;
381         unsigned long io_size;
382         void *io_addr;
383 } drm_mem_type_manager_t;
384
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 */
392
393 typedef struct drm_buffer_manager {
394         struct mutex init_mutex;
395         struct mutex evict_mutex;
396         int nice_mode;
397         int initialized;
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;
404 #else
405         struct delayed_work wq;
406 #endif
407         uint32_t fence_type;
408         unsigned long cur_pages;
409         atomic_t count;
410 } drm_buffer_manager_t;
411
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);
426 } drm_bo_driver_t;
427
428 /*
429  * buffer objects (drm_bo.c)
430  */
431
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);
442
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,
451                        int no_wait);
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);
456
457 /*
458  * Buffer object memory move helpers.
459  * drm_bo_move.c
460  */
461
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,
465                               int evict,
466                               int no_wait, drm_bo_mem_reg_t * new_mem);
467 extern int drm_bo_move_accel_cleanup(drm_buffer_object_t * bo,
468                                      int evict,
469                                      int no_wait,
470                                      uint32_t fence_class,
471                                      uint32_t fence_type,
472                                      uint32_t fence_flags,
473                                      drm_bo_mem_reg_t * new_mem);
474
475 #endif