ttm_bo_api.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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_API_H_
  31. #define _TTM_BO_API_H_
  32. #include <drm/drm_hashtab.h>
  33. #include <drm/drm_vma_manager.h>
  34. #include <linux/kref.h>
  35. #include <linux/list.h>
  36. #include <linux/wait.h>
  37. #include <linux/mutex.h>
  38. #include <linux/mm.h>
  39. #include <linux/bitmap.h>
  40. #include <linux/reservation.h>
  41. struct ttm_bo_global;
  42. struct ttm_bo_device;
  43. struct drm_mm_node;
  44. struct ttm_placement;
  45. struct ttm_place;
  46. /**
  47. * struct ttm_bus_placement
  48. *
  49. * @addr: mapped virtual address
  50. * @base: bus base address
  51. * @is_iomem: is this io memory ?
  52. * @size: size in byte
  53. * @offset: offset from the base address
  54. * @io_reserved_vm: The VM system has a refcount in @io_reserved_count
  55. * @io_reserved_count: Refcounting the numbers of callers to ttm_mem_io_reserve
  56. *
  57. * Structure indicating the bus placement of an object.
  58. */
  59. struct ttm_bus_placement {
  60. void *addr;
  61. phys_addr_t base;
  62. unsigned long size;
  63. unsigned long offset;
  64. bool is_iomem;
  65. bool io_reserved_vm;
  66. uint64_t io_reserved_count;
  67. };
  68. /**
  69. * struct ttm_mem_reg
  70. *
  71. * @mm_node: Memory manager node.
  72. * @size: Requested size of memory region.
  73. * @num_pages: Actual size of memory region in pages.
  74. * @page_alignment: Page alignment.
  75. * @placement: Placement flags.
  76. * @bus: Placement on io bus accessible to the CPU
  77. *
  78. * Structure indicating the placement and space resources used by a
  79. * buffer object.
  80. */
  81. struct ttm_mem_reg {
  82. void *mm_node;
  83. unsigned long start;
  84. unsigned long size;
  85. unsigned long num_pages;
  86. uint32_t page_alignment;
  87. uint32_t mem_type;
  88. uint32_t placement;
  89. struct ttm_bus_placement bus;
  90. };
  91. /**
  92. * enum ttm_bo_type
  93. *
  94. * @ttm_bo_type_device: These are 'normal' buffers that can
  95. * be mmapped by user space. Each of these bos occupy a slot in the
  96. * device address space, that can be used for normal vm operations.
  97. *
  98. * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
  99. * but they cannot be accessed from user-space. For kernel-only use.
  100. *
  101. * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
  102. * driver.
  103. */
  104. enum ttm_bo_type {
  105. ttm_bo_type_device,
  106. ttm_bo_type_kernel,
  107. ttm_bo_type_sg
  108. };
  109. struct ttm_tt;
  110. /**
  111. * struct ttm_buffer_object
  112. *
  113. * @bdev: Pointer to the buffer object device structure.
  114. * @type: The bo type.
  115. * @destroy: Destruction function. If NULL, kfree is used.
  116. * @num_pages: Actual number of pages.
  117. * @acc_size: Accounted size for this object.
  118. * @kref: Reference count of this buffer object. When this refcount reaches
  119. * zero, the object is put on the delayed delete list.
  120. * @list_kref: List reference count of this buffer object. This member is
  121. * used to avoid destruction while the buffer object is still on a list.
  122. * Lru lists may keep one refcount, the delayed delete list, and kref != 0
  123. * keeps one refcount. When this refcount reaches zero,
  124. * the object is destroyed.
  125. * @mem: structure describing current placement.
  126. * @persistent_swap_storage: Usually the swap storage is deleted for buffers
  127. * pinned in physical memory. If this behaviour is not desired, this member
  128. * holds a pointer to a persistent shmem object.
  129. * @ttm: TTM structure holding system pages.
  130. * @evicted: Whether the object was evicted without user-space knowing.
  131. * @cpu_writes: For synchronization. Number of cpu writers.
  132. * @lru: List head for the lru list.
  133. * @ddestroy: List head for the delayed destroy list.
  134. * @swap: List head for swap LRU list.
  135. * @moving: Fence set when BO is moving
  136. * @vma_node: Address space manager node.
  137. * @offset: The current GPU offset, which can have different meanings
  138. * depending on the memory type. For SYSTEM type memory, it should be 0.
  139. * @cur_placement: Hint of current placement.
  140. * @wu_mutex: Wait unreserved mutex.
  141. *
  142. * Base class for TTM buffer object, that deals with data placement and CPU
  143. * mappings. GPU mappings are really up to the driver, but for simpler GPUs
  144. * the driver can usually use the placement offset @offset directly as the
  145. * GPU virtual address. For drivers implementing multiple
  146. * GPU memory manager contexts, the driver should manage the address space
  147. * in these contexts separately and use these objects to get the correct
  148. * placement and caching for these GPU maps. This makes it possible to use
  149. * these objects for even quite elaborate memory management schemes.
  150. * The destroy member, the API visibility of this object makes it possible
  151. * to derive driver specific types.
  152. */
  153. struct ttm_buffer_object {
  154. /**
  155. * Members constant at init.
  156. */
  157. struct ttm_bo_device *bdev;
  158. enum ttm_bo_type type;
  159. void (*destroy) (struct ttm_buffer_object *);
  160. unsigned long num_pages;
  161. size_t acc_size;
  162. /**
  163. * Members not needing protection.
  164. */
  165. struct kref kref;
  166. struct kref list_kref;
  167. /**
  168. * Members protected by the bo::resv::reserved lock.
  169. */
  170. struct ttm_mem_reg mem;
  171. struct file *persistent_swap_storage;
  172. struct ttm_tt *ttm;
  173. bool evicted;
  174. /**
  175. * Members protected by the bo::reserved lock only when written to.
  176. */
  177. atomic_t cpu_writers;
  178. /**
  179. * Members protected by the bdev::lru_lock.
  180. */
  181. struct list_head lru;
  182. struct list_head ddestroy;
  183. struct list_head swap;
  184. struct list_head io_reserve_lru;
  185. /**
  186. * Members protected by a bo reservation.
  187. */
  188. struct dma_fence *moving;
  189. struct drm_vma_offset_node vma_node;
  190. unsigned priority;
  191. /**
  192. * Special members that are protected by the reserve lock
  193. * and the bo::lock when written to. Can be read with
  194. * either of these locks held.
  195. */
  196. uint64_t offset; /* GPU address space is independent of CPU word size */
  197. struct sg_table *sg;
  198. struct reservation_object *resv;
  199. struct reservation_object ttm_resv;
  200. struct mutex wu_mutex;
  201. };
  202. /**
  203. * struct ttm_bo_kmap_obj
  204. *
  205. * @virtual: The current kernel virtual address.
  206. * @page: The page when kmap'ing a single page.
  207. * @bo_kmap_type: Type of bo_kmap.
  208. *
  209. * Object describing a kernel mapping. Since a TTM bo may be located
  210. * in various memory types with various caching policies, the
  211. * mapping can either be an ioremap, a vmap, a kmap or part of a
  212. * premapped region.
  213. */
  214. #define TTM_BO_MAP_IOMEM_MASK 0x80
  215. struct ttm_bo_kmap_obj {
  216. void *virtual;
  217. struct page *page;
  218. enum {
  219. ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
  220. ttm_bo_map_vmap = 2,
  221. ttm_bo_map_kmap = 3,
  222. ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
  223. } bo_kmap_type;
  224. struct ttm_buffer_object *bo;
  225. };
  226. /**
  227. * struct ttm_operation_ctx
  228. *
  229. * @interruptible: Sleep interruptible if sleeping.
  230. * @no_wait_gpu: Return immediately if the GPU is busy.
  231. * @resv: Reservation object to allow reserved evictions with.
  232. * @flags: Including the following flags
  233. *
  234. * Context for TTM operations like changing buffer placement or general memory
  235. * allocation.
  236. */
  237. struct ttm_operation_ctx {
  238. bool interruptible;
  239. bool no_wait_gpu;
  240. struct reservation_object *resv;
  241. uint64_t bytes_moved;
  242. uint32_t flags;
  243. };
  244. /* Allow eviction of reserved BOs */
  245. #define TTM_OPT_FLAG_ALLOW_RES_EVICT 0x1
  246. /* when serving page fault or suspend, allow alloc anyway */
  247. #define TTM_OPT_FLAG_FORCE_ALLOC 0x2
  248. /**
  249. * ttm_bo_reference - reference a struct ttm_buffer_object
  250. *
  251. * @bo: The buffer object.
  252. *
  253. * Returns a refcounted pointer to a buffer object.
  254. */
  255. static inline struct ttm_buffer_object *
  256. ttm_bo_reference(struct ttm_buffer_object *bo)
  257. {
  258. kref_get(&bo->kref);
  259. return bo;
  260. }
  261. /**
  262. * ttm_bo_wait - wait for buffer idle.
  263. *
  264. * @bo: The buffer object.
  265. * @interruptible: Use interruptible wait.
  266. * @no_wait: Return immediately if buffer is busy.
  267. *
  268. * This function must be called with the bo::mutex held, and makes
  269. * sure any previous rendering to the buffer is completed.
  270. * Note: It might be necessary to block validations before the
  271. * wait by reserving the buffer.
  272. * Returns -EBUSY if no_wait is true and the buffer is busy.
  273. * Returns -ERESTARTSYS if interrupted by a signal.
  274. */
  275. int ttm_bo_wait(struct ttm_buffer_object *bo, bool interruptible, bool no_wait);
  276. /**
  277. * ttm_bo_mem_compat - Check if proposed placement is compatible with a bo
  278. *
  279. * @placement: Return immediately if buffer is busy.
  280. * @mem: The struct ttm_mem_reg indicating the region where the bo resides
  281. * @new_flags: Describes compatible placement found
  282. *
  283. * Returns true if the placement is compatible
  284. */
  285. bool ttm_bo_mem_compat(struct ttm_placement *placement, struct ttm_mem_reg *mem,
  286. uint32_t *new_flags);
  287. /**
  288. * ttm_bo_validate
  289. *
  290. * @bo: The buffer object.
  291. * @placement: Proposed placement for the buffer object.
  292. * @ctx: validation parameters.
  293. *
  294. * Changes placement and caching policy of the buffer object
  295. * according proposed placement.
  296. * Returns
  297. * -EINVAL on invalid proposed placement.
  298. * -ENOMEM on out-of-memory condition.
  299. * -EBUSY if no_wait is true and buffer busy.
  300. * -ERESTARTSYS if interrupted by a signal.
  301. */
  302. int ttm_bo_validate(struct ttm_buffer_object *bo,
  303. struct ttm_placement *placement,
  304. struct ttm_operation_ctx *ctx);
  305. /**
  306. * ttm_bo_unref
  307. *
  308. * @bo: The buffer object.
  309. *
  310. * Unreference and clear a pointer to a buffer object.
  311. */
  312. void ttm_bo_unref(struct ttm_buffer_object **bo);
  313. /**
  314. * ttm_bo_add_to_lru
  315. *
  316. * @bo: The buffer object.
  317. *
  318. * Add this bo to the relevant mem type lru and, if it's backed by
  319. * system pages (ttms) to the swap list.
  320. * This function must be called with struct ttm_bo_global::lru_lock held, and
  321. * is typically called immediately prior to unreserving a bo.
  322. */
  323. void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);
  324. /**
  325. * ttm_bo_del_from_lru
  326. *
  327. * @bo: The buffer object.
  328. *
  329. * Remove this bo from all lru lists used to lookup and reserve an object.
  330. * This function must be called with struct ttm_bo_global::lru_lock held,
  331. * and is usually called just immediately after the bo has been reserved to
  332. * avoid recursive reservation from lru lists.
  333. */
  334. void ttm_bo_del_from_lru(struct ttm_buffer_object *bo);
  335. /**
  336. * ttm_bo_move_to_lru_tail
  337. *
  338. * @bo: The buffer object.
  339. *
  340. * Move this BO to the tail of all lru lists used to lookup and reserve an
  341. * object. This function must be called with struct ttm_bo_global::lru_lock
  342. * held, and is used to make a BO less likely to be considered for eviction.
  343. */
  344. void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo);
  345. /**
  346. * ttm_bo_lock_delayed_workqueue
  347. *
  348. * Prevent the delayed workqueue from running.
  349. * Returns
  350. * True if the workqueue was queued at the time
  351. */
  352. int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
  353. /**
  354. * ttm_bo_unlock_delayed_workqueue
  355. *
  356. * Allows the delayed workqueue to run.
  357. */
  358. void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched);
  359. /**
  360. * ttm_bo_eviction_valuable
  361. *
  362. * @bo: The buffer object to evict
  363. * @place: the placement we need to make room for
  364. *
  365. * Check if it is valuable to evict the BO to make room for the given placement.
  366. */
  367. bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
  368. const struct ttm_place *place);
  369. /**
  370. * ttm_bo_synccpu_write_grab
  371. *
  372. * @bo: The buffer object:
  373. * @no_wait: Return immediately if buffer is busy.
  374. *
  375. * Synchronizes a buffer object for CPU RW access. This means
  376. * command submission that affects the buffer will return -EBUSY
  377. * until ttm_bo_synccpu_write_release is called.
  378. *
  379. * Returns
  380. * -EBUSY if the buffer is busy and no_wait is true.
  381. * -ERESTARTSYS if interrupted by a signal.
  382. */
  383. int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
  384. /**
  385. * ttm_bo_synccpu_write_release:
  386. *
  387. * @bo : The buffer object.
  388. *
  389. * Releases a synccpu lock.
  390. */
  391. void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo);
  392. /**
  393. * ttm_bo_acc_size
  394. *
  395. * @bdev: Pointer to a ttm_bo_device struct.
  396. * @bo_size: size of the buffer object in byte.
  397. * @struct_size: size of the structure holding buffer object datas
  398. *
  399. * Returns size to account for a buffer object
  400. */
  401. size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
  402. unsigned long bo_size,
  403. unsigned struct_size);
  404. size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
  405. unsigned long bo_size,
  406. unsigned struct_size);
  407. /**
  408. * ttm_bo_init_reserved
  409. *
  410. * @bdev: Pointer to a ttm_bo_device struct.
  411. * @bo: Pointer to a ttm_buffer_object to be initialized.
  412. * @size: Requested size of buffer object.
  413. * @type: Requested type of buffer object.
  414. * @flags: Initial placement flags.
  415. * @page_alignment: Data alignment in pages.
  416. * @ctx: TTM operation context for memory allocation.
  417. * @acc_size: Accounted size for this object.
  418. * @resv: Pointer to a reservation_object, or NULL to let ttm allocate one.
  419. * @destroy: Destroy function. Use NULL for kfree().
  420. *
  421. * This function initializes a pre-allocated struct ttm_buffer_object.
  422. * As this object may be part of a larger structure, this function,
  423. * together with the @destroy function,
  424. * enables driver-specific objects derived from a ttm_buffer_object.
  425. *
  426. * On successful return, the caller owns an object kref to @bo. The kref and
  427. * list_kref are usually set to 1, but note that in some situations, other
  428. * tasks may already be holding references to @bo as well.
  429. * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
  430. * and it is the caller's responsibility to call ttm_bo_unreserve.
  431. *
  432. * If a failure occurs, the function will call the @destroy function, or
  433. * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
  434. * illegal and will likely cause memory corruption.
  435. *
  436. * Returns
  437. * -ENOMEM: Out of memory.
  438. * -EINVAL: Invalid placement flags.
  439. * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
  440. */
  441. int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
  442. struct ttm_buffer_object *bo,
  443. unsigned long size,
  444. enum ttm_bo_type type,
  445. struct ttm_placement *placement,
  446. uint32_t page_alignment,
  447. struct ttm_operation_ctx *ctx,
  448. size_t acc_size,
  449. struct sg_table *sg,
  450. struct reservation_object *resv,
  451. void (*destroy) (struct ttm_buffer_object *));
  452. /**
  453. * ttm_bo_init
  454. *
  455. * @bdev: Pointer to a ttm_bo_device struct.
  456. * @bo: Pointer to a ttm_buffer_object to be initialized.
  457. * @size: Requested size of buffer object.
  458. * @type: Requested type of buffer object.
  459. * @flags: Initial placement flags.
  460. * @page_alignment: Data alignment in pages.
  461. * @interruptible: If needing to sleep to wait for GPU resources,
  462. * sleep interruptible.
  463. * pinned in physical memory. If this behaviour is not desired, this member
  464. * holds a pointer to a persistent shmem object. Typically, this would
  465. * point to the shmem object backing a GEM object if TTM is used to back a
  466. * GEM user interface.
  467. * @acc_size: Accounted size for this object.
  468. * @resv: Pointer to a reservation_object, or NULL to let ttm allocate one.
  469. * @destroy: Destroy function. Use NULL for kfree().
  470. *
  471. * This function initializes a pre-allocated struct ttm_buffer_object.
  472. * As this object may be part of a larger structure, this function,
  473. * together with the @destroy function,
  474. * enables driver-specific objects derived from a ttm_buffer_object.
  475. *
  476. * On successful return, the caller owns an object kref to @bo. The kref and
  477. * list_kref are usually set to 1, but note that in some situations, other
  478. * tasks may already be holding references to @bo as well.
  479. *
  480. * If a failure occurs, the function will call the @destroy function, or
  481. * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
  482. * illegal and will likely cause memory corruption.
  483. *
  484. * Returns
  485. * -ENOMEM: Out of memory.
  486. * -EINVAL: Invalid placement flags.
  487. * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
  488. */
  489. int ttm_bo_init(struct ttm_bo_device *bdev, struct ttm_buffer_object *bo,
  490. unsigned long size, enum ttm_bo_type type,
  491. struct ttm_placement *placement,
  492. uint32_t page_alignment, bool interrubtible, size_t acc_size,
  493. struct sg_table *sg, struct reservation_object *resv,
  494. void (*destroy) (struct ttm_buffer_object *));
  495. /**
  496. * ttm_bo_create
  497. *
  498. * @bdev: Pointer to a ttm_bo_device struct.
  499. * @size: Requested size of buffer object.
  500. * @type: Requested type of buffer object.
  501. * @placement: Initial placement.
  502. * @page_alignment: Data alignment in pages.
  503. * @interruptible: If needing to sleep while waiting for GPU resources,
  504. * sleep interruptible.
  505. * @p_bo: On successful completion *p_bo points to the created object.
  506. *
  507. * This function allocates a ttm_buffer_object, and then calls ttm_bo_init
  508. * on that object. The destroy function is set to kfree().
  509. * Returns
  510. * -ENOMEM: Out of memory.
  511. * -EINVAL: Invalid placement flags.
  512. * -ERESTARTSYS: Interrupted by signal while waiting for resources.
  513. */
  514. int ttm_bo_create(struct ttm_bo_device *bdev, unsigned long size,
  515. enum ttm_bo_type type, struct ttm_placement *placement,
  516. uint32_t page_alignment, bool interruptible,
  517. struct ttm_buffer_object **p_bo);
  518. /**
  519. * ttm_bo_init_mm
  520. *
  521. * @bdev: Pointer to a ttm_bo_device struct.
  522. * @mem_type: The memory type.
  523. * @p_size: size managed area in pages.
  524. *
  525. * Initialize a manager for a given memory type.
  526. * Note: if part of driver firstopen, it must be protected from a
  527. * potentially racing lastclose.
  528. * Returns:
  529. * -EINVAL: invalid size or memory type.
  530. * -ENOMEM: Not enough memory.
  531. * May also return driver-specified errors.
  532. */
  533. int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
  534. unsigned long p_size);
  535. /**
  536. * ttm_bo_clean_mm
  537. *
  538. * @bdev: Pointer to a ttm_bo_device struct.
  539. * @mem_type: The memory type.
  540. *
  541. * Take down a manager for a given memory type after first walking
  542. * the LRU list to evict any buffers left alive.
  543. *
  544. * Normally, this function is part of lastclose() or unload(), and at that
  545. * point there shouldn't be any buffers left created by user-space, since
  546. * there should've been removed by the file descriptor release() method.
  547. * However, before this function is run, make sure to signal all sync objects,
  548. * and verify that the delayed delete queue is empty. The driver must also
  549. * make sure that there are no NO_EVICT buffers present in this memory type
  550. * when the call is made.
  551. *
  552. * If this function is part of a VT switch, the caller must make sure that
  553. * there are no appications currently validating buffers before this
  554. * function is called. The caller can do that by first taking the
  555. * struct ttm_bo_device::ttm_lock in write mode.
  556. *
  557. * Returns:
  558. * -EINVAL: invalid or uninitialized memory type.
  559. * -EBUSY: There are still buffers left in this memory type.
  560. */
  561. int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type);
  562. /**
  563. * ttm_bo_evict_mm
  564. *
  565. * @bdev: Pointer to a ttm_bo_device struct.
  566. * @mem_type: The memory type.
  567. *
  568. * Evicts all buffers on the lru list of the memory type.
  569. * This is normally part of a VT switch or an
  570. * out-of-memory-space-due-to-fragmentation handler.
  571. * The caller must make sure that there are no other processes
  572. * currently validating buffers, and can do that by taking the
  573. * struct ttm_bo_device::ttm_lock in write mode.
  574. *
  575. * Returns:
  576. * -EINVAL: Invalid or uninitialized memory type.
  577. * -ERESTARTSYS: The call was interrupted by a signal while waiting to
  578. * evict a buffer.
  579. */
  580. int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type);
  581. /**
  582. * ttm_kmap_obj_virtual
  583. *
  584. * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
  585. * @is_iomem: Pointer to an integer that on return indicates 1 if the
  586. * virtual map is io memory, 0 if normal memory.
  587. *
  588. * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
  589. * If *is_iomem is 1 on return, the virtual address points to an io memory area,
  590. * that should strictly be accessed by the iowriteXX() and similar functions.
  591. */
  592. static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
  593. bool *is_iomem)
  594. {
  595. *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
  596. return map->virtual;
  597. }
  598. /**
  599. * ttm_bo_kmap
  600. *
  601. * @bo: The buffer object.
  602. * @start_page: The first page to map.
  603. * @num_pages: Number of pages to map.
  604. * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
  605. *
  606. * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
  607. * data in the buffer object. The ttm_kmap_obj_virtual function can then be
  608. * used to obtain a virtual address to the data.
  609. *
  610. * Returns
  611. * -ENOMEM: Out of memory.
  612. * -EINVAL: Invalid range.
  613. */
  614. int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
  615. unsigned long num_pages, struct ttm_bo_kmap_obj *map);
  616. /**
  617. * ttm_bo_kunmap
  618. *
  619. * @map: Object describing the map to unmap.
  620. *
  621. * Unmaps a kernel map set up by ttm_bo_kmap.
  622. */
  623. void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
  624. /**
  625. * ttm_fbdev_mmap - mmap fbdev memory backed by a ttm buffer object.
  626. *
  627. * @vma: vma as input from the fbdev mmap method.
  628. * @bo: The bo backing the address space. The address space will
  629. * have the same size as the bo, and start at offset 0.
  630. *
  631. * This function is intended to be called by the fbdev mmap method
  632. * if the fbdev address space is to be backed by a bo.
  633. */
  634. int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
  635. /**
  636. * ttm_bo_mmap - mmap out of the ttm device address space.
  637. *
  638. * @filp: filp as input from the mmap method.
  639. * @vma: vma as input from the mmap method.
  640. * @bdev: Pointer to the ttm_bo_device with the address space manager.
  641. *
  642. * This function is intended to be called by the device mmap method.
  643. * if the device address space is to be backed by the bo manager.
  644. */
  645. int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
  646. struct ttm_bo_device *bdev);
  647. void *ttm_kmap_atomic_prot(struct page *page, pgprot_t prot);
  648. void ttm_kunmap_atomic_prot(void *addr, pgprot_t prot);
  649. /**
  650. * ttm_bo_io
  651. *
  652. * @bdev: Pointer to the struct ttm_bo_device.
  653. * @filp: Pointer to the struct file attempting to read / write.
  654. * @wbuf: User-space pointer to address of buffer to write. NULL on read.
  655. * @rbuf: User-space pointer to address of buffer to read into.
  656. * Null on write.
  657. * @count: Number of bytes to read / write.
  658. * @f_pos: Pointer to current file position.
  659. * @write: 1 for read, 0 for write.
  660. *
  661. * This function implements read / write into ttm buffer objects, and is
  662. * intended to
  663. * be called from the fops::read and fops::write method.
  664. * Returns:
  665. * See man (2) write, man(2) read. In particular,
  666. * the function may return -ERESTARTSYS if
  667. * interrupted by a signal.
  668. */
  669. ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
  670. const char __user *wbuf, char __user *rbuf,
  671. size_t count, loff_t *f_pos, bool write);
  672. int ttm_bo_swapout(struct ttm_bo_global *glob,
  673. struct ttm_operation_ctx *ctx);
  674. void ttm_bo_swapout_all(struct ttm_bo_device *bdev);
  675. int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo);
  676. #endif