ttm_bo_driver.h 33 KB

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