ttm_bo_api.h 24 KB

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