29f6a1d1c853d77510c733edb42d32304ec21ebc
[linux-2.6-microblaze.git] / include / drm / ttm / ttm_bo_driver.h
1 /**************************************************************************
2  *
3  * Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., 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 Hellstrom <thellstrom-at-vmware-dot-com>
29  */
30 #ifndef _TTM_BO_DRIVER_H_
31 #define _TTM_BO_DRIVER_H_
32
33 #include <drm/drm_mm.h>
34 #include <drm/drm_vma_manager.h>
35 #include <linux/workqueue.h>
36 #include <linux/fs.h>
37 #include <linux/spinlock.h>
38 #include <linux/dma-resv.h>
39
40 #include "ttm_bo_api.h"
41 #include "ttm_memory.h"
42 #include "ttm_module.h"
43 #include "ttm_placement.h"
44 #include "ttm_tt.h"
45
46 /**
47  * struct ttm_bo_driver
48  *
49  * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
50  * @evict_flags: Callback to obtain placement flags when a buffer is evicted.
51  * @move: Callback for a driver to hook in accelerated functions to
52  * move a buffer.
53  * If set to NULL, a potentially slow memcpy() move is used.
54  */
55
56 struct ttm_bo_driver {
57         /**
58          * ttm_tt_create
59          *
60          * @bo: The buffer object to create the ttm for.
61          * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
62          *
63          * Create a struct ttm_tt to back data with system memory pages.
64          * No pages are actually allocated.
65          * Returns:
66          * NULL: Out of memory.
67          */
68         struct ttm_tt *(*ttm_tt_create)(struct ttm_buffer_object *bo,
69                                         uint32_t page_flags);
70
71         /**
72          * ttm_tt_populate
73          *
74          * @ttm: The struct ttm_tt to contain the backing pages.
75          *
76          * Allocate all backing pages
77          * Returns:
78          * -ENOMEM: Out of memory.
79          */
80         int (*ttm_tt_populate)(struct ttm_bo_device *bdev,
81                                struct ttm_tt *ttm,
82                                struct ttm_operation_ctx *ctx);
83
84         /**
85          * ttm_tt_unpopulate
86          *
87          * @ttm: The struct ttm_tt to contain the backing pages.
88          *
89          * Free all backing page
90          */
91         void (*ttm_tt_unpopulate)(struct ttm_bo_device *bdev, struct ttm_tt *ttm);
92
93         /**
94          * ttm_tt_destroy
95          *
96          * @bdev: Pointer to a ttm device
97          * @ttm: Pointer to a struct ttm_tt.
98          *
99          * Destroy the backend. This will be call back from ttm_tt_destroy so
100          * don't call ttm_tt_destroy from the callback or infinite loop.
101          */
102         void (*ttm_tt_destroy)(struct ttm_bo_device *bdev, struct ttm_tt *ttm);
103
104         /**
105          * struct ttm_bo_driver member eviction_valuable
106          *
107          * @bo: the buffer object to be evicted
108          * @place: placement we need room for
109          *
110          * Check with the driver if it is valuable to evict a BO to make room
111          * for a certain placement.
112          */
113         bool (*eviction_valuable)(struct ttm_buffer_object *bo,
114                                   const struct ttm_place *place);
115         /**
116          * struct ttm_bo_driver member evict_flags:
117          *
118          * @bo: the buffer object to be evicted
119          *
120          * Return the bo flags for a buffer which is not mapped to the hardware.
121          * These will be placed in proposed_flags so that when the move is
122          * finished, they'll end up in bo->mem.flags
123          */
124
125         void (*evict_flags)(struct ttm_buffer_object *bo,
126                             struct ttm_placement *placement);
127
128         /**
129          * struct ttm_bo_driver member move:
130          *
131          * @bo: the buffer to move
132          * @evict: whether this motion is evicting the buffer from
133          * the graphics address space
134          * @ctx: context for this move with parameters
135          * @new_mem: the new memory region receiving the buffer
136          *
137          * Move a buffer between two memory regions.
138          */
139         int (*move)(struct ttm_buffer_object *bo, bool evict,
140                     struct ttm_operation_ctx *ctx,
141                     struct ttm_resource *new_mem);
142
143         /**
144          * struct ttm_bo_driver_member verify_access
145          *
146          * @bo: Pointer to a buffer object.
147          * @filp: Pointer to a struct file trying to access the object.
148          *
149          * Called from the map / write / read methods to verify that the
150          * caller is permitted to access the buffer object.
151          * This member may be set to NULL, which will refuse this kind of
152          * access for all buffer objects.
153          * This function should return 0 if access is granted, -EPERM otherwise.
154          */
155         int (*verify_access)(struct ttm_buffer_object *bo,
156                              struct file *filp);
157
158         /**
159          * Hook to notify driver about a resource delete.
160          */
161         void (*delete_mem_notify)(struct ttm_buffer_object *bo);
162
163         /**
164          * notify the driver that we're about to swap out this bo
165          */
166         void (*swap_notify)(struct ttm_buffer_object *bo);
167
168         /**
169          * Driver callback on when mapping io memory (for bo_move_memcpy
170          * for instance). TTM will take care to call io_mem_free whenever
171          * the mapping is not use anymore. io_mem_reserve & io_mem_free
172          * are balanced.
173          */
174         int (*io_mem_reserve)(struct ttm_bo_device *bdev,
175                               struct ttm_resource *mem);
176         void (*io_mem_free)(struct ttm_bo_device *bdev,
177                             struct ttm_resource *mem);
178
179         /**
180          * Return the pfn for a given page_offset inside the BO.
181          *
182          * @bo: the BO to look up the pfn for
183          * @page_offset: the offset to look up
184          */
185         unsigned long (*io_mem_pfn)(struct ttm_buffer_object *bo,
186                                     unsigned long page_offset);
187
188         /**
189          * Read/write memory buffers for ptrace access
190          *
191          * @bo: the BO to access
192          * @offset: the offset from the start of the BO
193          * @buf: pointer to source/destination buffer
194          * @len: number of bytes to copy
195          * @write: whether to read (0) from or write (non-0) to BO
196          *
197          * If successful, this function should return the number of
198          * bytes copied, -EIO otherwise. If the number of bytes
199          * returned is < len, the function may be called again with
200          * the remainder of the buffer to copy.
201          */
202         int (*access_memory)(struct ttm_buffer_object *bo, unsigned long offset,
203                              void *buf, int len, int write);
204
205         /**
206          * struct ttm_bo_driver member del_from_lru_notify
207          *
208          * @bo: the buffer object deleted from lru
209          *
210          * notify driver that a BO was deleted from LRU.
211          */
212         void (*del_from_lru_notify)(struct ttm_buffer_object *bo);
213
214         /**
215          * Notify the driver that we're about to release a BO
216          *
217          * @bo: BO that is about to be released
218          *
219          * Gives the driver a chance to do any cleanup, including
220          * adding fences that may force a delayed delete
221          */
222         void (*release_notify)(struct ttm_buffer_object *bo);
223 };
224
225 /**
226  * struct ttm_bo_global - Buffer object driver global data.
227  *
228  * @dummy_read_page: Pointer to a dummy page used for mapping requests
229  * of unpopulated pages.
230  * @shrink: A shrink callback object used for buffer object swap.
231  * @device_list_mutex: Mutex protecting the device list.
232  * This mutex is held while traversing the device list for pm options.
233  * @lru_lock: Spinlock protecting the bo subsystem lru lists.
234  * @device_list: List of buffer object devices.
235  * @swap_lru: Lru list of buffer objects used for swapping.
236  */
237
238 extern struct ttm_bo_global {
239
240         /**
241          * Constant after init.
242          */
243
244         struct kobject kobj;
245         struct page *dummy_read_page;
246         spinlock_t lru_lock;
247
248         /**
249          * Protected by ttm_global_mutex.
250          */
251         struct list_head device_list;
252
253         /**
254          * Protected by the lru_lock.
255          */
256         struct list_head swap_lru[TTM_MAX_BO_PRIORITY];
257
258         /**
259          * Internal protection.
260          */
261         atomic_t bo_count;
262 } ttm_bo_glob;
263
264
265 #define TTM_NUM_MEM_TYPES 8
266
267 /**
268  * struct ttm_bo_device - Buffer object driver device-specific data.
269  *
270  * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
271  * @man: An array of resource_managers.
272  * @vma_manager: Address space manager (pointer)
273  * lru_lock: Spinlock that protects the buffer+device lru lists and
274  * ddestroy lists.
275  * @dev_mapping: A pointer to the struct address_space representing the
276  * device address space.
277  * @wq: Work queue structure for the delayed delete workqueue.
278  * @no_retry: Don't retry allocation if it fails
279  *
280  */
281
282 struct ttm_bo_device {
283
284         /*
285          * Constant after bo device init / atomic.
286          */
287         struct list_head device_list;
288         struct ttm_bo_driver *driver;
289         /*
290          * access via ttm_manager_type.
291          */
292         struct ttm_resource_manager sysman;
293         struct ttm_resource_manager *man_drv[TTM_NUM_MEM_TYPES];
294         /*
295          * Protected by internal locks.
296          */
297         struct drm_vma_offset_manager *vma_manager;
298
299         /*
300          * Protected by the global:lru lock.
301          */
302         struct list_head ddestroy;
303
304         /*
305          * Protected by load / firstopen / lastclose /unload sync.
306          */
307
308         struct address_space *dev_mapping;
309
310         /*
311          * Internal protection.
312          */
313
314         struct delayed_work wq;
315
316         bool need_dma32;
317
318         bool no_retry;
319 };
320
321 static inline struct ttm_resource_manager *ttm_manager_type(struct ttm_bo_device *bdev,
322                                                             int mem_type)
323 {
324         return bdev->man_drv[mem_type];
325 }
326
327 static inline void ttm_set_driver_manager(struct ttm_bo_device *bdev,
328                                           int type,
329                                           struct ttm_resource_manager *manager)
330 {
331         bdev->man_drv[type] = manager;
332 }
333
334 /**
335  * struct ttm_lru_bulk_move_pos
336  *
337  * @first: first BO in the bulk move range
338  * @last: last BO in the bulk move range
339  *
340  * Positions for a lru bulk move.
341  */
342 struct ttm_lru_bulk_move_pos {
343         struct ttm_buffer_object *first;
344         struct ttm_buffer_object *last;
345 };
346
347 /**
348  * struct ttm_lru_bulk_move
349  *
350  * @tt: first/last lru entry for BOs in the TT domain
351  * @vram: first/last lru entry for BOs in the VRAM domain
352  * @swap: first/last lru entry for BOs on the swap list
353  *
354  * Helper structure for bulk moves on the LRU list.
355  */
356 struct ttm_lru_bulk_move {
357         struct ttm_lru_bulk_move_pos tt[TTM_MAX_BO_PRIORITY];
358         struct ttm_lru_bulk_move_pos vram[TTM_MAX_BO_PRIORITY];
359         struct ttm_lru_bulk_move_pos swap[TTM_MAX_BO_PRIORITY];
360 };
361
362 /*
363  * ttm_bo.c
364  */
365
366 /**
367  * ttm_bo_mem_space
368  *
369  * @bo: Pointer to a struct ttm_buffer_object. the data of which
370  * we want to allocate space for.
371  * @proposed_placement: Proposed new placement for the buffer object.
372  * @mem: A struct ttm_resource.
373  * @interruptible: Sleep interruptible when sliping.
374  * @no_wait_gpu: Return immediately if the GPU is busy.
375  *
376  * Allocate memory space for the buffer object pointed to by @bo, using
377  * the placement flags in @mem, potentially evicting other idle buffer objects.
378  * This function may sleep while waiting for space to become available.
379  * Returns:
380  * -EBUSY: No space available (only if no_wait == 1).
381  * -ENOMEM: Could not allocate memory for the buffer object, either due to
382  * fragmentation or concurrent allocators.
383  * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
384  */
385 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
386                      struct ttm_placement *placement,
387                      struct ttm_resource *mem,
388                      struct ttm_operation_ctx *ctx);
389
390 int ttm_bo_device_release(struct ttm_bo_device *bdev);
391
392 /**
393  * ttm_bo_device_init
394  *
395  * @bdev: A pointer to a struct ttm_bo_device to initialize.
396  * @glob: A pointer to an initialized struct ttm_bo_global.
397  * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
398  * @mapping: The address space to use for this bo.
399  * @vma_manager: A pointer to a vma manager.
400  * @file_page_offset: Offset into the device address space that is available
401  * for buffer data. This ensures compatibility with other users of the
402  * address space.
403  *
404  * Initializes a struct ttm_bo_device:
405  * Returns:
406  * !0: Failure.
407  */
408 int ttm_bo_device_init(struct ttm_bo_device *bdev,
409                        struct ttm_bo_driver *driver,
410                        struct address_space *mapping,
411                        struct drm_vma_offset_manager *vma_manager,
412                        bool need_dma32);
413
414 /**
415  * ttm_bo_unmap_virtual
416  *
417  * @bo: tear down the virtual mappings for this BO
418  */
419 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
420
421 /**
422  * ttm_bo_reserve:
423  *
424  * @bo: A pointer to a struct ttm_buffer_object.
425  * @interruptible: Sleep interruptible if waiting.
426  * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
427  * @ticket: ticket used to acquire the ww_mutex.
428  *
429  * Locks a buffer object for validation. (Or prevents other processes from
430  * locking it for validation), while taking a number of measures to prevent
431  * deadlocks.
432  *
433  * Returns:
434  * -EDEADLK: The reservation may cause a deadlock.
435  * Release all buffer reservations, wait for @bo to become unreserved and
436  * try again.
437  * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
438  * a signal. Release all buffer reservations and return to user-space.
439  * -EBUSY: The function needed to sleep, but @no_wait was true
440  * -EALREADY: Bo already reserved using @ticket. This error code will only
441  * be returned if @use_ticket is set to true.
442  */
443 static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
444                                  bool interruptible, bool no_wait,
445                                  struct ww_acquire_ctx *ticket)
446 {
447         int ret = 0;
448
449         if (no_wait) {
450                 bool success;
451                 if (WARN_ON(ticket))
452                         return -EBUSY;
453
454                 success = dma_resv_trylock(bo->base.resv);
455                 return success ? 0 : -EBUSY;
456         }
457
458         if (interruptible)
459                 ret = dma_resv_lock_interruptible(bo->base.resv, ticket);
460         else
461                 ret = dma_resv_lock(bo->base.resv, ticket);
462         if (ret == -EINTR)
463                 return -ERESTARTSYS;
464         return ret;
465 }
466
467 /**
468  * ttm_bo_reserve_slowpath:
469  * @bo: A pointer to a struct ttm_buffer_object.
470  * @interruptible: Sleep interruptible if waiting.
471  * @sequence: Set (@bo)->sequence to this value after lock
472  *
473  * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
474  * from all our other reservations. Because there are no other reservations
475  * held by us, this function cannot deadlock any more.
476  */
477 static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
478                                           bool interruptible,
479                                           struct ww_acquire_ctx *ticket)
480 {
481         if (interruptible) {
482                 int ret = dma_resv_lock_slow_interruptible(bo->base.resv,
483                                                            ticket);
484                 if (ret == -EINTR)
485                         ret = -ERESTARTSYS;
486                 return ret;
487         }
488         dma_resv_lock_slow(bo->base.resv, ticket);
489         return 0;
490 }
491
492 static inline void ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo)
493 {
494         spin_lock(&ttm_bo_glob.lru_lock);
495         ttm_bo_move_to_lru_tail(bo, NULL);
496         spin_unlock(&ttm_bo_glob.lru_lock);
497 }
498
499 static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo,
500                                      struct ttm_resource *new_mem)
501 {
502         bo->mem = *new_mem;
503         new_mem->mm_node = NULL;
504 }
505
506 /**
507  * ttm_bo_move_null = assign memory for a buffer object.
508  * @bo: The bo to assign the memory to
509  * @new_mem: The memory to be assigned.
510  *
511  * Assign the memory from new_mem to the memory of the buffer object bo.
512  */
513 static inline void ttm_bo_move_null(struct ttm_buffer_object *bo,
514                                     struct ttm_resource *new_mem)
515 {
516         struct ttm_resource *old_mem = &bo->mem;
517
518         WARN_ON(old_mem->mm_node != NULL);
519         ttm_bo_assign_mem(bo, new_mem);
520 }
521
522 /**
523  * ttm_bo_unreserve
524  *
525  * @bo: A pointer to a struct ttm_buffer_object.
526  *
527  * Unreserve a previous reservation of @bo.
528  */
529 static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
530 {
531         ttm_bo_move_to_lru_tail_unlocked(bo);
532         dma_resv_unlock(bo->base.resv);
533 }
534
535 /*
536  * ttm_bo_util.c
537  */
538 int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
539                        struct ttm_resource *mem);
540 void ttm_mem_io_free(struct ttm_bo_device *bdev,
541                      struct ttm_resource *mem);
542
543 /**
544  * ttm_bo_move_memcpy
545  *
546  * @bo: A pointer to a struct ttm_buffer_object.
547  * @interruptible: Sleep interruptible if waiting.
548  * @no_wait_gpu: Return immediately if the GPU is busy.
549  * @new_mem: struct ttm_resource indicating where to move.
550  *
551  * Fallback move function for a mappable buffer object in mappable memory.
552  * The function will, if successful,
553  * free any old aperture space, and set (@new_mem)->mm_node to NULL,
554  * and update the (@bo)->mem placement flags. If unsuccessful, the old
555  * data remains untouched, and it's up to the caller to free the
556  * memory space indicated by @new_mem.
557  * Returns:
558  * !0: Failure.
559  */
560
561 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
562                        struct ttm_operation_ctx *ctx,
563                        struct ttm_resource *new_mem);
564
565 /**
566  * ttm_bo_move_accel_cleanup.
567  *
568  * @bo: A pointer to a struct ttm_buffer_object.
569  * @fence: A fence object that signals when moving is complete.
570  * @evict: This is an evict move. Don't return until the buffer is idle.
571  * @pipeline: evictions are to be pipelined.
572  * @new_mem: struct ttm_resource indicating where to move.
573  *
574  * Accelerated move function to be called when an accelerated move
575  * has been scheduled. The function will create a new temporary buffer object
576  * representing the old placement, and put the sync object on both buffer
577  * objects. After that the newly created buffer object is unref'd to be
578  * destroyed when the move is complete. This will help pipeline
579  * buffer moves.
580  */
581 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
582                               struct dma_fence *fence, bool evict,
583                               bool pipeline,
584                               struct ttm_resource *new_mem);
585
586 /**
587  * ttm_bo_pipeline_gutting.
588  *
589  * @bo: A pointer to a struct ttm_buffer_object.
590  *
591  * Pipelined gutting a BO of its backing store.
592  */
593 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo);
594
595 /**
596  * ttm_io_prot
597  *
598  * bo: ttm buffer object
599  * res: ttm resource object
600  * @tmp: Page protection flag for a normal, cached mapping.
601  *
602  * Utility function that returns the pgprot_t that should be used for
603  * setting up a PTE with the caching model indicated by @c_state.
604  */
605 pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
606                      pgprot_t tmp);
607
608 /**
609  * ttm_bo_tt_bind
610  *
611  * Bind the object tt to a memory resource.
612  */
613 int ttm_bo_tt_bind(struct ttm_buffer_object *bo, struct ttm_resource *mem);
614
615 /**
616  * ttm_bo_tt_destroy.
617  */
618 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
619
620 /**
621  * ttm_range_man_init
622  *
623  * @bdev: ttm device
624  * @type: memory manager type
625  * @use_tt: if the memory manager uses tt
626  * @p_size: size of area to be managed in pages.
627  *
628  * Initialise a generic range manager for the selected memory type.
629  * The range manager is installed for this device in the type slot.
630  */
631 int ttm_range_man_init(struct ttm_bo_device *bdev,
632                        unsigned type, bool use_tt,
633                        unsigned long p_size);
634
635 /**
636  * ttm_range_man_fini
637  *
638  * @bdev: ttm device
639  * @type: memory manager type
640  *
641  * Remove the generic range manager from a slot and tear it down.
642  */
643 int ttm_range_man_fini(struct ttm_bo_device *bdev,
644                        unsigned type);
645
646 #endif