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