modesetting-101: rename modeflags, as to avoid conflicts with the xorg definitions
[platform/upstream/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 struct drm_bo_mem_reg;
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 enum drm_object_type {
44         drm_fence_type,
45         drm_buffer_type,
46         drm_lock_type,
47             /*
48              * Add other user space object types here.
49              */
50         drm_driver_type0 = 256,
51         drm_driver_type1,
52         drm_driver_type2,
53         drm_driver_type3,
54         drm_driver_type4
55 };
56
57 /*
58  * A user object is a structure that helps the drm give out user handles
59  * to kernel internal objects and to keep track of these objects so that
60  * they can be destroyed, for example when the user space process exits.
61  * Designed to be accessible using a user space 32-bit handle.
62  */
63
64 struct drm_user_object {
65         struct drm_hash_item hash;
66         struct list_head list;
67         enum drm_object_type type;
68         atomic_t refcount;
69         int shareable;
70         struct drm_file *owner;
71         void (*ref_struct_locked) (struct drm_file *priv,
72                                    struct drm_user_object *obj,
73                                    enum drm_ref_type ref_action);
74         void (*unref) (struct drm_file *priv, struct drm_user_object *obj,
75                        enum drm_ref_type unref_action);
76         void (*remove) (struct drm_file *priv, struct drm_user_object *obj);
77 };
78
79 /*
80  * A ref object is a structure which is used to
81  * keep track of references to user objects and to keep track of these
82  * references so that they can be destroyed for example when the user space
83  * process exits. Designed to be accessible using a pointer to the _user_ object.
84  */
85
86 struct drm_ref_object {
87         struct drm_hash_item hash;
88         struct list_head list;
89         atomic_t refcount;
90         enum drm_ref_type unref_action;
91 };
92
93 /**
94  * Must be called with the struct_mutex held.
95  */
96
97 extern int drm_add_user_object(struct drm_file *priv, struct drm_user_object *item,
98                                int shareable);
99 /**
100  * Must be called with the struct_mutex held.
101  */
102
103 extern struct drm_user_object *drm_lookup_user_object(struct drm_file *priv,
104                                                  uint32_t key);
105
106 /*
107  * Must be called with the struct_mutex held. May temporarily release it.
108  */
109
110 extern int drm_add_ref_object(struct drm_file *priv,
111                               struct drm_user_object *referenced_object,
112                               enum drm_ref_type ref_action);
113
114 /*
115  * Must be called with the struct_mutex held.
116  */
117
118 struct drm_ref_object *drm_lookup_ref_object(struct drm_file *priv,
119                                         struct drm_user_object *referenced_object,
120                                         enum drm_ref_type ref_action);
121 /*
122  * Must be called with the struct_mutex held.
123  * If "item" has been obtained by a call to drm_lookup_ref_object. You may not
124  * release the struct_mutex before calling drm_remove_ref_object.
125  * This function may temporarily release the struct_mutex.
126  */
127
128 extern void drm_remove_ref_object(struct drm_file *priv, struct drm_ref_object *item);
129 extern int drm_user_object_ref(struct drm_file *priv, uint32_t user_token,
130                                enum drm_object_type type,
131                                struct drm_user_object **object);
132 extern int drm_user_object_unref(struct drm_file *priv, uint32_t user_token,
133                                  enum drm_object_type type);
134
135 /***************************************************
136  * Fence objects. (drm_fence.c)
137  */
138
139 struct drm_fence_object {
140         struct drm_user_object base;
141         struct drm_device *dev;
142         atomic_t usage;
143
144         /*
145          * The below three fields are protected by the fence manager spinlock.
146          */
147
148         struct list_head ring;
149         int fence_class;
150         uint32_t native_types;
151         uint32_t type;
152         uint32_t signaled_types;
153         uint32_t sequence;
154         uint32_t waiting_types;
155         uint32_t error;
156 };
157
158 #define _DRM_FENCE_CLASSES 8
159
160 struct drm_fence_class_manager {
161         struct list_head ring;
162         uint32_t pending_flush;
163         uint32_t waiting_types;
164         wait_queue_head_t fence_queue;
165         uint32_t highest_waiting_sequence;
166         uint32_t latest_queued_sequence;
167 };
168
169 struct drm_fence_manager {
170         int initialized;
171         rwlock_t lock;
172         struct drm_fence_class_manager fence_class[_DRM_FENCE_CLASSES];
173         uint32_t num_classes;
174         atomic_t count;
175 };
176
177 struct drm_fence_driver {
178         unsigned long *waiting_jiffies;
179         uint32_t num_classes;
180         uint32_t wrap_diff;
181         uint32_t flush_diff;
182         uint32_t sequence_mask;
183
184         /*
185          * Driver implemented functions:
186          * has_irq() : 1 if the hardware can update the indicated type_flags using an
187          * irq handler. 0 if polling is required.
188          *
189          * emit() : Emit a sequence number to the command stream.
190          * Return the sequence number.
191          *
192          * flush() : Make sure the flags indicated in fc->pending_flush will eventually
193          * signal for fc->highest_received_sequence and all preceding sequences.
194          * Acknowledge by clearing the flags fc->pending_flush.
195          *
196          * poll() : Call drm_fence_handler with any new information.
197          *
198          * needed_flush() : Given the current state of the fence->type flags and previusly 
199          * executed or queued flushes, return the type_flags that need flushing.
200          *
201          * wait(): Wait for the "mask" flags to signal on a given fence, performing
202          * whatever's necessary to make this happen.
203          */
204
205         int (*has_irq) (struct drm_device *dev, uint32_t fence_class,
206                         uint32_t flags);
207         int (*emit) (struct drm_device *dev, uint32_t fence_class,
208                      uint32_t flags, uint32_t *breadcrumb,
209                      uint32_t *native_type);
210         void (*flush) (struct drm_device *dev, uint32_t fence_class);
211         void (*poll) (struct drm_device *dev, uint32_t fence_class,
212                 uint32_t types);
213         uint32_t (*needed_flush) (struct drm_fence_object *fence);
214         int (*wait) (struct drm_fence_object *fence, int lazy,
215                      int interruptible, uint32_t mask);
216 };
217
218 extern int drm_fence_wait_polling(struct drm_fence_object *fence, int lazy,
219                                   int interruptible, uint32_t mask,
220                                   unsigned long end_jiffies);
221 extern void drm_fence_handler(struct drm_device *dev, uint32_t fence_class,
222                               uint32_t sequence, uint32_t type,
223                               uint32_t error);
224 extern void drm_fence_manager_init(struct drm_device *dev);
225 extern void drm_fence_manager_takedown(struct drm_device *dev);
226 extern void drm_fence_flush_old(struct drm_device *dev, uint32_t fence_class,
227                                 uint32_t sequence);
228 extern int drm_fence_object_flush(struct drm_fence_object *fence,
229                                   uint32_t type);
230 extern int drm_fence_object_signaled(struct drm_fence_object *fence,
231                                      uint32_t type);
232 extern void drm_fence_usage_deref_locked(struct drm_fence_object **fence);
233 extern void drm_fence_usage_deref_unlocked(struct drm_fence_object **fence);
234 extern struct drm_fence_object *drm_fence_reference_locked(struct drm_fence_object *src);
235 extern void drm_fence_reference_unlocked(struct drm_fence_object **dst,
236                                          struct drm_fence_object *src);
237 extern int drm_fence_object_wait(struct drm_fence_object *fence,
238                                  int lazy, int ignore_signals, uint32_t mask);
239 extern int drm_fence_object_create(struct drm_device *dev, uint32_t type,
240                                    uint32_t fence_flags, uint32_t fence_class,
241                                    struct drm_fence_object **c_fence);
242 extern int drm_fence_object_emit(struct drm_fence_object *fence,
243                                  uint32_t fence_flags, uint32_t class,
244                                  uint32_t type);
245 extern void drm_fence_fill_arg(struct drm_fence_object *fence,
246                                struct drm_fence_arg *arg);
247
248 extern int drm_fence_add_user_object(struct drm_file *priv,
249                                      struct drm_fence_object *fence,
250                                      int shareable);
251
252 extern int drm_fence_create_ioctl(struct drm_device *dev, void *data,
253                                   struct drm_file *file_priv);
254 extern int drm_fence_destroy_ioctl(struct drm_device *dev, void *data,
255                                    struct drm_file *file_priv);
256 extern int drm_fence_reference_ioctl(struct drm_device *dev, void *data,
257                                      struct drm_file *file_priv);
258 extern int drm_fence_unreference_ioctl(struct drm_device *dev, void *data,
259                                        struct drm_file *file_priv);
260 extern int drm_fence_signaled_ioctl(struct drm_device *dev, void *data,
261                                     struct drm_file *file_priv);
262 extern int drm_fence_flush_ioctl(struct drm_device *dev, void *data,
263                                  struct drm_file *file_priv);
264 extern int drm_fence_wait_ioctl(struct drm_device *dev, void *data,
265                                 struct drm_file *file_priv);
266 extern int drm_fence_emit_ioctl(struct drm_device *dev, void *data,
267                                 struct drm_file *file_priv);
268 extern int drm_fence_buffers_ioctl(struct drm_device *dev, void *data,
269                                    struct drm_file *file_priv);
270 /**************************************************
271  *TTMs
272  */
273
274 /*
275  * The ttm backend GTT interface. (In our case AGP).
276  * Any similar type of device (PCIE?)
277  * needs only to implement these functions to be usable with the TTM interface.
278  * The AGP backend implementation lives in drm_agpsupport.c
279  * basically maps these calls to available functions in agpgart.
280  * Each drm device driver gets an
281  * additional function pointer that creates these types,
282  * so that the device can choose the correct aperture.
283  * (Multiple AGP apertures, etc.)
284  * Most device drivers will let this point to the standard AGP implementation.
285  */
286
287 #define DRM_BE_FLAG_NEEDS_FREE     0x00000001
288 #define DRM_BE_FLAG_BOUND_CACHED   0x00000002
289
290 struct drm_ttm_backend;
291 struct drm_ttm_backend_func {
292         int (*needs_ub_cache_adjust) (struct drm_ttm_backend *backend);
293         int (*populate) (struct drm_ttm_backend *backend,
294                          unsigned long num_pages, struct page **pages,
295                          struct page *dummy_read_page);
296         void (*clear) (struct drm_ttm_backend *backend);
297         int (*bind) (struct drm_ttm_backend *backend,
298                      struct drm_bo_mem_reg *bo_mem);
299         int (*unbind) (struct drm_ttm_backend *backend);
300         void (*destroy) (struct drm_ttm_backend *backend);
301 };
302
303
304 struct drm_ttm_backend {
305         struct drm_device *dev;
306         uint32_t flags;
307         struct drm_ttm_backend_func *func;
308 };
309
310 struct drm_ttm {
311         struct page *dummy_read_page;
312         struct page **pages;
313         long first_himem_page;
314         long last_lomem_page;
315         uint32_t page_flags;
316         unsigned long num_pages;
317         atomic_t vma_count;
318         struct drm_device *dev;
319         int destroy;
320         uint32_t mapping_offset;
321         struct drm_ttm_backend *be;
322         unsigned long highest_lomem_entry;
323         unsigned long lowest_himem_entry;
324         enum {
325                 ttm_bound,
326                 ttm_evicted,
327                 ttm_unbound,
328                 ttm_unpopulated,
329         } state;
330
331 };
332
333 extern struct drm_ttm *drm_ttm_create(struct drm_device *dev, unsigned long size,
334                                       uint32_t page_flags,
335                                       struct page *dummy_read_page);
336 extern int drm_ttm_bind(struct drm_ttm *ttm, struct drm_bo_mem_reg *bo_mem);
337 extern void drm_ttm_unbind(struct drm_ttm *ttm);
338 extern void drm_ttm_evict(struct drm_ttm *ttm);
339 extern void drm_ttm_fixup_caching(struct drm_ttm *ttm);
340 extern struct page *drm_ttm_get_page(struct drm_ttm *ttm, int index);
341 extern void drm_ttm_cache_flush(struct page *pages[], unsigned long num_pages);
342 extern int drm_ttm_populate(struct drm_ttm *ttm);
343 extern int drm_ttm_set_user(struct drm_ttm *ttm,
344                             struct task_struct *tsk,
345                             unsigned long start,
346                             unsigned long num_pages);
347
348 /*
349  * Destroy a ttm. The user normally calls drmRmMap or a similar IOCTL to do
350  * this which calls this function iff there are no vmas referencing it anymore.
351  * Otherwise it is called when the last vma exits.
352  */
353
354 extern int drm_ttm_destroy(struct drm_ttm *ttm);
355
356 #define DRM_FLAG_MASKED(_old, _new, _mask) {\
357 (_old) ^= (((_old) ^ (_new)) & (_mask)); \
358 }
359
360 #define DRM_TTM_MASK_FLAGS ((1 << PAGE_SHIFT) - 1)
361 #define DRM_TTM_MASK_PFN (0xFFFFFFFFU - DRM_TTM_MASK_FLAGS)
362
363 /*
364  * Page flags.
365  */
366
367 /*
368  * This ttm should not be cached by the CPU
369  */
370 #define DRM_TTM_PAGE_UNCACHED   (1 << 0)
371 /*
372  * This flat is not used at this time; I don't know what the
373  * intent was
374  */
375 #define DRM_TTM_PAGE_USED       (1 << 1)
376 /*
377  * This flat is not used at this time; I don't know what the
378  * intent was
379  */
380 #define DRM_TTM_PAGE_BOUND      (1 << 2)
381 /*
382  * This flat is not used at this time; I don't know what the
383  * intent was
384  */
385 #define DRM_TTM_PAGE_PRESENT    (1 << 3)
386 /*
387  * The array of page pointers was allocated with vmalloc
388  * instead of drm_calloc.
389  */
390 #define DRM_TTM_PAGEDIR_VMALLOC (1 << 4)
391 /*
392  * This ttm is mapped from user space
393  */
394 #define DRM_TTM_PAGE_USER       (1 << 5)
395 /*
396  * This ttm will be written to by the GPU
397  */
398 #define DRM_TTM_PAGE_WRITE      (1 << 6)
399 /*
400  * This ttm was mapped to the GPU, and so the contents may have
401  * been modified
402  */
403 #define DRM_TTM_PAGE_USER_DIRTY (1 << 7)
404 /*
405  * This flag is not used at this time; I don't know what the
406  * intent was.
407  */
408 #define DRM_TTM_PAGE_USER_DMA   (1 << 8)
409
410 /***************************************************
411  * Buffer objects. (drm_bo.c, drm_bo_move.c)
412  */
413
414 struct drm_bo_mem_reg {
415         struct drm_mm_node *mm_node;
416         unsigned long size;
417         unsigned long num_pages;
418         uint32_t page_alignment;
419         uint32_t mem_type;
420         /*
421          * Current buffer status flags, indicating
422          * where the buffer is located and which
423          * access modes are in effect
424          */
425         uint64_t flags;
426         /**
427          * These are the flags proposed for
428          * a validate operation. If the
429          * validate succeeds, they'll get moved
430          * into the flags field
431          */
432         uint64_t proposed_flags;
433         
434         uint32_t desired_tile_stride;
435         uint32_t hw_tile_stride;
436 };
437
438 enum drm_bo_type {
439         /*
440          * drm_bo_type_device are 'normal' drm allocations,
441          * pages are allocated from within the kernel automatically
442          * and the objects can be mmap'd from the drm device. Each
443          * drm_bo_type_device object has a unique name which can be
444          * used by other processes to share access to the underlying
445          * buffer.
446          */
447         drm_bo_type_device,
448         /*
449          * drm_bo_type_user are buffers of pages that already exist
450          * in the process address space. They are more limited than
451          * drm_bo_type_device buffers in that they must always
452          * remain cached (as we assume the user pages are mapped cached),
453          * and they are not sharable to other processes through DRM
454          * (although, regular shared memory should still work fine).
455          */
456         drm_bo_type_user,
457         /*
458          * drm_bo_type_kernel are buffers that exist solely for use
459          * within the kernel. The pages cannot be mapped into the
460          * process. One obvious use would be for the ring
461          * buffer where user access would not (ideally) be required.
462          */
463         drm_bo_type_kernel,
464 };
465
466 struct drm_buffer_object {
467         struct drm_device *dev;
468         struct drm_user_object base;
469
470         /*
471          * If there is a possibility that the usage variable is zero,
472          * then dev->struct_mutext should be locked before incrementing it.
473          */
474
475         atomic_t usage;
476         unsigned long buffer_start;
477         enum drm_bo_type type;
478         unsigned long offset;
479         atomic_t mapped;
480         struct drm_bo_mem_reg mem;
481
482         struct list_head lru;
483         struct list_head ddestroy;
484
485         uint32_t fence_type;
486         uint32_t fence_class;
487         uint32_t new_fence_type;
488         uint32_t new_fence_class;
489         struct drm_fence_object *fence;
490         uint32_t priv_flags;
491         wait_queue_head_t event_queue;
492         struct mutex mutex;
493         unsigned long num_pages;
494
495         /* For pinned buffers */
496         struct drm_mm_node *pinned_node;
497         uint32_t pinned_mem_type;
498         struct list_head pinned_lru;
499
500         /* For vm */
501         struct drm_ttm *ttm;
502         struct drm_map_list map_list;
503         uint32_t memory_type;
504         unsigned long bus_offset;
505         uint32_t vm_flags;
506         void *iomap;
507
508 #ifdef DRM_ODD_MM_COMPAT
509         /* dev->struct_mutex only protected. */
510         struct list_head vma_list;
511         struct list_head p_mm_list;
512 #endif
513
514 };
515
516 #define _DRM_BO_FLAG_UNFENCED 0x00000001
517 #define _DRM_BO_FLAG_EVICTED  0x00000002
518
519 /*
520  * This flag indicates that a flag called with bo->mutex held has
521  * temporarily released the buffer object mutex, (usually to wait for something).
522  * and thus any post-lock validation needs to be rerun.
523  */
524
525 #define _DRM_BO_FLAG_UNLOCKED 0x00000004
526
527 struct drm_mem_type_manager {
528         int has_type;
529         int use_type;
530         int kern_init_type;
531         struct drm_mm manager;
532         struct list_head lru;
533         struct list_head pinned;
534         uint32_t flags;
535         uint32_t drm_bus_maptype;
536         unsigned long gpu_offset;
537         unsigned long io_offset;
538         unsigned long io_size;
539         void *io_addr;
540         uint64_t size; /* size of managed area for reporting to userspace */
541 };
542
543 struct drm_bo_lock {
544         struct drm_user_object base;
545         wait_queue_head_t queue;
546         atomic_t write_lock_pending;
547         atomic_t readers;
548 };
549
550 #define _DRM_FLAG_MEMTYPE_FIXED     0x00000001  /* Fixed (on-card) PCI memory */
551 #define _DRM_FLAG_MEMTYPE_MAPPABLE  0x00000002  /* Memory mappable */
552 #define _DRM_FLAG_MEMTYPE_CACHED    0x00000004  /* Cached binding */
553 #define _DRM_FLAG_NEEDS_IOREMAP     0x00000008  /* Fixed memory needs ioremap
554                                                    before kernel access. */
555 #define _DRM_FLAG_MEMTYPE_CMA       0x00000010  /* Can't map aperture */
556 #define _DRM_FLAG_MEMTYPE_CSELECT   0x00000020  /* Select caching */
557
558 struct drm_buffer_manager {
559         struct drm_bo_lock bm_lock;
560         struct mutex evict_mutex;
561         int nice_mode;
562         int initialized;
563         struct drm_file *last_to_validate;
564         struct drm_mem_type_manager man[DRM_BO_MEM_TYPES];
565         struct list_head unfenced;
566         struct list_head ddestroy;
567 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
568         struct work_struct wq;
569 #else
570         struct delayed_work wq;
571 #endif
572         uint32_t fence_type;
573         unsigned long cur_pages;
574         atomic_t count;
575         struct page *dummy_read_page;
576 };
577
578 struct drm_bo_driver {
579         const uint32_t *mem_type_prio;
580         const uint32_t *mem_busy_prio;
581         uint32_t num_mem_type_prio;
582         uint32_t num_mem_busy_prio;
583         struct drm_ttm_backend *(*create_ttm_backend_entry)
584          (struct drm_device *dev);
585         int (*fence_type) (struct drm_buffer_object *bo, uint32_t *fclass,
586                            uint32_t *type);
587         int (*invalidate_caches) (struct drm_device *dev, uint64_t flags);
588         int (*init_mem_type) (struct drm_device *dev, uint32_t type,
589                               struct drm_mem_type_manager *man);
590         /*
591          * evict_flags:
592          *
593          * @bo: the buffer object to be evicted
594          *
595          * Return the bo flags for a buffer which is not mapped to the hardware.
596          * These will be placed in proposed_flags so that when the move is
597          * finished, they'll end up in bo->mem.flags
598          */
599         uint64_t(*evict_flags) (struct drm_buffer_object *bo);
600         /*
601          * move:
602          *
603          * @bo: the buffer to move
604          *
605          * @evict: whether this motion is evicting the buffer from
606          * the graphics address space
607          *
608          * @no_wait: whether this should give up and return -EBUSY
609          * if this move would require sleeping
610          *
611          * @new_mem: the new memory region receiving the buffer
612          *
613          * Move a buffer between two memory regions.
614          */
615         int (*move) (struct drm_buffer_object *bo,
616                      int evict, int no_wait, struct drm_bo_mem_reg *new_mem);
617         /*
618          * ttm_cache_flush
619          */
620         void (*ttm_cache_flush)(struct drm_ttm *ttm);
621
622         /*
623          * command_stream_barrier
624          *
625          * @dev: The drm device.
626          *
627          * @bo: The buffer object to validate.
628          *
629          * @new_fence_class: The new fence class for the buffer object.
630          *
631          * @new_fence_type: The new fence type for the buffer object.
632          *
633          * @no_wait: whether this should give up and return -EBUSY
634          * if this operation would require sleeping
635          *
636          * Insert a command stream barrier that makes sure that the
637          * buffer is idle once the commands associated with the
638          * current validation are starting to execute. If an error
639          * condition is returned, or the function pointer is NULL,
640          * the drm core will force buffer idle
641          * during validation.
642          */
643
644         int (*command_stream_barrier) (struct drm_buffer_object *bo,
645                                        uint32_t new_fence_class,
646                                        uint32_t new_fence_type,
647                                        int no_wait);                                   
648 };
649
650 /*
651  * buffer objects (drm_bo.c)
652  */
653 extern int drm_bo_create_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
654 extern int drm_bo_destroy_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
655 extern int drm_bo_map_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
656 extern int drm_bo_unmap_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
657 extern int drm_bo_reference_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
658 extern int drm_bo_set_pin(struct drm_device *dev, struct drm_buffer_object *bo, int pin);
659 extern int drm_bo_unreference_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
660 extern int drm_bo_wait_idle_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
661 extern int drm_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
662 extern int drm_bo_setstatus_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
663 extern int drm_mm_init_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
664 extern int drm_mm_takedown_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
665 extern int drm_mm_lock_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
666 extern int drm_mm_unlock_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
667 extern int drm_mm_info_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
668 extern int drm_bo_version_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
669 extern int drm_bo_driver_finish(struct drm_device *dev);
670 extern int drm_bo_driver_init(struct drm_device *dev);
671 extern int drm_bo_pci_offset(struct drm_device *dev,
672                              struct drm_bo_mem_reg *mem,
673                              unsigned long *bus_base,
674                              unsigned long *bus_offset,
675                              unsigned long *bus_size);
676 extern int drm_mem_reg_is_pci(struct drm_device *dev, struct drm_bo_mem_reg *mem);
677
678 extern void drm_bo_usage_deref_locked(struct drm_buffer_object **bo);
679 extern void drm_bo_usage_deref_unlocked(struct drm_buffer_object **bo);
680 extern void drm_putback_buffer_objects(struct drm_device *dev);
681 extern int drm_fence_buffer_objects(struct drm_device *dev,
682                                     struct list_head *list,
683                                     uint32_t fence_flags,
684                                     struct drm_fence_object *fence,
685                                     struct drm_fence_object **used_fence);
686 extern void drm_bo_add_to_lru(struct drm_buffer_object *bo);
687 extern int drm_buffer_object_create(struct drm_device *dev, unsigned long size,
688                                     enum drm_bo_type type, uint64_t flags,
689                                     uint32_t hint, uint32_t page_alignment,
690                                     unsigned long buffer_start,
691                                     struct drm_buffer_object **bo);
692 extern int drm_bo_wait(struct drm_buffer_object *bo, int lazy, int interruptible,
693                        int no_wait, int check_unfenced);
694 extern int drm_bo_mem_space(struct drm_buffer_object *bo,
695                             struct drm_bo_mem_reg *mem, int no_wait);
696 extern int drm_bo_move_buffer(struct drm_buffer_object *bo,
697                               uint64_t new_mem_flags,
698                               int no_wait, int move_unfenced);
699 extern int drm_bo_clean_mm(struct drm_device *dev, unsigned mem_type, int kern_clean);
700 extern int drm_bo_init_mm(struct drm_device *dev, unsigned type,
701                           unsigned long p_offset, unsigned long p_size,
702                           int kern_init);
703 extern int drm_bo_handle_validate(struct drm_file *file_priv, uint32_t handle,
704                                   uint64_t flags, uint64_t mask, uint32_t hint,
705                                   uint32_t fence_class,
706                                   struct drm_bo_info_rep *rep,
707                                   struct drm_buffer_object **bo_rep);
708 extern struct drm_buffer_object *drm_lookup_buffer_object(struct drm_file *file_priv,
709                                                           uint32_t handle,
710                                                           int check_owner);
711 extern int drm_bo_do_validate(struct drm_buffer_object *bo,
712                               uint64_t flags, uint64_t mask, uint32_t hint,
713                               uint32_t fence_class,
714                               struct drm_bo_info_rep *rep);
715 extern int drm_bo_evict_cached(struct drm_buffer_object *bo);
716 /*
717  * Buffer object memory move- and map helpers.
718  * drm_bo_move.c
719  */
720
721 extern int drm_bo_move_ttm(struct drm_buffer_object *bo,
722                            int evict, int no_wait,
723                            struct drm_bo_mem_reg *new_mem);
724 extern int drm_bo_move_memcpy(struct drm_buffer_object *bo,
725                               int evict,
726                               int no_wait, struct drm_bo_mem_reg *new_mem);
727 extern int drm_bo_move_accel_cleanup(struct drm_buffer_object *bo,
728                                      int evict, int no_wait,
729                                      uint32_t fence_class, uint32_t fence_type,
730                                      uint32_t fence_flags,
731                                      struct drm_bo_mem_reg *new_mem);
732 extern int drm_bo_same_page(unsigned long offset, unsigned long offset2);
733 extern unsigned long drm_bo_offset_end(unsigned long offset,
734                                        unsigned long end);
735
736 struct drm_bo_kmap_obj {
737         void *virtual;
738         struct page *page;
739         enum {
740                 bo_map_iomap,
741                 bo_map_vmap,
742                 bo_map_kmap,
743                 bo_map_premapped,
744         } bo_kmap_type;
745 };
746
747 static inline void *drm_bmo_virtual(struct drm_bo_kmap_obj *map, int *is_iomem)
748 {
749         *is_iomem = (map->bo_kmap_type == bo_map_iomap ||
750                      map->bo_kmap_type == bo_map_premapped);
751         return map->virtual;
752 }
753 extern void drm_bo_kunmap(struct drm_bo_kmap_obj *map);
754 extern int drm_bo_kmap(struct drm_buffer_object *bo, unsigned long start_page,
755                        unsigned long num_pages, struct drm_bo_kmap_obj *map);
756 extern int drm_bo_pfn_prot(struct drm_buffer_object *bo,
757                            unsigned long dst_offset,
758                            unsigned long *pfn,
759                            pgprot_t *prot);
760 extern void drm_bo_fill_rep_arg(struct drm_buffer_object *bo,
761                                 struct drm_bo_info_rep *rep);
762
763
764 /*
765  * drm_regman.c
766  */
767
768 struct drm_reg {
769         struct list_head head;
770         struct drm_fence_object *fence;
771         uint32_t fence_type;
772         uint32_t new_fence_type;
773 };
774
775 struct drm_reg_manager {
776         struct list_head free;
777         struct list_head lru;
778         struct list_head unfenced;
779
780         int (*reg_reusable)(const struct drm_reg *reg, const void *data);
781         void (*reg_destroy)(struct drm_reg *reg);
782 };
783
784 extern int drm_regs_alloc(struct drm_reg_manager *manager,
785                           const void *data,
786                           uint32_t fence_class,
787                           uint32_t fence_type,
788                           int interruptible,
789                           int no_wait,
790                           struct drm_reg **reg);
791
792 extern void drm_regs_fence(struct drm_reg_manager *regs,
793                            struct drm_fence_object *fence);
794
795 extern void drm_regs_free(struct drm_reg_manager *manager);
796 extern void drm_regs_add(struct drm_reg_manager *manager, struct drm_reg *reg);
797 extern void drm_regs_init(struct drm_reg_manager *manager,
798                           int (*reg_reusable)(const struct drm_reg *,
799                                               const void *),
800                           void (*reg_destroy)(struct drm_reg *));
801
802 extern int drm_mem_reg_ioremap(struct drm_device *dev, struct drm_bo_mem_reg * mem,
803                                void **virtual);
804 extern void drm_mem_reg_iounmap(struct drm_device *dev, struct drm_bo_mem_reg * mem,
805                                 void *virtual);
806 /*
807  * drm_bo_lock.c
808  * Simple replacement for the hardware lock on buffer manager init and clean.
809  */
810
811
812 extern void drm_bo_init_lock(struct drm_bo_lock *lock);
813 extern void drm_bo_read_unlock(struct drm_bo_lock *lock);
814 extern int drm_bo_read_lock(struct drm_bo_lock *lock,
815                             int interruptible);
816 extern int drm_bo_write_lock(struct drm_bo_lock *lock,
817                              int interruptible,
818                              struct drm_file *file_priv);
819
820 extern int drm_bo_write_unlock(struct drm_bo_lock *lock,
821                                struct drm_file *file_priv);
822
823 #ifdef CONFIG_DEBUG_MUTEXES
824 #define DRM_ASSERT_LOCKED(_mutex)                                       \
825         BUG_ON(!mutex_is_locked(_mutex) ||                              \
826                ((_mutex)->owner != current_thread_info()))
827 #else
828 #define DRM_ASSERT_LOCKED(_mutex)
829 #endif
830 #endif