ttm_bo_driver.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. #include <drm/drm_mm.h>
  33. #include <drm/drm_global.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/reservation.h>
  39. #include "ttm_bo_api.h"
  40. #include "ttm_memory.h"
  41. #include "ttm_module.h"
  42. #include "ttm_placement.h"
  43. #include "ttm_tt.h"
  44. #define TTM_MAX_BO_PRIORITY 4U
  45. #define TTM_MEMTYPE_FLAG_FIXED (1 << 0) /* Fixed (on-card) PCI memory */
  46. #define TTM_MEMTYPE_FLAG_MAPPABLE (1 << 1) /* Memory mappable */
  47. #define TTM_MEMTYPE_FLAG_CMA (1 << 3) /* Can't map aperture */
  48. struct ttm_mem_type_manager;
  49. struct ttm_mem_type_manager_func {
  50. /**
  51. * struct ttm_mem_type_manager member init
  52. *
  53. * @man: Pointer to a memory type manager.
  54. * @p_size: Implementation dependent, but typically the size of the
  55. * range to be managed in pages.
  56. *
  57. * Called to initialize a private range manager. The function is
  58. * expected to initialize the man::priv member.
  59. * Returns 0 on success, negative error code on failure.
  60. */
  61. int (*init)(struct ttm_mem_type_manager *man, unsigned long p_size);
  62. /**
  63. * struct ttm_mem_type_manager member takedown
  64. *
  65. * @man: Pointer to a memory type manager.
  66. *
  67. * Called to undo the setup done in init. All allocated resources
  68. * should be freed.
  69. */
  70. int (*takedown)(struct ttm_mem_type_manager *man);
  71. /**
  72. * struct ttm_mem_type_manager member get_node
  73. *
  74. * @man: Pointer to a memory type manager.
  75. * @bo: Pointer to the buffer object we're allocating space for.
  76. * @placement: Placement details.
  77. * @flags: Additional placement flags.
  78. * @mem: Pointer to a struct ttm_mem_reg to be filled in.
  79. *
  80. * This function should allocate space in the memory type managed
  81. * by @man. Placement details if
  82. * applicable are given by @placement. If successful,
  83. * @mem::mm_node should be set to a non-null value, and
  84. * @mem::start should be set to a value identifying the beginning
  85. * of the range allocated, and the function should return zero.
  86. * If the memory region accommodate the buffer object, @mem::mm_node
  87. * should be set to NULL, and the function should return 0.
  88. * If a system error occurred, preventing the request to be fulfilled,
  89. * the function should return a negative error code.
  90. *
  91. * Note that @mem::mm_node will only be dereferenced by
  92. * struct ttm_mem_type_manager functions and optionally by the driver,
  93. * which has knowledge of the underlying type.
  94. *
  95. * This function may not be called from within atomic context, so
  96. * an implementation can and must use either a mutex or a spinlock to
  97. * protect any data structures managing the space.
  98. */
  99. int (*get_node)(struct ttm_mem_type_manager *man,
  100. struct ttm_buffer_object *bo,
  101. const struct ttm_place *place,
  102. struct ttm_mem_reg *mem);
  103. /**
  104. * struct ttm_mem_type_manager member put_node
  105. *
  106. * @man: Pointer to a memory type manager.
  107. * @mem: Pointer to a struct ttm_mem_reg to be filled in.
  108. *
  109. * This function frees memory type resources previously allocated
  110. * and that are identified by @mem::mm_node and @mem::start. May not
  111. * be called from within atomic context.
  112. */
  113. void (*put_node)(struct ttm_mem_type_manager *man,
  114. struct ttm_mem_reg *mem);
  115. /**
  116. * struct ttm_mem_type_manager member debug
  117. *
  118. * @man: Pointer to a memory type manager.
  119. * @printer: Prefix to be used in printout to identify the caller.
  120. *
  121. * This function is called to print out the state of the memory
  122. * type manager to aid debugging of out-of-memory conditions.
  123. * It may not be called from within atomic context.
  124. */
  125. void (*debug)(struct ttm_mem_type_manager *man,
  126. struct drm_printer *printer);
  127. };
  128. /**
  129. * struct ttm_mem_type_manager
  130. *
  131. * @has_type: The memory type has been initialized.
  132. * @use_type: The memory type is enabled.
  133. * @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory
  134. * managed by this memory type.
  135. * @gpu_offset: If used, the GPU offset of the first managed page of
  136. * fixed memory or the first managed location in an aperture.
  137. * @size: Size of the managed region.
  138. * @available_caching: A mask of available caching types, TTM_PL_FLAG_XX,
  139. * as defined in ttm_placement_common.h
  140. * @default_caching: The default caching policy used for a buffer object
  141. * placed in this memory type if the user doesn't provide one.
  142. * @func: structure pointer implementing the range manager. See above
  143. * @priv: Driver private closure for @func.
  144. * @io_reserve_mutex: Mutex optionally protecting shared io_reserve structures
  145. * @use_io_reserve_lru: Use an lru list to try to unreserve io_mem_regions
  146. * reserved by the TTM vm system.
  147. * @io_reserve_lru: Optional lru list for unreserving io mem regions.
  148. * @io_reserve_fastpath: Only use bdev::driver::io_mem_reserve to obtain
  149. * @move_lock: lock for move fence
  150. * static information. bdev::driver::io_mem_free is never used.
  151. * @lru: The lru list for this memory type.
  152. * @move: The fence of the last pipelined move operation.
  153. *
  154. * This structure is used to identify and manage memory types for a device.
  155. * It's set up by the ttm_bo_driver::init_mem_type method.
  156. */
  157. struct ttm_mem_type_manager {
  158. struct ttm_bo_device *bdev;
  159. /*
  160. * No protection. Constant from start.
  161. */
  162. bool has_type;
  163. bool use_type;
  164. uint32_t flags;
  165. uint64_t gpu_offset; /* GPU address space is independent of CPU word size */
  166. uint64_t size;
  167. uint32_t available_caching;
  168. uint32_t default_caching;
  169. const struct ttm_mem_type_manager_func *func;
  170. void *priv;
  171. struct mutex io_reserve_mutex;
  172. bool use_io_reserve_lru;
  173. bool io_reserve_fastpath;
  174. spinlock_t move_lock;
  175. /*
  176. * Protected by @io_reserve_mutex:
  177. */
  178. struct list_head io_reserve_lru;
  179. /*
  180. * Protected by the global->lru_lock.
  181. */
  182. struct list_head lru[TTM_MAX_BO_PRIORITY];
  183. /*
  184. * Protected by @move_lock.
  185. */
  186. struct dma_fence *move;
  187. };
  188. /**
  189. * struct ttm_bo_driver
  190. *
  191. * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
  192. * @invalidate_caches: Callback to invalidate read caches when a buffer object
  193. * has been evicted.
  194. * @init_mem_type: Callback to initialize a struct ttm_mem_type_manager
  195. * structure.
  196. * @evict_flags: Callback to obtain placement flags when a buffer is evicted.
  197. * @move: Callback for a driver to hook in accelerated functions to
  198. * move a buffer.
  199. * If set to NULL, a potentially slow memcpy() move is used.
  200. */
  201. struct ttm_bo_driver {
  202. /**
  203. * ttm_tt_create
  204. *
  205. * @bo: The buffer object to create the ttm for.
  206. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  207. *
  208. * Create a struct ttm_tt to back data with system memory pages.
  209. * No pages are actually allocated.
  210. * Returns:
  211. * NULL: Out of memory.
  212. */
  213. struct ttm_tt *(*ttm_tt_create)(struct ttm_buffer_object *bo,
  214. uint32_t page_flags);
  215. /**
  216. * ttm_tt_populate
  217. *
  218. * @ttm: The struct ttm_tt to contain the backing pages.
  219. *
  220. * Allocate all backing pages
  221. * Returns:
  222. * -ENOMEM: Out of memory.
  223. */
  224. int (*ttm_tt_populate)(struct ttm_tt *ttm,
  225. struct ttm_operation_ctx *ctx);
  226. /**
  227. * ttm_tt_unpopulate
  228. *
  229. * @ttm: The struct ttm_tt to contain the backing pages.
  230. *
  231. * Free all backing page
  232. */
  233. void (*ttm_tt_unpopulate)(struct ttm_tt *ttm);
  234. /**
  235. * struct ttm_bo_driver member invalidate_caches
  236. *
  237. * @bdev: the buffer object device.
  238. * @flags: new placement of the rebound buffer object.
  239. *
  240. * A previosly evicted buffer has been rebound in a
  241. * potentially new location. Tell the driver that it might
  242. * consider invalidating read (texture) caches on the next command
  243. * submission as a consequence.
  244. */
  245. int (*invalidate_caches)(struct ttm_bo_device *bdev, uint32_t flags);
  246. int (*init_mem_type)(struct ttm_bo_device *bdev, uint32_t type,
  247. struct ttm_mem_type_manager *man);
  248. /**
  249. * struct ttm_bo_driver member eviction_valuable
  250. *
  251. * @bo: the buffer object to be evicted
  252. * @place: placement we need room for
  253. *
  254. * Check with the driver if it is valuable to evict a BO to make room
  255. * for a certain placement.
  256. */
  257. bool (*eviction_valuable)(struct ttm_buffer_object *bo,
  258. const struct ttm_place *place);
  259. /**
  260. * struct ttm_bo_driver member evict_flags:
  261. *
  262. * @bo: the buffer object to be evicted
  263. *
  264. * Return the bo flags for a buffer which is not mapped to the hardware.
  265. * These will be placed in proposed_flags so that when the move is
  266. * finished, they'll end up in bo->mem.flags
  267. */
  268. void (*evict_flags)(struct ttm_buffer_object *bo,
  269. struct ttm_placement *placement);
  270. /**
  271. * struct ttm_bo_driver member move:
  272. *
  273. * @bo: the buffer to move
  274. * @evict: whether this motion is evicting the buffer from
  275. * the graphics address space
  276. * @ctx: context for this move with parameters
  277. * @new_mem: the new memory region receiving the buffer
  278. *
  279. * Move a buffer between two memory regions.
  280. */
  281. int (*move)(struct ttm_buffer_object *bo, bool evict,
  282. struct ttm_operation_ctx *ctx,
  283. struct ttm_mem_reg *new_mem);
  284. /**
  285. * struct ttm_bo_driver_member verify_access
  286. *
  287. * @bo: Pointer to a buffer object.
  288. * @filp: Pointer to a struct file trying to access the object.
  289. *
  290. * Called from the map / write / read methods to verify that the
  291. * caller is permitted to access the buffer object.
  292. * This member may be set to NULL, which will refuse this kind of
  293. * access for all buffer objects.
  294. * This function should return 0 if access is granted, -EPERM otherwise.
  295. */
  296. int (*verify_access)(struct ttm_buffer_object *bo,
  297. struct file *filp);
  298. /**
  299. * Hook to notify driver about a driver move so it
  300. * can do tiling things and book-keeping.
  301. *
  302. * @evict: whether this move is evicting the buffer from the graphics
  303. * address space
  304. */
  305. void (*move_notify)(struct ttm_buffer_object *bo,
  306. bool evict,
  307. struct ttm_mem_reg *new_mem);
  308. /* notify the driver we are taking a fault on this BO
  309. * and have reserved it */
  310. int (*fault_reserve_notify)(struct ttm_buffer_object *bo);
  311. /**
  312. * notify the driver that we're about to swap out this bo
  313. */
  314. void (*swap_notify)(struct ttm_buffer_object *bo);
  315. /**
  316. * Driver callback on when mapping io memory (for bo_move_memcpy
  317. * for instance). TTM will take care to call io_mem_free whenever
  318. * the mapping is not use anymore. io_mem_reserve & io_mem_free
  319. * are balanced.
  320. */
  321. int (*io_mem_reserve)(struct ttm_bo_device *bdev,
  322. struct ttm_mem_reg *mem);
  323. void (*io_mem_free)(struct ttm_bo_device *bdev,
  324. struct ttm_mem_reg *mem);
  325. /**
  326. * Return the pfn for a given page_offset inside the BO.
  327. *
  328. * @bo: the BO to look up the pfn for
  329. * @page_offset: the offset to look up
  330. */
  331. unsigned long (*io_mem_pfn)(struct ttm_buffer_object *bo,
  332. unsigned long page_offset);
  333. /**
  334. * Read/write memory buffers for ptrace access
  335. *
  336. * @bo: the BO to access
  337. * @offset: the offset from the start of the BO
  338. * @buf: pointer to source/destination buffer
  339. * @len: number of bytes to copy
  340. * @write: whether to read (0) from or write (non-0) to BO
  341. *
  342. * If successful, this function should return the number of
  343. * bytes copied, -EIO otherwise. If the number of bytes
  344. * returned is < len, the function may be called again with
  345. * the remainder of the buffer to copy.
  346. */
  347. int (*access_memory)(struct ttm_buffer_object *bo, unsigned long offset,
  348. void *buf, int len, int write);
  349. };
  350. /**
  351. * struct ttm_bo_global_ref - Argument to initialize a struct ttm_bo_global.
  352. */
  353. struct ttm_bo_global_ref {
  354. struct drm_global_reference ref;
  355. struct ttm_mem_global *mem_glob;
  356. };
  357. /**
  358. * struct ttm_bo_global - Buffer object driver global data.
  359. *
  360. * @mem_glob: Pointer to a struct ttm_mem_global object for accounting.
  361. * @dummy_read_page: Pointer to a dummy page used for mapping requests
  362. * of unpopulated pages.
  363. * @shrink: A shrink callback object used for buffer object swap.
  364. * @device_list_mutex: Mutex protecting the device list.
  365. * This mutex is held while traversing the device list for pm options.
  366. * @lru_lock: Spinlock protecting the bo subsystem lru lists.
  367. * @device_list: List of buffer object devices.
  368. * @swap_lru: Lru list of buffer objects used for swapping.
  369. */
  370. struct ttm_bo_global {
  371. /**
  372. * Constant after init.
  373. */
  374. struct kobject kobj;
  375. struct ttm_mem_global *mem_glob;
  376. struct page *dummy_read_page;
  377. struct mutex device_list_mutex;
  378. spinlock_t lru_lock;
  379. /**
  380. * Protected by device_list_mutex.
  381. */
  382. struct list_head device_list;
  383. /**
  384. * Protected by the lru_lock.
  385. */
  386. struct list_head swap_lru[TTM_MAX_BO_PRIORITY];
  387. /**
  388. * Internal protection.
  389. */
  390. atomic_t bo_count;
  391. };
  392. #define TTM_NUM_MEM_TYPES 8
  393. /**
  394. * struct ttm_bo_device - Buffer object driver device-specific data.
  395. *
  396. * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
  397. * @man: An array of mem_type_managers.
  398. * @vma_manager: Address space manager
  399. * lru_lock: Spinlock that protects the buffer+device lru lists and
  400. * ddestroy lists.
  401. * @dev_mapping: A pointer to the struct address_space representing the
  402. * device address space.
  403. * @wq: Work queue structure for the delayed delete workqueue.
  404. * @no_retry: Don't retry allocation if it fails
  405. *
  406. */
  407. struct ttm_bo_device {
  408. /*
  409. * Constant after bo device init / atomic.
  410. */
  411. struct list_head device_list;
  412. struct ttm_bo_global *glob;
  413. struct ttm_bo_driver *driver;
  414. struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];
  415. /*
  416. * Protected by internal locks.
  417. */
  418. struct drm_vma_offset_manager vma_manager;
  419. /*
  420. * Protected by the global:lru lock.
  421. */
  422. struct list_head ddestroy;
  423. /*
  424. * Protected by load / firstopen / lastclose /unload sync.
  425. */
  426. struct address_space *dev_mapping;
  427. /*
  428. * Internal protection.
  429. */
  430. struct delayed_work wq;
  431. bool need_dma32;
  432. bool no_retry;
  433. };
  434. /**
  435. * ttm_flag_masked
  436. *
  437. * @old: Pointer to the result and original value.
  438. * @new: New value of bits.
  439. * @mask: Mask of bits to change.
  440. *
  441. * Convenience function to change a number of bits identified by a mask.
  442. */
  443. static inline uint32_t
  444. ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
  445. {
  446. *old ^= (*old ^ new) & mask;
  447. return *old;
  448. }
  449. /*
  450. * ttm_bo.c
  451. */
  452. /**
  453. * ttm_mem_reg_is_pci
  454. *
  455. * @bdev: Pointer to a struct ttm_bo_device.
  456. * @mem: A valid struct ttm_mem_reg.
  457. *
  458. * Returns true if the memory described by @mem is PCI memory,
  459. * false otherwise.
  460. */
  461. bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem);
  462. /**
  463. * ttm_bo_mem_space
  464. *
  465. * @bo: Pointer to a struct ttm_buffer_object. the data of which
  466. * we want to allocate space for.
  467. * @proposed_placement: Proposed new placement for the buffer object.
  468. * @mem: A struct ttm_mem_reg.
  469. * @interruptible: Sleep interruptible when sliping.
  470. * @no_wait_gpu: Return immediately if the GPU is busy.
  471. *
  472. * Allocate memory space for the buffer object pointed to by @bo, using
  473. * the placement flags in @mem, potentially evicting other idle buffer objects.
  474. * This function may sleep while waiting for space to become available.
  475. * Returns:
  476. * -EBUSY: No space available (only if no_wait == 1).
  477. * -ENOMEM: Could not allocate memory for the buffer object, either due to
  478. * fragmentation or concurrent allocators.
  479. * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
  480. */
  481. int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  482. struct ttm_placement *placement,
  483. struct ttm_mem_reg *mem,
  484. struct ttm_operation_ctx *ctx);
  485. void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem);
  486. void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo,
  487. struct ttm_mem_reg *mem);
  488. void ttm_bo_global_release(struct drm_global_reference *ref);
  489. int ttm_bo_global_init(struct drm_global_reference *ref);
  490. int ttm_bo_device_release(struct ttm_bo_device *bdev);
  491. /**
  492. * ttm_bo_device_init
  493. *
  494. * @bdev: A pointer to a struct ttm_bo_device to initialize.
  495. * @glob: A pointer to an initialized struct ttm_bo_global.
  496. * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
  497. * @mapping: The address space to use for this bo.
  498. * @file_page_offset: Offset into the device address space that is available
  499. * for buffer data. This ensures compatibility with other users of the
  500. * address space.
  501. *
  502. * Initializes a struct ttm_bo_device:
  503. * Returns:
  504. * !0: Failure.
  505. */
  506. int ttm_bo_device_init(struct ttm_bo_device *bdev, struct ttm_bo_global *glob,
  507. struct ttm_bo_driver *driver,
  508. struct address_space *mapping,
  509. uint64_t file_page_offset, bool need_dma32);
  510. /**
  511. * ttm_bo_unmap_virtual
  512. *
  513. * @bo: tear down the virtual mappings for this BO
  514. */
  515. void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
  516. /**
  517. * ttm_bo_unmap_virtual
  518. *
  519. * @bo: tear down the virtual mappings for this BO
  520. *
  521. * The caller must take ttm_mem_io_lock before calling this function.
  522. */
  523. void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo);
  524. int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo);
  525. void ttm_mem_io_free_vm(struct ttm_buffer_object *bo);
  526. int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible);
  527. void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
  528. void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo);
  529. void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);
  530. /**
  531. * __ttm_bo_reserve:
  532. *
  533. * @bo: A pointer to a struct ttm_buffer_object.
  534. * @interruptible: Sleep interruptible if waiting.
  535. * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  536. * @ticket: ticket used to acquire the ww_mutex.
  537. *
  538. * Will not remove reserved buffers from the lru lists.
  539. * Otherwise identical to ttm_bo_reserve.
  540. *
  541. * Returns:
  542. * -EDEADLK: The reservation may cause a deadlock.
  543. * Release all buffer reservations, wait for @bo to become unreserved and
  544. * try again. (only if use_sequence == 1).
  545. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  546. * a signal. Release all buffer reservations and return to user-space.
  547. * -EBUSY: The function needed to sleep, but @no_wait was true
  548. * -EALREADY: Bo already reserved using @ticket. This error code will only
  549. * be returned if @use_ticket is set to true.
  550. */
  551. static inline int __ttm_bo_reserve(struct ttm_buffer_object *bo,
  552. bool interruptible, bool no_wait,
  553. struct ww_acquire_ctx *ticket)
  554. {
  555. int ret = 0;
  556. if (no_wait) {
  557. bool success;
  558. if (WARN_ON(ticket))
  559. return -EBUSY;
  560. success = reservation_object_trylock(bo->resv);
  561. return success ? 0 : -EBUSY;
  562. }
  563. if (interruptible)
  564. ret = reservation_object_lock_interruptible(bo->resv, ticket);
  565. else
  566. ret = reservation_object_lock(bo->resv, ticket);
  567. if (ret == -EINTR)
  568. return -ERESTARTSYS;
  569. return ret;
  570. }
  571. /**
  572. * ttm_bo_reserve:
  573. *
  574. * @bo: A pointer to a struct ttm_buffer_object.
  575. * @interruptible: Sleep interruptible if waiting.
  576. * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  577. * @ticket: ticket used to acquire the ww_mutex.
  578. *
  579. * Locks a buffer object for validation. (Or prevents other processes from
  580. * locking it for validation) and removes it from lru lists, while taking
  581. * a number of measures to prevent deadlocks.
  582. *
  583. * Deadlocks may occur when two processes try to reserve multiple buffers in
  584. * different order, either by will or as a result of a buffer being evicted
  585. * to make room for a buffer already reserved. (Buffers are reserved before
  586. * they are evicted). The following algorithm prevents such deadlocks from
  587. * occurring:
  588. * Processes attempting to reserve multiple buffers other than for eviction,
  589. * (typically execbuf), should first obtain a unique 32-bit
  590. * validation sequence number,
  591. * and call this function with @use_ticket == 1 and @ticket->stamp == the unique
  592. * sequence number. If upon call of this function, the buffer object is already
  593. * reserved, the validation sequence is checked against the validation
  594. * sequence of the process currently reserving the buffer,
  595. * and if the current validation sequence is greater than that of the process
  596. * holding the reservation, the function returns -EDEADLK. Otherwise it sleeps
  597. * waiting for the buffer to become unreserved, after which it retries
  598. * reserving.
  599. * The caller should, when receiving an -EDEADLK error
  600. * release all its buffer reservations, wait for @bo to become unreserved, and
  601. * then rerun the validation with the same validation sequence. This procedure
  602. * will always guarantee that the process with the lowest validation sequence
  603. * will eventually succeed, preventing both deadlocks and starvation.
  604. *
  605. * Returns:
  606. * -EDEADLK: The reservation may cause a deadlock.
  607. * Release all buffer reservations, wait for @bo to become unreserved and
  608. * try again. (only if use_sequence == 1).
  609. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  610. * a signal. Release all buffer reservations and return to user-space.
  611. * -EBUSY: The function needed to sleep, but @no_wait was true
  612. * -EALREADY: Bo already reserved using @ticket. This error code will only
  613. * be returned if @use_ticket is set to true.
  614. */
  615. static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
  616. bool interruptible, bool no_wait,
  617. struct ww_acquire_ctx *ticket)
  618. {
  619. int ret;
  620. WARN_ON(!kref_read(&bo->kref));
  621. ret = __ttm_bo_reserve(bo, interruptible, no_wait, ticket);
  622. if (likely(ret == 0))
  623. ttm_bo_del_sub_from_lru(bo);
  624. return ret;
  625. }
  626. /**
  627. * ttm_bo_reserve_slowpath:
  628. * @bo: A pointer to a struct ttm_buffer_object.
  629. * @interruptible: Sleep interruptible if waiting.
  630. * @sequence: Set (@bo)->sequence to this value after lock
  631. *
  632. * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
  633. * from all our other reservations. Because there are no other reservations
  634. * held by us, this function cannot deadlock any more.
  635. */
  636. static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
  637. bool interruptible,
  638. struct ww_acquire_ctx *ticket)
  639. {
  640. int ret = 0;
  641. WARN_ON(!kref_read(&bo->kref));
  642. if (interruptible)
  643. ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
  644. ticket);
  645. else
  646. ww_mutex_lock_slow(&bo->resv->lock, ticket);
  647. if (likely(ret == 0))
  648. ttm_bo_del_sub_from_lru(bo);
  649. else if (ret == -EINTR)
  650. ret = -ERESTARTSYS;
  651. return ret;
  652. }
  653. /**
  654. * ttm_bo_unreserve
  655. *
  656. * @bo: A pointer to a struct ttm_buffer_object.
  657. *
  658. * Unreserve a previous reservation of @bo.
  659. */
  660. static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
  661. {
  662. if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  663. spin_lock(&bo->bdev->glob->lru_lock);
  664. ttm_bo_add_to_lru(bo);
  665. spin_unlock(&bo->bdev->glob->lru_lock);
  666. }
  667. reservation_object_unlock(bo->resv);
  668. }
  669. /*
  670. * ttm_bo_util.c
  671. */
  672. int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
  673. struct ttm_mem_reg *mem);
  674. void ttm_mem_io_free(struct ttm_bo_device *bdev,
  675. struct ttm_mem_reg *mem);
  676. /**
  677. * ttm_bo_move_ttm
  678. *
  679. * @bo: A pointer to a struct ttm_buffer_object.
  680. * @interruptible: Sleep interruptible if waiting.
  681. * @no_wait_gpu: Return immediately if the GPU is busy.
  682. * @new_mem: struct ttm_mem_reg indicating where to move.
  683. *
  684. * Optimized move function for a buffer object with both old and
  685. * new placement backed by a TTM. The function will, if successful,
  686. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  687. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  688. * data remains untouched, and it's up to the caller to free the
  689. * memory space indicated by @new_mem.
  690. * Returns:
  691. * !0: Failure.
  692. */
  693. int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
  694. struct ttm_operation_ctx *ctx,
  695. struct ttm_mem_reg *new_mem);
  696. /**
  697. * ttm_bo_move_memcpy
  698. *
  699. * @bo: A pointer to a struct ttm_buffer_object.
  700. * @interruptible: Sleep interruptible if waiting.
  701. * @no_wait_gpu: Return immediately if the GPU is busy.
  702. * @new_mem: struct ttm_mem_reg indicating where to move.
  703. *
  704. * Fallback move function for a mappable buffer object in mappable memory.
  705. * The function will, if successful,
  706. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  707. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  708. * data remains untouched, and it's up to the caller to free the
  709. * memory space indicated by @new_mem.
  710. * Returns:
  711. * !0: Failure.
  712. */
  713. int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  714. struct ttm_operation_ctx *ctx,
  715. struct ttm_mem_reg *new_mem);
  716. /**
  717. * ttm_bo_free_old_node
  718. *
  719. * @bo: A pointer to a struct ttm_buffer_object.
  720. *
  721. * Utility function to free an old placement after a successful move.
  722. */
  723. void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
  724. /**
  725. * ttm_bo_move_accel_cleanup.
  726. *
  727. * @bo: A pointer to a struct ttm_buffer_object.
  728. * @fence: A fence object that signals when moving is complete.
  729. * @evict: This is an evict move. Don't return until the buffer is idle.
  730. * @new_mem: struct ttm_mem_reg indicating where to move.
  731. *
  732. * Accelerated move function to be called when an accelerated move
  733. * has been scheduled. The function will create a new temporary buffer object
  734. * representing the old placement, and put the sync object on both buffer
  735. * objects. After that the newly created buffer object is unref'd to be
  736. * destroyed when the move is complete. This will help pipeline
  737. * buffer moves.
  738. */
  739. int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  740. struct dma_fence *fence, bool evict,
  741. struct ttm_mem_reg *new_mem);
  742. /**
  743. * ttm_bo_pipeline_move.
  744. *
  745. * @bo: A pointer to a struct ttm_buffer_object.
  746. * @fence: A fence object that signals when moving is complete.
  747. * @evict: This is an evict move. Don't return until the buffer is idle.
  748. * @new_mem: struct ttm_mem_reg indicating where to move.
  749. *
  750. * Function for pipelining accelerated moves. Either free the memory
  751. * immediately or hang it on a temporary buffer object.
  752. */
  753. int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
  754. struct dma_fence *fence, bool evict,
  755. struct ttm_mem_reg *new_mem);
  756. /**
  757. * ttm_bo_pipeline_gutting.
  758. *
  759. * @bo: A pointer to a struct ttm_buffer_object.
  760. *
  761. * Pipelined gutting a BO of it's backing store.
  762. */
  763. int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo);
  764. /**
  765. * ttm_io_prot
  766. *
  767. * @c_state: Caching state.
  768. * @tmp: Page protection flag for a normal, cached mapping.
  769. *
  770. * Utility function that returns the pgprot_t that should be used for
  771. * setting up a PTE with the caching model indicated by @c_state.
  772. */
  773. pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);
  774. extern const struct ttm_mem_type_manager_func ttm_bo_manager_func;
  775. #endif