OSDN Git Service

Radeon: restructure PLL data
[android-x86/external-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 #define DRM_FENCE_FLAG_EMIT                0x00000001
38 #define DRM_FENCE_FLAG_SHAREABLE           0x00000002
39 /**
40  * On hardware with no interrupt events for operation completion,
41  * indicates that the kernel should sleep while waiting for any blocking
42  * operation to complete rather than spinning.
43  *
44  * Has no effect otherwise.
45  */
46 #define DRM_FENCE_FLAG_WAIT_LAZY           0x00000004
47 #define DRM_FENCE_FLAG_NO_USER             0x00000010
48
49 /* Reserved for driver use */
50 #define DRM_FENCE_MASK_DRIVER              0xFF000000
51
52 #define DRM_FENCE_TYPE_EXE                 0x00000001
53
54 struct drm_fence_arg {
55         unsigned int handle;
56         unsigned int fence_class;
57         unsigned int type;
58         unsigned int flags;
59         unsigned int signaled;
60         unsigned int error;
61         unsigned int sequence;
62         unsigned int pad64;
63         uint64_t expand_pad[2]; /*Future expansion */
64 };
65
66 /* Buffer permissions, referring to how the GPU uses the buffers.
67  * these translate to fence types used for the buffers.
68  * Typically a texture buffer is read, A destination buffer is write and
69  *  a command (batch-) buffer is exe. Can be or-ed together.
70  */
71
72 #define DRM_BO_FLAG_READ        (1ULL << 0)
73 #define DRM_BO_FLAG_WRITE       (1ULL << 1)
74 #define DRM_BO_FLAG_EXE         (1ULL << 2)
75
76 /*
77  * All of the bits related to access mode
78  */
79 #define DRM_BO_MASK_ACCESS      (DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE | DRM_BO_FLAG_EXE)
80 /*
81  * Status flags. Can be read to determine the actual state of a buffer.
82  * Can also be set in the buffer mask before validation.
83  */
84
85 /*
86  * Mask: Never evict this buffer. Not even with force. This type of buffer is only
87  * available to root and must be manually removed before buffer manager shutdown
88  * or lock.
89  * Flags: Acknowledge
90  */
91 #define DRM_BO_FLAG_NO_EVICT    (1ULL << 4)
92
93 /*
94  * Mask: Require that the buffer is placed in mappable memory when validated.
95  *       If not set the buffer may or may not be in mappable memory when validated.
96  * Flags: If set, the buffer is in mappable memory.
97  */
98 #define DRM_BO_FLAG_MAPPABLE    (1ULL << 5)
99
100 /* Mask: The buffer should be shareable with other processes.
101  * Flags: The buffer is shareable with other processes.
102  */
103 #define DRM_BO_FLAG_SHAREABLE   (1ULL << 6)
104
105 /* Mask: If set, place the buffer in cache-coherent memory if available.
106  *       If clear, never place the buffer in cache coherent memory if validated.
107  * Flags: The buffer is currently in cache-coherent memory.
108  */
109 #define DRM_BO_FLAG_CACHED      (1ULL << 7)
110
111 /* Mask: Make sure that every time this buffer is validated,
112  *       it ends up on the same location provided that the memory mask is the same.
113  *       The buffer will also not be evicted when claiming space for
114  *       other buffers. Basically a pinned buffer but it may be thrown out as
115  *       part of buffer manager shutdown or locking.
116  * Flags: Acknowledge.
117  */
118 #define DRM_BO_FLAG_NO_MOVE     (1ULL << 8)
119
120 /* Mask: Make sure the buffer is in cached memory when mapped.  In conjunction
121  * with DRM_BO_FLAG_CACHED it also allows the buffer to be bound into the GART
122  * with unsnooped PTEs instead of snooped, by using chipset-specific cache
123  * flushing at bind time.  A better name might be DRM_BO_FLAG_TT_UNSNOOPED,
124  * as the eviction to local memory (TTM unbind) on map is just a side effect
125  * to prevent aggressive cache prefetch from the GPU disturbing the cache
126  * management that the DRM is doing.
127  *
128  * Flags: Acknowledge.
129  * Buffers allocated with this flag should not be used for suballocators
130  * This type may have issues on CPUs with over-aggressive caching
131  * http://marc.info/?l=linux-kernel&m=102376926732464&w=2
132  */
133 #define DRM_BO_FLAG_CACHED_MAPPED    (1ULL << 19)
134
135
136 /* Mask: Force DRM_BO_FLAG_CACHED flag strictly also if it is set.
137  * Flags: Acknowledge.
138  */
139 #define DRM_BO_FLAG_FORCE_CACHING  (1ULL << 13)
140
141 /*
142  * Mask: Force DRM_BO_FLAG_MAPPABLE flag strictly also if it is clear.
143  * Flags: Acknowledge.
144  */
145 #define DRM_BO_FLAG_FORCE_MAPPABLE (1ULL << 14)
146 #define DRM_BO_FLAG_TILE           (1ULL << 15)
147
148 /*
149  * Buffer has been mapped or touched since creation
150  * for VRAM we don't need to migrate, just fill with 0s for non-dirty
151  */
152 #define DRM_BO_FLAG_CLEAN  (1ULL << 16)
153
154 /*
155  * Memory type flags that can be or'ed together in the mask, but only
156  * one appears in flags.
157  */
158
159 /* System memory */
160 #define DRM_BO_FLAG_MEM_LOCAL  (1ULL << 24)
161 /* Translation table memory */
162 #define DRM_BO_FLAG_MEM_TT     (1ULL << 25)
163 /* Vram memory */
164 #define DRM_BO_FLAG_MEM_VRAM   (1ULL << 26)
165 /* Up to the driver to define. */
166 #define DRM_BO_FLAG_MEM_PRIV0  (1ULL << 27)
167 #define DRM_BO_FLAG_MEM_PRIV1  (1ULL << 28)
168 #define DRM_BO_FLAG_MEM_PRIV2  (1ULL << 29)
169 #define DRM_BO_FLAG_MEM_PRIV3  (1ULL << 30)
170 #define DRM_BO_FLAG_MEM_PRIV4  (1ULL << 31)
171 /* We can add more of these now with a 64-bit flag type */
172
173 /*
174  * This is a mask covering all of the memory type flags; easier to just
175  * use a single constant than a bunch of | values. It covers
176  * DRM_BO_FLAG_MEM_LOCAL through DRM_BO_FLAG_MEM_PRIV4
177  */
178 #define DRM_BO_MASK_MEM         0x00000000FF000000ULL
179 /*
180  * This adds all of the CPU-mapping options in with the memory
181  * type to label all bits which change how the page gets mapped
182  */
183 #define DRM_BO_MASK_MEMTYPE     (DRM_BO_MASK_MEM | \
184                                  DRM_BO_FLAG_CACHED_MAPPED | \
185                                  DRM_BO_FLAG_CACHED | \
186                                  DRM_BO_FLAG_MAPPABLE)
187                                  
188 /* Driver-private flags */
189 #define DRM_BO_MASK_DRIVER      0xFFFF000000000000ULL
190
191 /*
192  * Don't block on validate and map. Instead, return EBUSY.
193  */
194 #define DRM_BO_HINT_DONT_BLOCK  0x00000002
195 /*
196  * Don't place this buffer on the unfenced list. This means
197  * that the buffer will not end up having a fence associated
198  * with it as a result of this operation
199  */
200 #define DRM_BO_HINT_DONT_FENCE  0x00000004
201 /**
202  * On hardware with no interrupt events for operation completion,
203  * indicates that the kernel should sleep while waiting for any blocking
204  * operation to complete rather than spinning.
205  *
206  * Has no effect otherwise.
207  */
208 #define DRM_BO_HINT_WAIT_LAZY   0x00000008
209 /*
210  * The client has compute relocations refering to this buffer using the
211  * offset in the presumed_offset field. If that offset ends up matching
212  * where this buffer lands, the kernel is free to skip executing those
213  * relocations
214  */
215 #define DRM_BO_HINT_PRESUMED_OFFSET 0x00000010
216
217 #define DRM_BO_MEM_LOCAL 0
218 #define DRM_BO_MEM_TT 1
219 #define DRM_BO_MEM_VRAM 2
220 #define DRM_BO_MEM_PRIV0 3
221 #define DRM_BO_MEM_PRIV1 4
222 #define DRM_BO_MEM_PRIV2 5
223 #define DRM_BO_MEM_PRIV3 6
224 #define DRM_BO_MEM_PRIV4 7
225
226 #define DRM_BO_MEM_TYPES 8 /* For now. */
227
228 #define DRM_BO_LOCK_UNLOCK_BM       (1 << 0)
229 #define DRM_BO_LOCK_IGNORE_NO_EVICT (1 << 1)
230
231
232 /***************************************************
233  * Fence objects. (drm_fence.c)
234  */
235
236 struct drm_fence_object {
237         struct drm_device *dev;
238         atomic_t usage;
239
240         /*
241          * The below three fields are protected by the fence manager spinlock.
242          */
243
244         struct list_head ring;
245         int fence_class;
246         uint32_t native_types;
247         uint32_t type;
248         uint32_t signaled_types;
249         uint32_t sequence;
250         uint32_t waiting_types;
251         uint32_t error;
252 };
253
254 #define _DRM_FENCE_CLASSES 8
255
256 struct drm_fence_class_manager {
257         struct list_head ring;
258         uint32_t pending_flush;
259         uint32_t waiting_types;
260         wait_queue_head_t fence_queue;
261         uint32_t highest_waiting_sequence;
262         uint32_t latest_queued_sequence;
263 };
264
265 struct drm_fence_manager {
266         int initialized;
267         rwlock_t lock;
268         struct drm_fence_class_manager fence_class[_DRM_FENCE_CLASSES];
269         uint32_t num_classes;
270         atomic_t count;
271 };
272
273 struct drm_fence_driver {
274         unsigned long *waiting_jiffies;
275         uint32_t num_classes;
276         uint32_t wrap_diff;
277         uint32_t flush_diff;
278         uint32_t sequence_mask;
279
280         /*
281          * Driver implemented functions:
282          * has_irq() : 1 if the hardware can update the indicated type_flags using an
283          * irq handler. 0 if polling is required.
284          *
285          * emit() : Emit a sequence number to the command stream.
286          * Return the sequence number.
287          *
288          * flush() : Make sure the flags indicated in fc->pending_flush will eventually
289          * signal for fc->highest_received_sequence and all preceding sequences.
290          * Acknowledge by clearing the flags fc->pending_flush.
291          *
292          * poll() : Call drm_fence_handler with any new information.
293          *
294          * needed_flush() : Given the current state of the fence->type flags and previusly 
295          * executed or queued flushes, return the type_flags that need flushing.
296          *
297          * wait(): Wait for the "mask" flags to signal on a given fence, performing
298          * whatever's necessary to make this happen.
299          */
300
301         int (*has_irq) (struct drm_device *dev, uint32_t fence_class,
302                         uint32_t flags);
303         int (*emit) (struct drm_device *dev, uint32_t fence_class,
304                      uint32_t flags, uint32_t *breadcrumb,
305                      uint32_t *native_type);
306         void (*flush) (struct drm_device *dev, uint32_t fence_class);
307         void (*poll) (struct drm_device *dev, uint32_t fence_class,
308                 uint32_t types);
309         uint32_t (*needed_flush) (struct drm_fence_object *fence);
310         int (*wait) (struct drm_fence_object *fence, int lazy,
311                      int interruptible, uint32_t mask);
312 };
313
314 extern int drm_fence_wait_polling(struct drm_fence_object *fence, int lazy,
315                                   int interruptible, uint32_t mask,
316                                   unsigned long end_jiffies);
317 extern void drm_fence_handler(struct drm_device *dev, uint32_t fence_class,
318                               uint32_t sequence, uint32_t type,
319                               uint32_t error);
320 extern void drm_fence_manager_init(struct drm_device *dev);
321 extern void drm_fence_manager_takedown(struct drm_device *dev);
322 extern void drm_fence_flush_old(struct drm_device *dev, uint32_t fence_class,
323                                 uint32_t sequence);
324 extern int drm_fence_object_flush(struct drm_fence_object *fence,
325                                   uint32_t type);
326 extern int drm_fence_object_signaled(struct drm_fence_object *fence,
327                                      uint32_t type);
328 extern void drm_fence_usage_deref_locked(struct drm_fence_object **fence);
329 extern void drm_fence_usage_deref_unlocked(struct drm_fence_object **fence);
330 extern struct drm_fence_object *drm_fence_reference_locked(struct drm_fence_object *src);
331 extern void drm_fence_reference_unlocked(struct drm_fence_object **dst,
332                                          struct drm_fence_object *src);
333 extern int drm_fence_object_wait(struct drm_fence_object *fence,
334                                  int lazy, int ignore_signals, uint32_t mask);
335 extern int drm_fence_object_create(struct drm_device *dev, uint32_t type,
336                                    uint32_t fence_flags, uint32_t fence_class,
337                                    struct drm_fence_object **c_fence);
338 extern int drm_fence_object_emit(struct drm_fence_object *fence,
339                                  uint32_t fence_flags, uint32_t class,
340                                  uint32_t type);
341 extern void drm_fence_fill_arg(struct drm_fence_object *fence,
342                                struct drm_fence_arg *arg);
343
344 extern int drm_fence_add_user_object(struct drm_file *priv,
345                                      struct drm_fence_object *fence,
346                                      int shareable);
347
348 extern int drm_fence_create_ioctl(struct drm_device *dev, void *data,
349                                   struct drm_file *file_priv);
350 extern int drm_fence_destroy_ioctl(struct drm_device *dev, void *data,
351                                    struct drm_file *file_priv);
352 extern int drm_fence_reference_ioctl(struct drm_device *dev, void *data,
353                                      struct drm_file *file_priv);
354 extern int drm_fence_unreference_ioctl(struct drm_device *dev, void *data,
355                                        struct drm_file *file_priv);
356 extern int drm_fence_signaled_ioctl(struct drm_device *dev, void *data,
357                                     struct drm_file *file_priv);
358 extern int drm_fence_flush_ioctl(struct drm_device *dev, void *data,
359                                  struct drm_file *file_priv);
360 extern int drm_fence_wait_ioctl(struct drm_device *dev, void *data,
361                                 struct drm_file *file_priv);
362 extern int drm_fence_emit_ioctl(struct drm_device *dev, void *data,
363                                 struct drm_file *file_priv);
364 extern int drm_fence_buffers_ioctl(struct drm_device *dev, void *data,
365                                    struct drm_file *file_priv);
366 /**************************************************
367  *TTMs
368  */
369
370 /*
371  * The ttm backend GTT interface. (In our case AGP).
372  * Any similar type of device (PCIE?)
373  * needs only to implement these functions to be usable with the TTM interface.
374  * The AGP backend implementation lives in drm_agpsupport.c
375  * basically maps these calls to available functions in agpgart.
376  * Each drm device driver gets an
377  * additional function pointer that creates these types,
378  * so that the device can choose the correct aperture.
379  * (Multiple AGP apertures, etc.)
380  * Most device drivers will let this point to the standard AGP implementation.
381  */
382
383 #define DRM_BE_FLAG_NEEDS_FREE     0x00000001
384 #define DRM_BE_FLAG_BOUND_CACHED   0x00000002
385
386 struct drm_ttm_backend;
387 struct drm_ttm_backend_func {
388         int (*needs_ub_cache_adjust) (struct drm_ttm_backend *backend);
389         int (*populate) (struct drm_ttm_backend *backend,
390                          unsigned long num_pages, struct page **pages,
391                          struct page *dummy_read_page);
392         void (*clear) (struct drm_ttm_backend *backend);
393         int (*bind) (struct drm_ttm_backend *backend,
394                      struct drm_bo_mem_reg *bo_mem);
395         int (*unbind) (struct drm_ttm_backend *backend);
396         void (*destroy) (struct drm_ttm_backend *backend);
397 };
398
399 /**
400  * This structure associates a set of flags and methods with a drm_ttm
401  * object, and will also be subclassed by the particular backend.
402  *
403  * \sa #drm_agp_ttm_backend
404  */
405 struct drm_ttm_backend {
406         struct drm_device *dev;
407         uint32_t flags;
408         struct drm_ttm_backend_func *func;
409 };
410
411 struct drm_ttm {
412         struct page *dummy_read_page;
413         struct page **pages;
414         long first_himem_page;
415         long last_lomem_page;
416         uint32_t page_flags;
417         unsigned long num_pages;
418         atomic_t vma_count;
419         struct drm_device *dev;
420         int destroy;
421         uint32_t mapping_offset;
422         struct drm_ttm_backend *be;
423         unsigned long highest_lomem_entry;
424         unsigned long lowest_himem_entry;
425         enum {
426                 ttm_bound,
427                 ttm_evicted,
428                 ttm_unbound,
429                 ttm_unpopulated,
430         } state;
431
432 };
433
434 extern struct drm_ttm *drm_ttm_create(struct drm_device *dev, unsigned long size,
435                                       uint32_t page_flags,
436                                       struct page *dummy_read_page);
437 extern int drm_ttm_bind(struct drm_ttm *ttm, struct drm_bo_mem_reg *bo_mem);
438 extern void drm_ttm_unbind(struct drm_ttm *ttm);
439 extern void drm_ttm_evict(struct drm_ttm *ttm);
440 extern void drm_ttm_fixup_caching(struct drm_ttm *ttm);
441 extern struct page *drm_ttm_get_page(struct drm_ttm *ttm, int index);
442 extern void drm_ttm_cache_flush(struct page *pages[], unsigned long num_pages);
443 extern int drm_ttm_populate(struct drm_ttm *ttm);
444 extern int drm_ttm_set_user(struct drm_ttm *ttm,
445                             struct task_struct *tsk,
446                             unsigned long start,
447                             unsigned long num_pages);
448
449 /*
450  * Destroy a ttm. The user normally calls drmRmMap or a similar IOCTL to do
451  * this which calls this function iff there are no vmas referencing it anymore.
452  * Otherwise it is called when the last vma exits.
453  */
454
455 extern int drm_ttm_destroy(struct drm_ttm *ttm);
456
457 #define DRM_FLAG_MASKED(_old, _new, _mask) {\
458 (_old) ^= (((_old) ^ (_new)) & (_mask)); \
459 }
460
461 #define DRM_TTM_MASK_FLAGS ((1 << PAGE_SHIFT) - 1)
462 #define DRM_TTM_MASK_PFN (0xFFFFFFFFU - DRM_TTM_MASK_FLAGS)
463
464 /*
465  * Page flags.
466  */
467
468 /*
469  * This ttm should not be cached by the CPU
470  */
471 #define DRM_TTM_PAGE_UNCACHED   (1 << 0)
472 /*
473  * This flat is not used at this time; I don't know what the
474  * intent was
475  */
476 #define DRM_TTM_PAGE_USED       (1 << 1)
477 /*
478  * This flat is not used at this time; I don't know what the
479  * intent was
480  */
481 #define DRM_TTM_PAGE_BOUND      (1 << 2)
482 /*
483  * This flat is not used at this time; I don't know what the
484  * intent was
485  */
486 #define DRM_TTM_PAGE_PRESENT    (1 << 3)
487 /*
488  * The array of page pointers was allocated with vmalloc
489  * instead of drm_calloc.
490  */
491 #define DRM_TTM_PAGEDIR_VMALLOC (1 << 4)
492 /*
493  * This ttm is mapped from user space
494  */
495 #define DRM_TTM_PAGE_USER       (1 << 5)
496 /*
497  * This ttm will be written to by the GPU
498  */
499 #define DRM_TTM_PAGE_WRITE      (1 << 6)
500 /*
501  * This ttm was mapped to the GPU, and so the contents may have
502  * been modified
503  */
504 #define DRM_TTM_PAGE_USER_DIRTY (1 << 7)
505 /*
506  * This flag is not used at this time; I don't know what the
507  * intent was.
508  */
509 #define DRM_TTM_PAGE_USER_DMA   (1 << 8)
510
511 /***************************************************
512  * Buffer objects. (drm_bo.c, drm_bo_move.c)
513  */
514
515 struct drm_bo_mem_reg {
516         struct drm_mm_node *mm_node;
517         unsigned long size;
518         unsigned long num_pages;
519         uint32_t page_alignment;
520         uint32_t mem_type;
521         /*
522          * Current buffer status flags, indicating
523          * where the buffer is located and which
524          * access modes are in effect
525          */
526         uint64_t flags;
527         /**
528          * These are the flags proposed for
529          * a validate operation. If the
530          * validate succeeds, they'll get moved
531          * into the flags field
532          */
533         uint64_t proposed_flags;
534         
535         uint32_t desired_tile_stride;
536         uint32_t hw_tile_stride;
537 };
538
539 enum drm_bo_type {
540         /*
541          * drm_bo_type_device are 'normal' drm allocations,
542          * pages are allocated from within the kernel automatically
543          * and the objects can be mmap'd from the drm device. Each
544          * drm_bo_type_device object has a unique name which can be
545          * used by other processes to share access to the underlying
546          * buffer.
547          */
548         drm_bo_type_device,
549         /*
550          * drm_bo_type_user are buffers of pages that already exist
551          * in the process address space. They are more limited than
552          * drm_bo_type_device buffers in that they must always
553          * remain cached (as we assume the user pages are mapped cached),
554          * and they are not sharable to other processes through DRM
555          * (although, regular shared memory should still work fine).
556          */
557         drm_bo_type_user,
558         /*
559          * drm_bo_type_kernel are buffers that exist solely for use
560          * within the kernel. The pages cannot be mapped into the
561          * process. One obvious use would be for the ring
562          * buffer where user access would not (ideally) be required.
563          */
564         drm_bo_type_kernel,
565 };
566
567 struct drm_buffer_object {
568         struct drm_device *dev;
569
570         /*
571          * If there is a possibility that the usage variable is zero,
572          * then dev->struct_mutext should be locked before incrementing it.
573          */
574
575         atomic_t usage;
576         unsigned long buffer_start;
577         enum drm_bo_type type;
578         unsigned long offset;
579         atomic_t mapped;
580         struct drm_bo_mem_reg mem;
581
582         struct list_head lru;
583         struct list_head ddestroy;
584
585         uint32_t fence_type;
586         uint32_t fence_class;
587         uint32_t new_fence_type;
588         uint32_t new_fence_class;
589         struct drm_fence_object *fence;
590         uint32_t priv_flags;
591         wait_queue_head_t event_queue;
592         struct mutex mutex;
593         unsigned long num_pages;
594
595         /* For pinned buffers */
596         struct drm_mm_node *pinned_node;
597         uint32_t pinned_mem_type;
598         struct list_head pinned_lru;
599
600         /* For vm */
601         struct drm_ttm *ttm;
602         struct drm_map_list map_list;
603         uint32_t memory_type;
604         unsigned long bus_offset;
605         uint32_t vm_flags;
606         void *iomap;
607
608 #ifdef DRM_ODD_MM_COMPAT
609         /* dev->struct_mutex only protected. */
610         struct list_head vma_list;
611         struct list_head p_mm_list;
612 #endif
613
614 };
615
616 #define _DRM_BO_FLAG_UNFENCED 0x00000001
617 #define _DRM_BO_FLAG_EVICTED  0x00000002
618
619 /*
620  * This flag indicates that a flag called with bo->mutex held has
621  * temporarily released the buffer object mutex, (usually to wait for something).
622  * and thus any post-lock validation needs to be rerun.
623  */
624
625 #define _DRM_BO_FLAG_UNLOCKED 0x00000004
626
627 struct drm_mem_type_manager {
628         int has_type;
629         int use_type;
630         int kern_init_type;
631         struct drm_mm manager;
632         struct list_head lru;
633         struct list_head pinned;
634         uint32_t flags;
635         uint32_t drm_bus_maptype;
636         unsigned long gpu_offset;
637         unsigned long io_offset;
638         unsigned long io_size;
639         void *io_addr;
640         uint64_t size; /* size of managed area for reporting to userspace */
641 };
642
643 struct drm_bo_lock {
644   //    struct drm_user_object base;
645         wait_queue_head_t queue;
646         atomic_t write_lock_pending;
647         atomic_t readers;
648 };
649
650 #define _DRM_FLAG_MEMTYPE_FIXED     0x00000001  /* Fixed (on-card) PCI memory */
651 #define _DRM_FLAG_MEMTYPE_MAPPABLE  0x00000002  /* Memory mappable */
652 #define _DRM_FLAG_MEMTYPE_CACHED    0x00000004  /* Cached binding */
653 #define _DRM_FLAG_NEEDS_IOREMAP     0x00000008  /* Fixed memory needs ioremap
654                                                    before kernel access. */
655 #define _DRM_FLAG_MEMTYPE_CMA       0x00000010  /* Can't map aperture */
656 #define _DRM_FLAG_MEMTYPE_CSELECT   0x00000020  /* Select caching */
657
658 struct drm_buffer_manager {
659         struct drm_bo_lock bm_lock;
660         struct mutex evict_mutex;
661         int nice_mode;
662         int initialized;
663         struct drm_file *last_to_validate;
664         struct drm_mem_type_manager man[DRM_BO_MEM_TYPES];
665         struct list_head unfenced;
666         struct list_head ddestroy;
667 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
668         struct work_struct wq;
669 #else
670         struct delayed_work wq;
671 #endif
672         uint32_t fence_type;
673         unsigned long cur_pages;
674         atomic_t count;
675         struct page *dummy_read_page;
676 };
677
678 struct drm_bo_driver {
679         const uint32_t *mem_type_prio;
680         const uint32_t *mem_busy_prio;
681         uint32_t num_mem_type_prio;
682         uint32_t num_mem_busy_prio;
683         struct drm_ttm_backend *(*create_ttm_backend_entry)
684          (struct drm_device *dev);
685         int (*fence_type) (struct drm_buffer_object *bo, uint32_t *fclass,
686                            uint32_t *type);
687         int (*invalidate_caches) (struct drm_device *dev, uint64_t flags);
688         int (*init_mem_type) (struct drm_device *dev, uint32_t type,
689                               struct drm_mem_type_manager *man);
690         /*
691          * evict_flags:
692          *
693          * @bo: the buffer object to be evicted
694          *
695          * Return the bo flags for a buffer which is not mapped to the hardware.
696          * These will be placed in proposed_flags so that when the move is
697          * finished, they'll end up in bo->mem.flags
698          */
699         uint64_t(*evict_flags) (struct drm_buffer_object *bo);
700         /*
701          * move:
702          *
703          * @bo: the buffer to move
704          *
705          * @evict: whether this motion is evicting the buffer from
706          * the graphics address space
707          *
708          * @no_wait: whether this should give up and return -EBUSY
709          * if this move would require sleeping
710          *
711          * @new_mem: the new memory region receiving the buffer
712          *
713          * Move a buffer between two memory regions.
714          */
715         int (*move) (struct drm_buffer_object *bo,
716                      int evict, int no_wait, struct drm_bo_mem_reg *new_mem);
717         /*
718          * ttm_cache_flush
719          */
720         void (*ttm_cache_flush)(struct drm_ttm *ttm);
721
722         /*
723          * command_stream_barrier
724          *
725          * @dev: The drm device.
726          *
727          * @bo: The buffer object to validate.
728          *
729          * @new_fence_class: The new fence class for the buffer object.
730          *
731          * @new_fence_type: The new fence type for the buffer object.
732          *
733          * @no_wait: whether this should give up and return -EBUSY
734          * if this operation would require sleeping
735          *
736          * Insert a command stream barrier that makes sure that the
737          * buffer is idle once the commands associated with the
738          * current validation are starting to execute. If an error
739          * condition is returned, or the function pointer is NULL,
740          * the drm core will force buffer idle
741          * during validation.
742          */
743
744         int (*command_stream_barrier) (struct drm_buffer_object *bo,
745                                        uint32_t new_fence_class,
746                                        uint32_t new_fence_type,
747                                        int no_wait);                                   
748 };
749
750 /*
751  * buffer objects (drm_bo.c)
752  */
753 int drm_bo_do_validate(struct drm_buffer_object *bo,
754                        uint64_t flags, uint64_t mask, uint32_t hint,
755                        uint32_t fence_class);
756 extern int drm_bo_set_pin(struct drm_device *dev, struct drm_buffer_object *bo, int pin);
757 extern int drm_bo_driver_finish(struct drm_device *dev);
758 extern int drm_bo_driver_init(struct drm_device *dev);
759 extern int drm_bo_pci_offset(struct drm_device *dev,
760                              struct drm_bo_mem_reg *mem,
761                              unsigned long *bus_base,
762                              unsigned long *bus_offset,
763                              unsigned long *bus_size);
764 extern int drm_mem_reg_is_pci(struct drm_device *dev, struct drm_bo_mem_reg *mem);
765
766 extern int drm_bo_add_user_object(struct drm_file *file_priv,
767                                   struct drm_buffer_object *bo, int shareable);
768 extern void drm_bo_usage_deref_locked(struct drm_buffer_object **bo);
769 extern void drm_bo_usage_deref_unlocked(struct drm_buffer_object **bo);
770 extern void drm_putback_buffer_objects(struct drm_device *dev);
771 extern int drm_fence_buffer_objects(struct drm_device *dev,
772                                     struct list_head *list,
773                                     uint32_t fence_flags,
774                                     struct drm_fence_object *fence,
775                                     struct drm_fence_object **used_fence);
776 extern void drm_bo_add_to_lru(struct drm_buffer_object *bo);
777 extern int drm_buffer_object_create(struct drm_device *dev, unsigned long size,
778                                     enum drm_bo_type type, uint64_t flags,
779                                     uint32_t hint, uint32_t page_alignment,
780                                     unsigned long buffer_start,
781                                     struct drm_buffer_object **bo);
782 extern int drm_bo_wait(struct drm_buffer_object *bo, int lazy, int interruptible,
783                        int no_wait, int check_unfenced);
784 extern int drm_bo_mem_space(struct drm_buffer_object *bo,
785                             struct drm_bo_mem_reg *mem, int no_wait);
786 extern int drm_bo_move_buffer(struct drm_buffer_object *bo,
787                               uint64_t new_mem_flags,
788                               int no_wait, int move_unfenced);
789 extern int drm_bo_clean_mm(struct drm_device *dev, unsigned mem_type, int kern_clean);
790 extern int drm_bo_init_mm(struct drm_device *dev, unsigned type,
791                           unsigned long p_offset, unsigned long p_size,
792                           int kern_init);
793 extern struct drm_buffer_object *drm_lookup_buffer_object(struct drm_file *file_priv,
794                                                           uint32_t handle,
795                                                           int check_owner);
796 extern int drm_bo_evict_cached(struct drm_buffer_object *bo);
797
798 extern void drm_bo_takedown_vm_locked(struct drm_buffer_object *bo);
799 extern void drm_bo_evict_mm(struct drm_device *dev, int mem_type, int no_wait);
800 /*
801  * Buffer object memory move- and map helpers.
802  * drm_bo_move.c
803  */
804
805 extern int drm_bo_add_ttm(struct drm_buffer_object *bo);
806 extern int drm_bo_move_ttm(struct drm_buffer_object *bo,
807                            int evict, int no_wait,
808                            struct drm_bo_mem_reg *new_mem);
809 extern int drm_bo_move_memcpy(struct drm_buffer_object *bo,
810                               int evict,
811                               int no_wait, struct drm_bo_mem_reg *new_mem);
812 extern int drm_bo_move_zero(struct drm_buffer_object *bo,
813                             int evict, int no_wait, struct drm_bo_mem_reg *new_mem);
814 extern int drm_bo_move_accel_cleanup(struct drm_buffer_object *bo,
815                                      int evict, int no_wait,
816                                      uint32_t fence_class, uint32_t fence_type,
817                                      uint32_t fence_flags,
818                                      struct drm_bo_mem_reg *new_mem);
819 extern int drm_bo_same_page(unsigned long offset, unsigned long offset2);
820 extern unsigned long drm_bo_offset_end(unsigned long offset,
821                                        unsigned long end);
822
823 struct drm_bo_kmap_obj {
824         void *virtual;
825         struct page *page;
826         enum {
827                 bo_map_iomap,
828                 bo_map_vmap,
829                 bo_map_kmap,
830                 bo_map_premapped,
831         } bo_kmap_type;
832 };
833
834 static inline void *drm_bmo_virtual(struct drm_bo_kmap_obj *map, int *is_iomem)
835 {
836         *is_iomem = (map->bo_kmap_type == bo_map_iomap ||
837                      map->bo_kmap_type == bo_map_premapped);
838         return map->virtual;
839 }
840 extern void drm_bo_kunmap(struct drm_bo_kmap_obj *map);
841 extern int drm_bo_kmap(struct drm_buffer_object *bo, unsigned long start_page,
842                        unsigned long num_pages, struct drm_bo_kmap_obj *map);
843 extern int drm_bo_pfn_prot(struct drm_buffer_object *bo,
844                            unsigned long dst_offset,
845                            unsigned long *pfn,
846                            pgprot_t *prot);
847
848
849 /*
850  * drm_regman.c
851  */
852
853 struct drm_reg {
854         struct list_head head;
855         struct drm_fence_object *fence;
856         uint32_t fence_type;
857         uint32_t new_fence_type;
858 };
859
860 struct drm_reg_manager {
861         struct list_head free;
862         struct list_head lru;
863         struct list_head unfenced;
864
865         int (*reg_reusable)(const struct drm_reg *reg, const void *data);
866         void (*reg_destroy)(struct drm_reg *reg);
867 };
868
869 extern int drm_regs_alloc(struct drm_reg_manager *manager,
870                           const void *data,
871                           uint32_t fence_class,
872                           uint32_t fence_type,
873                           int interruptible,
874                           int no_wait,
875                           struct drm_reg **reg);
876
877 extern void drm_regs_fence(struct drm_reg_manager *regs,
878                            struct drm_fence_object *fence);
879
880 extern void drm_regs_free(struct drm_reg_manager *manager);
881 extern void drm_regs_add(struct drm_reg_manager *manager, struct drm_reg *reg);
882 extern void drm_regs_init(struct drm_reg_manager *manager,
883                           int (*reg_reusable)(const struct drm_reg *,
884                                               const void *),
885                           void (*reg_destroy)(struct drm_reg *));
886
887 extern int drm_mem_reg_ioremap(struct drm_device *dev, struct drm_bo_mem_reg * mem,
888                                void **virtual);
889 extern void drm_mem_reg_iounmap(struct drm_device *dev, struct drm_bo_mem_reg * mem,
890                                 void *virtual);
891 #ifdef CONFIG_DEBUG_MUTEXES
892 #define DRM_ASSERT_LOCKED(_mutex)                                       \
893         BUG_ON(!mutex_is_locked(_mutex) ||                              \
894                ((_mutex)->owner != current_thread_info()))
895 #else
896 #define DRM_ASSERT_LOCKED(_mutex)
897 #endif
898 #endif