ttm_bo_driver.h 33 KB

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