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