ttm_bo_driver.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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 <ttm/ttm_bo_api.h>
  33. #include <ttm/ttm_memory.h>
  34. #include <ttm/ttm_module.h>
  35. #include <ttm/ttm_placement.h>
  36. #include <drm/drm_mm.h>
  37. #include <drm/drm_global.h>
  38. #include <drm/drm_vma_manager.h>
  39. #include <linux/workqueue.h>
  40. #include <linux/fs.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/reservation.h>
  43. struct ttm_backend_func {
  44. /**
  45. * struct ttm_backend_func member bind
  46. *
  47. * @ttm: Pointer to a struct ttm_tt.
  48. * @bo_mem: Pointer to a struct ttm_mem_reg describing the
  49. * memory type and location for binding.
  50. *
  51. * Bind the backend pages into the aperture in the location
  52. * indicated by @bo_mem. This function should be able to handle
  53. * differences between aperture and system page sizes.
  54. */
  55. int (*bind) (struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
  56. /**
  57. * struct ttm_backend_func member unbind
  58. *
  59. * @ttm: Pointer to a struct ttm_tt.
  60. *
  61. * Unbind previously bound backend pages. This function should be
  62. * able to handle differences between aperture and system page sizes.
  63. */
  64. int (*unbind) (struct ttm_tt *ttm);
  65. /**
  66. * struct ttm_backend_func member destroy
  67. *
  68. * @ttm: Pointer to a struct ttm_tt.
  69. *
  70. * Destroy the backend. This will be call back from ttm_tt_destroy so
  71. * don't call ttm_tt_destroy from the callback or infinite loop.
  72. */
  73. void (*destroy) (struct ttm_tt *ttm);
  74. };
  75. #define TTM_PAGE_FLAG_WRITE (1 << 3)
  76. #define TTM_PAGE_FLAG_SWAPPED (1 << 4)
  77. #define TTM_PAGE_FLAG_PERSISTENT_SWAP (1 << 5)
  78. #define TTM_PAGE_FLAG_ZERO_ALLOC (1 << 6)
  79. #define TTM_PAGE_FLAG_DMA32 (1 << 7)
  80. #define TTM_PAGE_FLAG_SG (1 << 8)
  81. enum ttm_caching_state {
  82. tt_uncached,
  83. tt_wc,
  84. tt_cached
  85. };
  86. /**
  87. * struct ttm_tt
  88. *
  89. * @bdev: Pointer to a struct ttm_bo_device.
  90. * @func: Pointer to a struct ttm_backend_func that describes
  91. * the backend methods.
  92. * @dummy_read_page: Page to map where the ttm_tt page array contains a NULL
  93. * pointer.
  94. * @pages: Array of pages backing the data.
  95. * @num_pages: Number of pages in the page array.
  96. * @bdev: Pointer to the current struct ttm_bo_device.
  97. * @be: Pointer to the ttm backend.
  98. * @swap_storage: Pointer to shmem struct file for swap storage.
  99. * @caching_state: The current caching state of the pages.
  100. * @state: The current binding state of the pages.
  101. *
  102. * This is a structure holding the pages, caching- and aperture binding
  103. * status for a buffer object that isn't backed by fixed (VRAM / AGP)
  104. * memory.
  105. */
  106. struct ttm_tt {
  107. struct ttm_bo_device *bdev;
  108. struct ttm_backend_func *func;
  109. struct page *dummy_read_page;
  110. struct page **pages;
  111. uint32_t page_flags;
  112. unsigned long num_pages;
  113. struct sg_table *sg; /* for SG objects via dma-buf */
  114. struct ttm_bo_global *glob;
  115. struct file *swap_storage;
  116. enum ttm_caching_state caching_state;
  117. enum {
  118. tt_bound,
  119. tt_unbound,
  120. tt_unpopulated,
  121. } state;
  122. };
  123. /**
  124. * struct ttm_dma_tt
  125. *
  126. * @ttm: Base ttm_tt struct.
  127. * @dma_address: The DMA (bus) addresses of the pages
  128. * @pages_list: used by some page allocation backend
  129. *
  130. * This is a structure holding the pages, caching- and aperture binding
  131. * status for a buffer object that isn't backed by fixed (VRAM / AGP)
  132. * memory.
  133. */
  134. struct ttm_dma_tt {
  135. struct ttm_tt ttm;
  136. dma_addr_t *dma_address;
  137. struct list_head pages_list;
  138. };
  139. #define TTM_MEMTYPE_FLAG_FIXED (1 << 0) /* Fixed (on-card) PCI memory */
  140. #define TTM_MEMTYPE_FLAG_MAPPABLE (1 << 1) /* Memory mappable */
  141. #define TTM_MEMTYPE_FLAG_CMA (1 << 3) /* Can't map aperture */
  142. struct ttm_mem_type_manager;
  143. struct ttm_mem_type_manager_func {
  144. /**
  145. * struct ttm_mem_type_manager member init
  146. *
  147. * @man: Pointer to a memory type manager.
  148. * @p_size: Implementation dependent, but typically the size of the
  149. * range to be managed in pages.
  150. *
  151. * Called to initialize a private range manager. The function is
  152. * expected to initialize the man::priv member.
  153. * Returns 0 on success, negative error code on failure.
  154. */
  155. int (*init)(struct ttm_mem_type_manager *man, unsigned long p_size);
  156. /**
  157. * struct ttm_mem_type_manager member takedown
  158. *
  159. * @man: Pointer to a memory type manager.
  160. *
  161. * Called to undo the setup done in init. All allocated resources
  162. * should be freed.
  163. */
  164. int (*takedown)(struct ttm_mem_type_manager *man);
  165. /**
  166. * struct ttm_mem_type_manager member get_node
  167. *
  168. * @man: Pointer to a memory type manager.
  169. * @bo: Pointer to the buffer object we're allocating space for.
  170. * @placement: Placement details.
  171. * @flags: Additional placement flags.
  172. * @mem: Pointer to a struct ttm_mem_reg to be filled in.
  173. *
  174. * This function should allocate space in the memory type managed
  175. * by @man. Placement details if
  176. * applicable are given by @placement. If successful,
  177. * @mem::mm_node should be set to a non-null value, and
  178. * @mem::start should be set to a value identifying the beginning
  179. * of the range allocated, and the function should return zero.
  180. * If the memory region accommodate the buffer object, @mem::mm_node
  181. * should be set to NULL, and the function should return 0.
  182. * If a system error occurred, preventing the request to be fulfilled,
  183. * the function should return a negative error code.
  184. *
  185. * Note that @mem::mm_node will only be dereferenced by
  186. * struct ttm_mem_type_manager functions and optionally by the driver,
  187. * which has knowledge of the underlying type.
  188. *
  189. * This function may not be called from within atomic context, so
  190. * an implementation can and must use either a mutex or a spinlock to
  191. * protect any data structures managing the space.
  192. */
  193. int (*get_node)(struct ttm_mem_type_manager *man,
  194. struct ttm_buffer_object *bo,
  195. const struct ttm_place *place,
  196. struct ttm_mem_reg *mem);
  197. /**
  198. * struct ttm_mem_type_manager member put_node
  199. *
  200. * @man: Pointer to a memory type manager.
  201. * @mem: Pointer to a struct ttm_mem_reg to be filled in.
  202. *
  203. * This function frees memory type resources previously allocated
  204. * and that are identified by @mem::mm_node and @mem::start. May not
  205. * be called from within atomic context.
  206. */
  207. void (*put_node)(struct ttm_mem_type_manager *man,
  208. struct ttm_mem_reg *mem);
  209. /**
  210. * struct ttm_mem_type_manager member debug
  211. *
  212. * @man: Pointer to a memory type manager.
  213. * @prefix: Prefix to be used in printout to identify the caller.
  214. *
  215. * This function is called to print out the state of the memory
  216. * type manager to aid debugging of out-of-memory conditions.
  217. * It may not be called from within atomic context.
  218. */
  219. void (*debug)(struct ttm_mem_type_manager *man, const char *prefix);
  220. };
  221. /**
  222. * struct ttm_mem_type_manager
  223. *
  224. * @has_type: The memory type has been initialized.
  225. * @use_type: The memory type is enabled.
  226. * @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory
  227. * managed by this memory type.
  228. * @gpu_offset: If used, the GPU offset of the first managed page of
  229. * fixed memory or the first managed location in an aperture.
  230. * @size: Size of the managed region.
  231. * @available_caching: A mask of available caching types, TTM_PL_FLAG_XX,
  232. * as defined in ttm_placement_common.h
  233. * @default_caching: The default caching policy used for a buffer object
  234. * placed in this memory type if the user doesn't provide one.
  235. * @func: structure pointer implementing the range manager. See above
  236. * @priv: Driver private closure for @func.
  237. * @io_reserve_mutex: Mutex optionally protecting shared io_reserve structures
  238. * @use_io_reserve_lru: Use an lru list to try to unreserve io_mem_regions
  239. * reserved by the TTM vm system.
  240. * @io_reserve_lru: Optional lru list for unreserving io mem regions.
  241. * @io_reserve_fastpath: Only use bdev::driver::io_mem_reserve to obtain
  242. * @move_lock: lock for move fence
  243. * static information. bdev::driver::io_mem_free is never used.
  244. * @lru: The lru list for this memory type.
  245. * @move: The fence of the last pipelined move operation.
  246. *
  247. * This structure is used to identify and manage memory types for a device.
  248. * It's set up by the ttm_bo_driver::init_mem_type method.
  249. */
  250. struct ttm_mem_type_manager {
  251. struct ttm_bo_device *bdev;
  252. /*
  253. * No protection. Constant from start.
  254. */
  255. bool has_type;
  256. bool use_type;
  257. uint32_t flags;
  258. uint64_t gpu_offset; /* GPU address space is independent of CPU word size */
  259. uint64_t size;
  260. uint32_t available_caching;
  261. uint32_t default_caching;
  262. const struct ttm_mem_type_manager_func *func;
  263. void *priv;
  264. struct mutex io_reserve_mutex;
  265. bool use_io_reserve_lru;
  266. bool io_reserve_fastpath;
  267. spinlock_t move_lock;
  268. /*
  269. * Protected by @io_reserve_mutex:
  270. */
  271. struct list_head io_reserve_lru;
  272. /*
  273. * Protected by the global->lru_lock.
  274. */
  275. struct list_head lru;
  276. /*
  277. * Protected by @move_lock.
  278. */
  279. struct dma_fence *move;
  280. };
  281. /**
  282. * struct ttm_bo_driver
  283. *
  284. * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
  285. * @invalidate_caches: Callback to invalidate read caches when a buffer object
  286. * has been evicted.
  287. * @init_mem_type: Callback to initialize a struct ttm_mem_type_manager
  288. * structure.
  289. * @evict_flags: Callback to obtain placement flags when a buffer is evicted.
  290. * @move: Callback for a driver to hook in accelerated functions to
  291. * move a buffer.
  292. * If set to NULL, a potentially slow memcpy() move is used.
  293. */
  294. struct ttm_bo_driver {
  295. /**
  296. * ttm_tt_create
  297. *
  298. * @bdev: pointer to a struct ttm_bo_device:
  299. * @size: Size of the data needed backing.
  300. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  301. * @dummy_read_page: See struct ttm_bo_device.
  302. *
  303. * Create a struct ttm_tt to back data with system memory pages.
  304. * No pages are actually allocated.
  305. * Returns:
  306. * NULL: Out of memory.
  307. */
  308. struct ttm_tt *(*ttm_tt_create)(struct ttm_bo_device *bdev,
  309. unsigned long size,
  310. uint32_t page_flags,
  311. struct page *dummy_read_page);
  312. /**
  313. * ttm_tt_populate
  314. *
  315. * @ttm: The struct ttm_tt to contain the backing pages.
  316. *
  317. * Allocate all backing pages
  318. * Returns:
  319. * -ENOMEM: Out of memory.
  320. */
  321. int (*ttm_tt_populate)(struct ttm_tt *ttm);
  322. /**
  323. * ttm_tt_unpopulate
  324. *
  325. * @ttm: The struct ttm_tt to contain the backing pages.
  326. *
  327. * Free all backing page
  328. */
  329. void (*ttm_tt_unpopulate)(struct ttm_tt *ttm);
  330. /**
  331. * struct ttm_bo_driver member invalidate_caches
  332. *
  333. * @bdev: the buffer object device.
  334. * @flags: new placement of the rebound buffer object.
  335. *
  336. * A previosly evicted buffer has been rebound in a
  337. * potentially new location. Tell the driver that it might
  338. * consider invalidating read (texture) caches on the next command
  339. * submission as a consequence.
  340. */
  341. int (*invalidate_caches)(struct ttm_bo_device *bdev, uint32_t flags);
  342. int (*init_mem_type)(struct ttm_bo_device *bdev, uint32_t type,
  343. struct ttm_mem_type_manager *man);
  344. /**
  345. * struct ttm_bo_driver member eviction_valuable
  346. *
  347. * @bo: the buffer object to be evicted
  348. * @place: placement we need room for
  349. *
  350. * Check with the driver if it is valuable to evict a BO to make room
  351. * for a certain placement.
  352. */
  353. bool (*eviction_valuable)(struct ttm_buffer_object *bo,
  354. const struct ttm_place *place);
  355. /**
  356. * struct ttm_bo_driver member evict_flags:
  357. *
  358. * @bo: the buffer object to be evicted
  359. *
  360. * Return the bo flags for a buffer which is not mapped to the hardware.
  361. * These will be placed in proposed_flags so that when the move is
  362. * finished, they'll end up in bo->mem.flags
  363. */
  364. void (*evict_flags)(struct ttm_buffer_object *bo,
  365. struct ttm_placement *placement);
  366. /**
  367. * struct ttm_bo_driver member move:
  368. *
  369. * @bo: the buffer to move
  370. * @evict: whether this motion is evicting the buffer from
  371. * the graphics address space
  372. * @interruptible: Use interruptible sleeps if possible when sleeping.
  373. * @no_wait: whether this should give up and return -EBUSY
  374. * if this move would require sleeping
  375. * @new_mem: the new memory region receiving the buffer
  376. *
  377. * Move a buffer between two memory regions.
  378. */
  379. int (*move)(struct ttm_buffer_object *bo, bool evict,
  380. bool interruptible, bool no_wait_gpu,
  381. struct ttm_mem_reg *new_mem);
  382. /**
  383. * struct ttm_bo_driver_member verify_access
  384. *
  385. * @bo: Pointer to a buffer object.
  386. * @filp: Pointer to a struct file trying to access the object.
  387. *
  388. * Called from the map / write / read methods to verify that the
  389. * caller is permitted to access the buffer object.
  390. * This member may be set to NULL, which will refuse this kind of
  391. * access for all buffer objects.
  392. * This function should return 0 if access is granted, -EPERM otherwise.
  393. */
  394. int (*verify_access)(struct ttm_buffer_object *bo,
  395. struct file *filp);
  396. /**
  397. * Hook to notify driver about a driver move so it
  398. * can do tiling things and book-keeping.
  399. *
  400. * @evict: whether this move is evicting the buffer from the graphics
  401. * address space
  402. */
  403. void (*move_notify)(struct ttm_buffer_object *bo,
  404. bool evict,
  405. struct ttm_mem_reg *new_mem);
  406. /* notify the driver we are taking a fault on this BO
  407. * and have reserved it */
  408. int (*fault_reserve_notify)(struct ttm_buffer_object *bo);
  409. /**
  410. * notify the driver that we're about to swap out this bo
  411. */
  412. void (*swap_notify)(struct ttm_buffer_object *bo);
  413. /**
  414. * Driver callback on when mapping io memory (for bo_move_memcpy
  415. * for instance). TTM will take care to call io_mem_free whenever
  416. * the mapping is not use anymore. io_mem_reserve & io_mem_free
  417. * are balanced.
  418. */
  419. int (*io_mem_reserve)(struct ttm_bo_device *bdev,
  420. struct ttm_mem_reg *mem);
  421. void (*io_mem_free)(struct ttm_bo_device *bdev,
  422. struct ttm_mem_reg *mem);
  423. /**
  424. * Optional driver callback for when BO is removed from the LRU.
  425. * Called with LRU lock held immediately before the removal.
  426. */
  427. void (*lru_removal)(struct ttm_buffer_object *bo);
  428. /**
  429. * Return the list_head after which a BO should be inserted in the LRU.
  430. */
  431. struct list_head *(*lru_tail)(struct ttm_buffer_object *bo);
  432. struct list_head *(*swap_lru_tail)(struct ttm_buffer_object *bo);
  433. };
  434. /**
  435. * struct ttm_bo_global_ref - Argument to initialize a struct ttm_bo_global.
  436. */
  437. struct ttm_bo_global_ref {
  438. struct drm_global_reference ref;
  439. struct ttm_mem_global *mem_glob;
  440. };
  441. /**
  442. * struct ttm_bo_global - Buffer object driver global data.
  443. *
  444. * @mem_glob: Pointer to a struct ttm_mem_global object for accounting.
  445. * @dummy_read_page: Pointer to a dummy page used for mapping requests
  446. * of unpopulated pages.
  447. * @shrink: A shrink callback object used for buffer object swap.
  448. * @device_list_mutex: Mutex protecting the device list.
  449. * This mutex is held while traversing the device list for pm options.
  450. * @lru_lock: Spinlock protecting the bo subsystem lru lists.
  451. * @device_list: List of buffer object devices.
  452. * @swap_lru: Lru list of buffer objects used for swapping.
  453. */
  454. struct ttm_bo_global {
  455. /**
  456. * Constant after init.
  457. */
  458. struct kobject kobj;
  459. struct ttm_mem_global *mem_glob;
  460. struct page *dummy_read_page;
  461. struct ttm_mem_shrink shrink;
  462. struct mutex device_list_mutex;
  463. spinlock_t lru_lock;
  464. /**
  465. * Protected by device_list_mutex.
  466. */
  467. struct list_head device_list;
  468. /**
  469. * Protected by the lru_lock.
  470. */
  471. struct list_head swap_lru;
  472. /**
  473. * Internal protection.
  474. */
  475. atomic_t bo_count;
  476. };
  477. #define TTM_NUM_MEM_TYPES 8
  478. /**
  479. * struct ttm_bo_device - Buffer object driver device-specific data.
  480. *
  481. * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
  482. * @man: An array of mem_type_managers.
  483. * @vma_manager: Address space manager
  484. * lru_lock: Spinlock that protects the buffer+device lru lists and
  485. * ddestroy lists.
  486. * @dev_mapping: A pointer to the struct address_space representing the
  487. * device address space.
  488. * @wq: Work queue structure for the delayed delete workqueue.
  489. *
  490. */
  491. struct ttm_bo_device {
  492. /*
  493. * Constant after bo device init / atomic.
  494. */
  495. struct list_head device_list;
  496. struct ttm_bo_global *glob;
  497. struct ttm_bo_driver *driver;
  498. struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];
  499. /*
  500. * Protected by internal locks.
  501. */
  502. struct drm_vma_offset_manager vma_manager;
  503. /*
  504. * Protected by the global:lru lock.
  505. */
  506. struct list_head ddestroy;
  507. /*
  508. * Protected by load / firstopen / lastclose /unload sync.
  509. */
  510. struct address_space *dev_mapping;
  511. /*
  512. * Internal protection.
  513. */
  514. struct delayed_work wq;
  515. bool need_dma32;
  516. };
  517. /**
  518. * ttm_flag_masked
  519. *
  520. * @old: Pointer to the result and original value.
  521. * @new: New value of bits.
  522. * @mask: Mask of bits to change.
  523. *
  524. * Convenience function to change a number of bits identified by a mask.
  525. */
  526. static inline uint32_t
  527. ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
  528. {
  529. *old ^= (*old ^ new) & mask;
  530. return *old;
  531. }
  532. /**
  533. * ttm_tt_init
  534. *
  535. * @ttm: The struct ttm_tt.
  536. * @bdev: pointer to a struct ttm_bo_device:
  537. * @size: Size of the data needed backing.
  538. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  539. * @dummy_read_page: See struct ttm_bo_device.
  540. *
  541. * Create a struct ttm_tt to back data with system memory pages.
  542. * No pages are actually allocated.
  543. * Returns:
  544. * NULL: Out of memory.
  545. */
  546. extern int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
  547. unsigned long size, uint32_t page_flags,
  548. struct page *dummy_read_page);
  549. extern int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
  550. unsigned long size, uint32_t page_flags,
  551. struct page *dummy_read_page);
  552. /**
  553. * ttm_tt_fini
  554. *
  555. * @ttm: the ttm_tt structure.
  556. *
  557. * Free memory of ttm_tt structure
  558. */
  559. extern void ttm_tt_fini(struct ttm_tt *ttm);
  560. extern void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma);
  561. /**
  562. * ttm_ttm_bind:
  563. *
  564. * @ttm: The struct ttm_tt containing backing pages.
  565. * @bo_mem: The struct ttm_mem_reg identifying the binding location.
  566. *
  567. * Bind the pages of @ttm to an aperture location identified by @bo_mem
  568. */
  569. extern int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
  570. /**
  571. * ttm_ttm_destroy:
  572. *
  573. * @ttm: The struct ttm_tt.
  574. *
  575. * Unbind, unpopulate and destroy common struct ttm_tt.
  576. */
  577. extern void ttm_tt_destroy(struct ttm_tt *ttm);
  578. /**
  579. * ttm_ttm_unbind:
  580. *
  581. * @ttm: The struct ttm_tt.
  582. *
  583. * Unbind a struct ttm_tt.
  584. */
  585. extern void ttm_tt_unbind(struct ttm_tt *ttm);
  586. /**
  587. * ttm_tt_swapin:
  588. *
  589. * @ttm: The struct ttm_tt.
  590. *
  591. * Swap in a previously swap out ttm_tt.
  592. */
  593. extern int ttm_tt_swapin(struct ttm_tt *ttm);
  594. /**
  595. * ttm_tt_set_placement_caching:
  596. *
  597. * @ttm A struct ttm_tt the backing pages of which will change caching policy.
  598. * @placement: Flag indicating the desired caching policy.
  599. *
  600. * This function will change caching policy of any default kernel mappings of
  601. * the pages backing @ttm. If changing from cached to uncached or
  602. * write-combined,
  603. * all CPU caches will first be flushed to make sure the data of the pages
  604. * hit RAM. This function may be very costly as it involves global TLB
  605. * and cache flushes and potential page splitting / combining.
  606. */
  607. extern int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);
  608. extern int ttm_tt_swapout(struct ttm_tt *ttm,
  609. struct file *persistent_swap_storage);
  610. /**
  611. * ttm_tt_unpopulate - free pages from a ttm
  612. *
  613. * @ttm: Pointer to the ttm_tt structure
  614. *
  615. * Calls the driver method to free all pages from a ttm
  616. */
  617. extern void ttm_tt_unpopulate(struct ttm_tt *ttm);
  618. /*
  619. * ttm_bo.c
  620. */
  621. /**
  622. * ttm_mem_reg_is_pci
  623. *
  624. * @bdev: Pointer to a struct ttm_bo_device.
  625. * @mem: A valid struct ttm_mem_reg.
  626. *
  627. * Returns true if the memory described by @mem is PCI memory,
  628. * false otherwise.
  629. */
  630. extern bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev,
  631. struct ttm_mem_reg *mem);
  632. /**
  633. * ttm_bo_mem_space
  634. *
  635. * @bo: Pointer to a struct ttm_buffer_object. the data of which
  636. * we want to allocate space for.
  637. * @proposed_placement: Proposed new placement for the buffer object.
  638. * @mem: A struct ttm_mem_reg.
  639. * @interruptible: Sleep interruptible when sliping.
  640. * @no_wait_gpu: Return immediately if the GPU is busy.
  641. *
  642. * Allocate memory space for the buffer object pointed to by @bo, using
  643. * the placement flags in @mem, potentially evicting other idle buffer objects.
  644. * This function may sleep while waiting for space to become available.
  645. * Returns:
  646. * -EBUSY: No space available (only if no_wait == 1).
  647. * -ENOMEM: Could not allocate memory for the buffer object, either due to
  648. * fragmentation or concurrent allocators.
  649. * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
  650. */
  651. extern int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  652. struct ttm_placement *placement,
  653. struct ttm_mem_reg *mem,
  654. bool interruptible,
  655. bool no_wait_gpu);
  656. extern void ttm_bo_mem_put(struct ttm_buffer_object *bo,
  657. struct ttm_mem_reg *mem);
  658. extern void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo,
  659. struct ttm_mem_reg *mem);
  660. extern void ttm_bo_global_release(struct drm_global_reference *ref);
  661. extern int ttm_bo_global_init(struct drm_global_reference *ref);
  662. extern int ttm_bo_device_release(struct ttm_bo_device *bdev);
  663. /**
  664. * ttm_bo_device_init
  665. *
  666. * @bdev: A pointer to a struct ttm_bo_device to initialize.
  667. * @glob: A pointer to an initialized struct ttm_bo_global.
  668. * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
  669. * @mapping: The address space to use for this bo.
  670. * @file_page_offset: Offset into the device address space that is available
  671. * for buffer data. This ensures compatibility with other users of the
  672. * address space.
  673. *
  674. * Initializes a struct ttm_bo_device:
  675. * Returns:
  676. * !0: Failure.
  677. */
  678. extern int ttm_bo_device_init(struct ttm_bo_device *bdev,
  679. struct ttm_bo_global *glob,
  680. struct ttm_bo_driver *driver,
  681. struct address_space *mapping,
  682. uint64_t file_page_offset, bool need_dma32);
  683. /**
  684. * ttm_bo_unmap_virtual
  685. *
  686. * @bo: tear down the virtual mappings for this BO
  687. */
  688. extern void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
  689. /**
  690. * ttm_bo_unmap_virtual
  691. *
  692. * @bo: tear down the virtual mappings for this BO
  693. *
  694. * The caller must take ttm_mem_io_lock before calling this function.
  695. */
  696. extern void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo);
  697. extern int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo);
  698. extern void ttm_mem_io_free_vm(struct ttm_buffer_object *bo);
  699. extern int ttm_mem_io_lock(struct ttm_mem_type_manager *man,
  700. bool interruptible);
  701. extern void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
  702. extern void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo);
  703. extern void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);
  704. struct list_head *ttm_bo_default_lru_tail(struct ttm_buffer_object *bo);
  705. struct list_head *ttm_bo_default_swap_lru_tail(struct ttm_buffer_object *bo);
  706. /**
  707. * __ttm_bo_reserve:
  708. *
  709. * @bo: A pointer to a struct ttm_buffer_object.
  710. * @interruptible: Sleep interruptible if waiting.
  711. * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  712. * @ticket: ticket used to acquire the ww_mutex.
  713. *
  714. * Will not remove reserved buffers from the lru lists.
  715. * Otherwise identical to ttm_bo_reserve.
  716. *
  717. * Returns:
  718. * -EDEADLK: The reservation may cause a deadlock.
  719. * Release all buffer reservations, wait for @bo to become unreserved and
  720. * try again. (only if use_sequence == 1).
  721. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  722. * a signal. Release all buffer reservations and return to user-space.
  723. * -EBUSY: The function needed to sleep, but @no_wait was true
  724. * -EALREADY: Bo already reserved using @ticket. This error code will only
  725. * be returned if @use_ticket is set to true.
  726. */
  727. static inline int __ttm_bo_reserve(struct ttm_buffer_object *bo,
  728. bool interruptible, bool no_wait,
  729. struct ww_acquire_ctx *ticket)
  730. {
  731. int ret = 0;
  732. if (no_wait) {
  733. bool success;
  734. if (WARN_ON(ticket))
  735. return -EBUSY;
  736. success = ww_mutex_trylock(&bo->resv->lock);
  737. return success ? 0 : -EBUSY;
  738. }
  739. if (interruptible)
  740. ret = ww_mutex_lock_interruptible(&bo->resv->lock, ticket);
  741. else
  742. ret = ww_mutex_lock(&bo->resv->lock, ticket);
  743. if (ret == -EINTR)
  744. return -ERESTARTSYS;
  745. return ret;
  746. }
  747. /**
  748. * ttm_bo_reserve:
  749. *
  750. * @bo: A pointer to a struct ttm_buffer_object.
  751. * @interruptible: Sleep interruptible if waiting.
  752. * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  753. * @ticket: ticket used to acquire the ww_mutex.
  754. *
  755. * Locks a buffer object for validation. (Or prevents other processes from
  756. * locking it for validation) and removes it from lru lists, while taking
  757. * a number of measures to prevent deadlocks.
  758. *
  759. * Deadlocks may occur when two processes try to reserve multiple buffers in
  760. * different order, either by will or as a result of a buffer being evicted
  761. * to make room for a buffer already reserved. (Buffers are reserved before
  762. * they are evicted). The following algorithm prevents such deadlocks from
  763. * occurring:
  764. * Processes attempting to reserve multiple buffers other than for eviction,
  765. * (typically execbuf), should first obtain a unique 32-bit
  766. * validation sequence number,
  767. * and call this function with @use_ticket == 1 and @ticket->stamp == the unique
  768. * sequence number. If upon call of this function, the buffer object is already
  769. * reserved, the validation sequence is checked against the validation
  770. * sequence of the process currently reserving the buffer,
  771. * and if the current validation sequence is greater than that of the process
  772. * holding the reservation, the function returns -EDEADLK. Otherwise it sleeps
  773. * waiting for the buffer to become unreserved, after which it retries
  774. * reserving.
  775. * The caller should, when receiving an -EDEADLK error
  776. * release all its buffer reservations, wait for @bo to become unreserved, and
  777. * then rerun the validation with the same validation sequence. This procedure
  778. * will always guarantee that the process with the lowest validation sequence
  779. * will eventually succeed, preventing both deadlocks and starvation.
  780. *
  781. * Returns:
  782. * -EDEADLK: The reservation may cause a deadlock.
  783. * Release all buffer reservations, wait for @bo to become unreserved and
  784. * try again. (only if use_sequence == 1).
  785. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  786. * a signal. Release all buffer reservations and return to user-space.
  787. * -EBUSY: The function needed to sleep, but @no_wait was true
  788. * -EALREADY: Bo already reserved using @ticket. This error code will only
  789. * be returned if @use_ticket is set to true.
  790. */
  791. static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
  792. bool interruptible, bool no_wait,
  793. struct ww_acquire_ctx *ticket)
  794. {
  795. int ret;
  796. WARN_ON(!atomic_read(&bo->kref.refcount));
  797. ret = __ttm_bo_reserve(bo, interruptible, no_wait, ticket);
  798. if (likely(ret == 0))
  799. ttm_bo_del_sub_from_lru(bo);
  800. return ret;
  801. }
  802. /**
  803. * ttm_bo_reserve_slowpath:
  804. * @bo: A pointer to a struct ttm_buffer_object.
  805. * @interruptible: Sleep interruptible if waiting.
  806. * @sequence: Set (@bo)->sequence to this value after lock
  807. *
  808. * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
  809. * from all our other reservations. Because there are no other reservations
  810. * held by us, this function cannot deadlock any more.
  811. */
  812. static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
  813. bool interruptible,
  814. struct ww_acquire_ctx *ticket)
  815. {
  816. int ret = 0;
  817. WARN_ON(!atomic_read(&bo->kref.refcount));
  818. if (interruptible)
  819. ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
  820. ticket);
  821. else
  822. ww_mutex_lock_slow(&bo->resv->lock, ticket);
  823. if (likely(ret == 0))
  824. ttm_bo_del_sub_from_lru(bo);
  825. else if (ret == -EINTR)
  826. ret = -ERESTARTSYS;
  827. return ret;
  828. }
  829. /**
  830. * __ttm_bo_unreserve
  831. * @bo: A pointer to a struct ttm_buffer_object.
  832. *
  833. * Unreserve a previous reservation of @bo where the buffer object is
  834. * already on lru lists.
  835. */
  836. static inline void __ttm_bo_unreserve(struct ttm_buffer_object *bo)
  837. {
  838. ww_mutex_unlock(&bo->resv->lock);
  839. }
  840. /**
  841. * ttm_bo_unreserve
  842. *
  843. * @bo: A pointer to a struct ttm_buffer_object.
  844. *
  845. * Unreserve a previous reservation of @bo.
  846. */
  847. static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
  848. {
  849. if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  850. spin_lock(&bo->glob->lru_lock);
  851. ttm_bo_add_to_lru(bo);
  852. spin_unlock(&bo->glob->lru_lock);
  853. }
  854. __ttm_bo_unreserve(bo);
  855. }
  856. /**
  857. * ttm_bo_unreserve_ticket
  858. * @bo: A pointer to a struct ttm_buffer_object.
  859. * @ticket: ww_acquire_ctx used for reserving
  860. *
  861. * Unreserve a previous reservation of @bo made with @ticket.
  862. */
  863. static inline void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo,
  864. struct ww_acquire_ctx *t)
  865. {
  866. ttm_bo_unreserve(bo);
  867. }
  868. /*
  869. * ttm_bo_util.c
  870. */
  871. int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
  872. struct ttm_mem_reg *mem);
  873. void ttm_mem_io_free(struct ttm_bo_device *bdev,
  874. struct ttm_mem_reg *mem);
  875. /**
  876. * ttm_bo_move_ttm
  877. *
  878. * @bo: A pointer to a struct ttm_buffer_object.
  879. * @interruptible: Sleep interruptible if waiting.
  880. * @no_wait_gpu: Return immediately if the GPU is busy.
  881. * @new_mem: struct ttm_mem_reg indicating where to move.
  882. *
  883. * Optimized move function for a buffer object with both old and
  884. * new placement backed by a TTM. The function will, if successful,
  885. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  886. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  887. * data remains untouched, and it's up to the caller to free the
  888. * memory space indicated by @new_mem.
  889. * Returns:
  890. * !0: Failure.
  891. */
  892. extern int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
  893. bool interruptible, bool no_wait_gpu,
  894. struct ttm_mem_reg *new_mem);
  895. /**
  896. * ttm_bo_move_memcpy
  897. *
  898. * @bo: A pointer to a struct ttm_buffer_object.
  899. * @interruptible: Sleep interruptible if waiting.
  900. * @no_wait_gpu: Return immediately if the GPU is busy.
  901. * @new_mem: struct ttm_mem_reg indicating where to move.
  902. *
  903. * Fallback move function for a mappable buffer object in mappable memory.
  904. * The function will, if successful,
  905. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  906. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  907. * data remains untouched, and it's up to the caller to free the
  908. * memory space indicated by @new_mem.
  909. * Returns:
  910. * !0: Failure.
  911. */
  912. extern int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  913. bool interruptible, bool no_wait_gpu,
  914. struct ttm_mem_reg *new_mem);
  915. /**
  916. * ttm_bo_free_old_node
  917. *
  918. * @bo: A pointer to a struct ttm_buffer_object.
  919. *
  920. * Utility function to free an old placement after a successful move.
  921. */
  922. extern void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
  923. /**
  924. * ttm_bo_move_accel_cleanup.
  925. *
  926. * @bo: A pointer to a struct ttm_buffer_object.
  927. * @fence: A fence object that signals when moving is complete.
  928. * @evict: This is an evict move. Don't return until the buffer is idle.
  929. * @new_mem: struct ttm_mem_reg indicating where to move.
  930. *
  931. * Accelerated move function to be called when an accelerated move
  932. * has been scheduled. The function will create a new temporary buffer object
  933. * representing the old placement, and put the sync object on both buffer
  934. * objects. After that the newly created buffer object is unref'd to be
  935. * destroyed when the move is complete. This will help pipeline
  936. * buffer moves.
  937. */
  938. extern int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  939. struct dma_fence *fence, bool evict,
  940. struct ttm_mem_reg *new_mem);
  941. /**
  942. * ttm_bo_pipeline_move.
  943. *
  944. * @bo: A pointer to a struct ttm_buffer_object.
  945. * @fence: A fence object that signals when moving is complete.
  946. * @evict: This is an evict move. Don't return until the buffer is idle.
  947. * @new_mem: struct ttm_mem_reg indicating where to move.
  948. *
  949. * Function for pipelining accelerated moves. Either free the memory
  950. * immediately or hang it on a temporary buffer object.
  951. */
  952. int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
  953. struct dma_fence *fence, bool evict,
  954. struct ttm_mem_reg *new_mem);
  955. /**
  956. * ttm_io_prot
  957. *
  958. * @c_state: Caching state.
  959. * @tmp: Page protection flag for a normal, cached mapping.
  960. *
  961. * Utility function that returns the pgprot_t that should be used for
  962. * setting up a PTE with the caching model indicated by @c_state.
  963. */
  964. extern pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);
  965. extern const struct ttm_mem_type_manager_func ttm_bo_manager_func;
  966. #if IS_ENABLED(CONFIG_AGP)
  967. #include <linux/agp_backend.h>
  968. /**
  969. * ttm_agp_tt_create
  970. *
  971. * @bdev: Pointer to a struct ttm_bo_device.
  972. * @bridge: The agp bridge this device is sitting on.
  973. * @size: Size of the data needed backing.
  974. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  975. * @dummy_read_page: See struct ttm_bo_device.
  976. *
  977. *
  978. * Create a TTM backend that uses the indicated AGP bridge as an aperture
  979. * for TT memory. This function uses the linux agpgart interface to
  980. * bind and unbind memory backing a ttm_tt.
  981. */
  982. extern struct ttm_tt *ttm_agp_tt_create(struct ttm_bo_device *bdev,
  983. struct agp_bridge_data *bridge,
  984. unsigned long size, uint32_t page_flags,
  985. struct page *dummy_read_page);
  986. int ttm_agp_tt_populate(struct ttm_tt *ttm);
  987. void ttm_agp_tt_unpopulate(struct ttm_tt *ttm);
  988. #endif
  989. #endif