ttm_bo_api.h 21 KB

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