ttm_bo_driver.h 33 KB

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