ttm_bo_driver.h 34 KB

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