ttm_bo_driver.h 33 KB

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