ttm_bo_driver.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29. */
  30. #ifndef _TTM_BO_DRIVER_H_
  31. #define _TTM_BO_DRIVER_H_
  32. #include <ttm/ttm_bo_api.h>
  33. #include <ttm/ttm_memory.h>
  34. #include <ttm/ttm_module.h>
  35. #include <ttm/ttm_placement.h>
  36. #include <drm/drm_mm.h>
  37. #include <drm/drm_global.h>
  38. #include <drm/drm_vma_manager.h>
  39. #include <linux/workqueue.h>
  40. #include <linux/fs.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/reservation.h>
  43. #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. * @prefix: 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, const char *prefix);
  221. };
  222. /**
  223. * struct ttm_mem_type_manager
  224. *
  225. * @has_type: The memory type has been initialized.
  226. * @use_type: The memory type is enabled.
  227. * @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory
  228. * managed by this memory type.
  229. * @gpu_offset: If used, the GPU offset of the first managed page of
  230. * fixed memory or the first managed location in an aperture.
  231. * @size: Size of the managed region.
  232. * @available_caching: A mask of available caching types, TTM_PL_FLAG_XX,
  233. * as defined in ttm_placement_common.h
  234. * @default_caching: The default caching policy used for a buffer object
  235. * placed in this memory type if the user doesn't provide one.
  236. * @func: structure pointer implementing the range manager. See above
  237. * @priv: Driver private closure for @func.
  238. * @io_reserve_mutex: Mutex optionally protecting shared io_reserve structures
  239. * @use_io_reserve_lru: Use an lru list to try to unreserve io_mem_regions
  240. * reserved by the TTM vm system.
  241. * @io_reserve_lru: Optional lru list for unreserving io mem regions.
  242. * @io_reserve_fastpath: Only use bdev::driver::io_mem_reserve to obtain
  243. * @move_lock: lock for move fence
  244. * static information. bdev::driver::io_mem_free is never used.
  245. * @lru: The lru list for this memory type.
  246. * @move: The fence of the last pipelined move operation.
  247. *
  248. * This structure is used to identify and manage memory types for a device.
  249. * It's set up by the ttm_bo_driver::init_mem_type method.
  250. */
  251. struct ttm_mem_type_manager {
  252. struct ttm_bo_device *bdev;
  253. /*
  254. * No protection. Constant from start.
  255. */
  256. bool has_type;
  257. bool use_type;
  258. uint32_t flags;
  259. uint64_t gpu_offset; /* GPU address space is independent of CPU word size */
  260. uint64_t size;
  261. uint32_t available_caching;
  262. uint32_t default_caching;
  263. const struct ttm_mem_type_manager_func *func;
  264. void *priv;
  265. struct mutex io_reserve_mutex;
  266. bool use_io_reserve_lru;
  267. bool io_reserve_fastpath;
  268. spinlock_t move_lock;
  269. /*
  270. * Protected by @io_reserve_mutex:
  271. */
  272. struct list_head io_reserve_lru;
  273. /*
  274. * Protected by the global->lru_lock.
  275. */
  276. struct list_head lru[TTM_MAX_BO_PRIORITY];
  277. /*
  278. * Protected by @move_lock.
  279. */
  280. struct dma_fence *move;
  281. };
  282. /**
  283. * struct ttm_bo_driver
  284. *
  285. * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
  286. * @invalidate_caches: Callback to invalidate read caches when a buffer object
  287. * has been evicted.
  288. * @init_mem_type: Callback to initialize a struct ttm_mem_type_manager
  289. * structure.
  290. * @evict_flags: Callback to obtain placement flags when a buffer is evicted.
  291. * @move: Callback for a driver to hook in accelerated functions to
  292. * move a buffer.
  293. * If set to NULL, a potentially slow memcpy() move is used.
  294. */
  295. struct ttm_bo_driver {
  296. /**
  297. * ttm_tt_create
  298. *
  299. * @bdev: pointer to a struct ttm_bo_device:
  300. * @size: Size of the data needed backing.
  301. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  302. * @dummy_read_page: See struct ttm_bo_device.
  303. *
  304. * Create a struct ttm_tt to back data with system memory pages.
  305. * No pages are actually allocated.
  306. * Returns:
  307. * NULL: Out of memory.
  308. */
  309. struct ttm_tt *(*ttm_tt_create)(struct ttm_bo_device *bdev,
  310. unsigned long size,
  311. uint32_t page_flags,
  312. struct page *dummy_read_page);
  313. /**
  314. * ttm_tt_populate
  315. *
  316. * @ttm: The struct ttm_tt to contain the backing pages.
  317. *
  318. * Allocate all backing pages
  319. * Returns:
  320. * -ENOMEM: Out of memory.
  321. */
  322. int (*ttm_tt_populate)(struct ttm_tt *ttm);
  323. /**
  324. * ttm_tt_unpopulate
  325. *
  326. * @ttm: The struct ttm_tt to contain the backing pages.
  327. *
  328. * Free all backing page
  329. */
  330. void (*ttm_tt_unpopulate)(struct ttm_tt *ttm);
  331. /**
  332. * struct ttm_bo_driver member invalidate_caches
  333. *
  334. * @bdev: the buffer object device.
  335. * @flags: new placement of the rebound buffer object.
  336. *
  337. * A previosly evicted buffer has been rebound in a
  338. * potentially new location. Tell the driver that it might
  339. * consider invalidating read (texture) caches on the next command
  340. * submission as a consequence.
  341. */
  342. int (*invalidate_caches)(struct ttm_bo_device *bdev, uint32_t flags);
  343. int (*init_mem_type)(struct ttm_bo_device *bdev, uint32_t type,
  344. struct ttm_mem_type_manager *man);
  345. /**
  346. * struct ttm_bo_driver member eviction_valuable
  347. *
  348. * @bo: the buffer object to be evicted
  349. * @place: placement we need room for
  350. *
  351. * Check with the driver if it is valuable to evict a BO to make room
  352. * for a certain placement.
  353. */
  354. bool (*eviction_valuable)(struct ttm_buffer_object *bo,
  355. const struct ttm_place *place);
  356. /**
  357. * struct ttm_bo_driver member evict_flags:
  358. *
  359. * @bo: the buffer object to be evicted
  360. *
  361. * Return the bo flags for a buffer which is not mapped to the hardware.
  362. * These will be placed in proposed_flags so that when the move is
  363. * finished, they'll end up in bo->mem.flags
  364. */
  365. void (*evict_flags)(struct ttm_buffer_object *bo,
  366. struct ttm_placement *placement);
  367. /**
  368. * struct ttm_bo_driver member move:
  369. *
  370. * @bo: the buffer to move
  371. * @evict: whether this motion is evicting the buffer from
  372. * the graphics address space
  373. * @interruptible: Use interruptible sleeps if possible when sleeping.
  374. * @no_wait: whether this should give up and return -EBUSY
  375. * if this move would require sleeping
  376. * @new_mem: the new memory region receiving the buffer
  377. *
  378. * Move a buffer between two memory regions.
  379. */
  380. int (*move)(struct ttm_buffer_object *bo, bool evict,
  381. bool interruptible, bool no_wait_gpu,
  382. struct ttm_mem_reg *new_mem);
  383. /**
  384. * struct ttm_bo_driver_member verify_access
  385. *
  386. * @bo: Pointer to a buffer object.
  387. * @filp: Pointer to a struct file trying to access the object.
  388. *
  389. * Called from the map / write / read methods to verify that the
  390. * caller is permitted to access the buffer object.
  391. * This member may be set to NULL, which will refuse this kind of
  392. * access for all buffer objects.
  393. * This function should return 0 if access is granted, -EPERM otherwise.
  394. */
  395. int (*verify_access)(struct ttm_buffer_object *bo,
  396. struct file *filp);
  397. /**
  398. * Hook to notify driver about a driver move so it
  399. * can do tiling things and book-keeping.
  400. *
  401. * @evict: whether this move is evicting the buffer from the graphics
  402. * address space
  403. */
  404. void (*move_notify)(struct ttm_buffer_object *bo,
  405. bool evict,
  406. struct ttm_mem_reg *new_mem);
  407. /* notify the driver we are taking a fault on this BO
  408. * and have reserved it */
  409. int (*fault_reserve_notify)(struct ttm_buffer_object *bo);
  410. /**
  411. * notify the driver that we're about to swap out this bo
  412. */
  413. void (*swap_notify)(struct ttm_buffer_object *bo);
  414. /**
  415. * Driver callback on when mapping io memory (for bo_move_memcpy
  416. * for instance). TTM will take care to call io_mem_free whenever
  417. * the mapping is not use anymore. io_mem_reserve & io_mem_free
  418. * are balanced.
  419. */
  420. int (*io_mem_reserve)(struct ttm_bo_device *bdev,
  421. struct ttm_mem_reg *mem);
  422. void (*io_mem_free)(struct ttm_bo_device *bdev,
  423. struct ttm_mem_reg *mem);
  424. /**
  425. * Return the pfn for a given page_offset inside the BO.
  426. *
  427. * @bo: the BO to look up the pfn for
  428. * @page_offset: the offset to look up
  429. */
  430. unsigned long (*io_mem_pfn)(struct ttm_buffer_object *bo,
  431. unsigned long page_offset);
  432. };
  433. /**
  434. * struct ttm_bo_global_ref - Argument to initialize a struct ttm_bo_global.
  435. */
  436. struct ttm_bo_global_ref {
  437. struct drm_global_reference ref;
  438. struct ttm_mem_global *mem_glob;
  439. };
  440. /**
  441. * struct ttm_bo_global - Buffer object driver global data.
  442. *
  443. * @mem_glob: Pointer to a struct ttm_mem_global object for accounting.
  444. * @dummy_read_page: Pointer to a dummy page used for mapping requests
  445. * of unpopulated pages.
  446. * @shrink: A shrink callback object used for buffer object swap.
  447. * @device_list_mutex: Mutex protecting the device list.
  448. * This mutex is held while traversing the device list for pm options.
  449. * @lru_lock: Spinlock protecting the bo subsystem lru lists.
  450. * @device_list: List of buffer object devices.
  451. * @swap_lru: Lru list of buffer objects used for swapping.
  452. */
  453. struct ttm_bo_global {
  454. /**
  455. * Constant after init.
  456. */
  457. struct kobject kobj;
  458. struct ttm_mem_global *mem_glob;
  459. struct page *dummy_read_page;
  460. struct ttm_mem_shrink shrink;
  461. struct mutex device_list_mutex;
  462. spinlock_t lru_lock;
  463. /**
  464. * Protected by device_list_mutex.
  465. */
  466. struct list_head device_list;
  467. /**
  468. * Protected by the lru_lock.
  469. */
  470. struct list_head swap_lru[TTM_MAX_BO_PRIORITY];
  471. /**
  472. * Internal protection.
  473. */
  474. atomic_t bo_count;
  475. };
  476. #define TTM_NUM_MEM_TYPES 8
  477. /**
  478. * struct ttm_bo_device - Buffer object driver device-specific data.
  479. *
  480. * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
  481. * @man: An array of mem_type_managers.
  482. * @vma_manager: Address space manager
  483. * lru_lock: Spinlock that protects the buffer+device lru lists and
  484. * ddestroy lists.
  485. * @dev_mapping: A pointer to the struct address_space representing the
  486. * device address space.
  487. * @wq: Work queue structure for the delayed delete workqueue.
  488. *
  489. */
  490. struct ttm_bo_device {
  491. /*
  492. * Constant after bo device init / atomic.
  493. */
  494. struct list_head device_list;
  495. struct ttm_bo_global *glob;
  496. struct ttm_bo_driver *driver;
  497. struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];
  498. /*
  499. * Protected by internal locks.
  500. */
  501. struct drm_vma_offset_manager vma_manager;
  502. /*
  503. * Protected by the global:lru lock.
  504. */
  505. struct list_head ddestroy;
  506. /*
  507. * Protected by load / firstopen / lastclose /unload sync.
  508. */
  509. struct address_space *dev_mapping;
  510. /*
  511. * Internal protection.
  512. */
  513. struct delayed_work wq;
  514. bool need_dma32;
  515. };
  516. /**
  517. * ttm_flag_masked
  518. *
  519. * @old: Pointer to the result and original value.
  520. * @new: New value of bits.
  521. * @mask: Mask of bits to change.
  522. *
  523. * Convenience function to change a number of bits identified by a mask.
  524. */
  525. static inline uint32_t
  526. ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
  527. {
  528. *old ^= (*old ^ new) & mask;
  529. return *old;
  530. }
  531. /**
  532. * ttm_tt_init
  533. *
  534. * @ttm: The struct ttm_tt.
  535. * @bdev: pointer to a struct ttm_bo_device:
  536. * @size: Size of the data needed backing.
  537. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  538. * @dummy_read_page: See struct ttm_bo_device.
  539. *
  540. * Create a struct ttm_tt to back data with system memory pages.
  541. * No pages are actually allocated.
  542. * Returns:
  543. * NULL: Out of memory.
  544. */
  545. extern int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
  546. unsigned long size, uint32_t page_flags,
  547. struct page *dummy_read_page);
  548. extern int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
  549. unsigned long size, uint32_t page_flags,
  550. struct page *dummy_read_page);
  551. /**
  552. * ttm_tt_fini
  553. *
  554. * @ttm: the ttm_tt structure.
  555. *
  556. * Free memory of ttm_tt structure
  557. */
  558. extern void ttm_tt_fini(struct ttm_tt *ttm);
  559. extern void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma);
  560. /**
  561. * ttm_ttm_bind:
  562. *
  563. * @ttm: The struct ttm_tt containing backing pages.
  564. * @bo_mem: The struct ttm_mem_reg identifying the binding location.
  565. *
  566. * Bind the pages of @ttm to an aperture location identified by @bo_mem
  567. */
  568. extern int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
  569. /**
  570. * ttm_ttm_destroy:
  571. *
  572. * @ttm: The struct ttm_tt.
  573. *
  574. * Unbind, unpopulate and destroy common struct ttm_tt.
  575. */
  576. extern void ttm_tt_destroy(struct ttm_tt *ttm);
  577. /**
  578. * ttm_ttm_unbind:
  579. *
  580. * @ttm: The struct ttm_tt.
  581. *
  582. * Unbind a struct ttm_tt.
  583. */
  584. extern void ttm_tt_unbind(struct ttm_tt *ttm);
  585. /**
  586. * ttm_tt_swapin:
  587. *
  588. * @ttm: The struct ttm_tt.
  589. *
  590. * Swap in a previously swap out ttm_tt.
  591. */
  592. extern int ttm_tt_swapin(struct ttm_tt *ttm);
  593. /**
  594. * ttm_tt_set_placement_caching:
  595. *
  596. * @ttm A struct ttm_tt the backing pages of which will change caching policy.
  597. * @placement: Flag indicating the desired caching policy.
  598. *
  599. * This function will change caching policy of any default kernel mappings of
  600. * the pages backing @ttm. If changing from cached to uncached or
  601. * write-combined,
  602. * all CPU caches will first be flushed to make sure the data of the pages
  603. * hit RAM. This function may be very costly as it involves global TLB
  604. * and cache flushes and potential page splitting / combining.
  605. */
  606. extern int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);
  607. extern int ttm_tt_swapout(struct ttm_tt *ttm,
  608. struct file *persistent_swap_storage);
  609. /**
  610. * ttm_tt_unpopulate - free pages from a ttm
  611. *
  612. * @ttm: Pointer to the ttm_tt structure
  613. *
  614. * Calls the driver method to free all pages from a ttm
  615. */
  616. extern void ttm_tt_unpopulate(struct ttm_tt *ttm);
  617. /*
  618. * ttm_bo.c
  619. */
  620. /**
  621. * ttm_mem_reg_is_pci
  622. *
  623. * @bdev: Pointer to a struct ttm_bo_device.
  624. * @mem: A valid struct ttm_mem_reg.
  625. *
  626. * Returns true if the memory described by @mem is PCI memory,
  627. * false otherwise.
  628. */
  629. extern bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev,
  630. struct ttm_mem_reg *mem);
  631. /**
  632. * ttm_bo_mem_space
  633. *
  634. * @bo: Pointer to a struct ttm_buffer_object. the data of which
  635. * we want to allocate space for.
  636. * @proposed_placement: Proposed new placement for the buffer object.
  637. * @mem: A struct ttm_mem_reg.
  638. * @interruptible: Sleep interruptible when sliping.
  639. * @no_wait_gpu: Return immediately if the GPU is busy.
  640. *
  641. * Allocate memory space for the buffer object pointed to by @bo, using
  642. * the placement flags in @mem, potentially evicting other idle buffer objects.
  643. * This function may sleep while waiting for space to become available.
  644. * Returns:
  645. * -EBUSY: No space available (only if no_wait == 1).
  646. * -ENOMEM: Could not allocate memory for the buffer object, either due to
  647. * fragmentation or concurrent allocators.
  648. * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
  649. */
  650. extern int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  651. struct ttm_placement *placement,
  652. struct ttm_mem_reg *mem,
  653. bool interruptible,
  654. bool no_wait_gpu);
  655. extern void ttm_bo_mem_put(struct ttm_buffer_object *bo,
  656. struct ttm_mem_reg *mem);
  657. extern void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo,
  658. struct ttm_mem_reg *mem);
  659. extern void ttm_bo_global_release(struct drm_global_reference *ref);
  660. extern int ttm_bo_global_init(struct drm_global_reference *ref);
  661. extern int ttm_bo_device_release(struct ttm_bo_device *bdev);
  662. /**
  663. * ttm_bo_device_init
  664. *
  665. * @bdev: A pointer to a struct ttm_bo_device to initialize.
  666. * @glob: A pointer to an initialized struct ttm_bo_global.
  667. * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
  668. * @mapping: The address space to use for this bo.
  669. * @file_page_offset: Offset into the device address space that is available
  670. * for buffer data. This ensures compatibility with other users of the
  671. * address space.
  672. *
  673. * Initializes a struct ttm_bo_device:
  674. * Returns:
  675. * !0: Failure.
  676. */
  677. extern int ttm_bo_device_init(struct ttm_bo_device *bdev,
  678. struct ttm_bo_global *glob,
  679. struct ttm_bo_driver *driver,
  680. struct address_space *mapping,
  681. uint64_t file_page_offset, bool need_dma32);
  682. /**
  683. * ttm_bo_unmap_virtual
  684. *
  685. * @bo: tear down the virtual mappings for this BO
  686. */
  687. extern void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
  688. /**
  689. * ttm_bo_unmap_virtual
  690. *
  691. * @bo: tear down the virtual mappings for this BO
  692. *
  693. * The caller must take ttm_mem_io_lock before calling this function.
  694. */
  695. extern void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo);
  696. extern int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo);
  697. extern void ttm_mem_io_free_vm(struct ttm_buffer_object *bo);
  698. extern int ttm_mem_io_lock(struct ttm_mem_type_manager *man,
  699. bool interruptible);
  700. extern void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
  701. extern void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo);
  702. extern void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);
  703. /**
  704. * __ttm_bo_reserve:
  705. *
  706. * @bo: A pointer to a struct ttm_buffer_object.
  707. * @interruptible: Sleep interruptible if waiting.
  708. * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  709. * @ticket: ticket used to acquire the ww_mutex.
  710. *
  711. * Will not remove reserved buffers from the lru lists.
  712. * Otherwise identical to ttm_bo_reserve.
  713. *
  714. * Returns:
  715. * -EDEADLK: The reservation may cause a deadlock.
  716. * Release all buffer reservations, wait for @bo to become unreserved and
  717. * try again. (only if use_sequence == 1).
  718. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  719. * a signal. Release all buffer reservations and return to user-space.
  720. * -EBUSY: The function needed to sleep, but @no_wait was true
  721. * -EALREADY: Bo already reserved using @ticket. This error code will only
  722. * be returned if @use_ticket is set to true.
  723. */
  724. static inline int __ttm_bo_reserve(struct ttm_buffer_object *bo,
  725. bool interruptible, bool no_wait,
  726. struct ww_acquire_ctx *ticket)
  727. {
  728. int ret = 0;
  729. if (no_wait) {
  730. bool success;
  731. if (WARN_ON(ticket))
  732. return -EBUSY;
  733. success = ww_mutex_trylock(&bo->resv->lock);
  734. return success ? 0 : -EBUSY;
  735. }
  736. if (interruptible)
  737. ret = ww_mutex_lock_interruptible(&bo->resv->lock, ticket);
  738. else
  739. ret = ww_mutex_lock(&bo->resv->lock, ticket);
  740. if (ret == -EINTR)
  741. return -ERESTARTSYS;
  742. return ret;
  743. }
  744. /**
  745. * ttm_bo_reserve:
  746. *
  747. * @bo: A pointer to a struct ttm_buffer_object.
  748. * @interruptible: Sleep interruptible if waiting.
  749. * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  750. * @ticket: ticket used to acquire the ww_mutex.
  751. *
  752. * Locks a buffer object for validation. (Or prevents other processes from
  753. * locking it for validation) and removes it from lru lists, while taking
  754. * a number of measures to prevent deadlocks.
  755. *
  756. * Deadlocks may occur when two processes try to reserve multiple buffers in
  757. * different order, either by will or as a result of a buffer being evicted
  758. * to make room for a buffer already reserved. (Buffers are reserved before
  759. * they are evicted). The following algorithm prevents such deadlocks from
  760. * occurring:
  761. * Processes attempting to reserve multiple buffers other than for eviction,
  762. * (typically execbuf), should first obtain a unique 32-bit
  763. * validation sequence number,
  764. * and call this function with @use_ticket == 1 and @ticket->stamp == the unique
  765. * sequence number. If upon call of this function, the buffer object is already
  766. * reserved, the validation sequence is checked against the validation
  767. * sequence of the process currently reserving the buffer,
  768. * and if the current validation sequence is greater than that of the process
  769. * holding the reservation, the function returns -EDEADLK. Otherwise it sleeps
  770. * waiting for the buffer to become unreserved, after which it retries
  771. * reserving.
  772. * The caller should, when receiving an -EDEADLK error
  773. * release all its buffer reservations, wait for @bo to become unreserved, and
  774. * then rerun the validation with the same validation sequence. This procedure
  775. * will always guarantee that the process with the lowest validation sequence
  776. * will eventually succeed, preventing both deadlocks and starvation.
  777. *
  778. * Returns:
  779. * -EDEADLK: The reservation may cause a deadlock.
  780. * Release all buffer reservations, wait for @bo to become unreserved and
  781. * try again. (only if use_sequence == 1).
  782. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  783. * a signal. Release all buffer reservations and return to user-space.
  784. * -EBUSY: The function needed to sleep, but @no_wait was true
  785. * -EALREADY: Bo already reserved using @ticket. This error code will only
  786. * be returned if @use_ticket is set to true.
  787. */
  788. static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
  789. bool interruptible, bool no_wait,
  790. struct ww_acquire_ctx *ticket)
  791. {
  792. int ret;
  793. WARN_ON(!kref_read(&bo->kref));
  794. ret = __ttm_bo_reserve(bo, interruptible, no_wait, ticket);
  795. if (likely(ret == 0))
  796. ttm_bo_del_sub_from_lru(bo);
  797. return ret;
  798. }
  799. /**
  800. * ttm_bo_reserve_slowpath:
  801. * @bo: A pointer to a struct ttm_buffer_object.
  802. * @interruptible: Sleep interruptible if waiting.
  803. * @sequence: Set (@bo)->sequence to this value after lock
  804. *
  805. * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
  806. * from all our other reservations. Because there are no other reservations
  807. * held by us, this function cannot deadlock any more.
  808. */
  809. static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
  810. bool interruptible,
  811. struct ww_acquire_ctx *ticket)
  812. {
  813. int ret = 0;
  814. WARN_ON(!kref_read(&bo->kref));
  815. if (interruptible)
  816. ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
  817. ticket);
  818. else
  819. ww_mutex_lock_slow(&bo->resv->lock, ticket);
  820. if (likely(ret == 0))
  821. ttm_bo_del_sub_from_lru(bo);
  822. else if (ret == -EINTR)
  823. ret = -ERESTARTSYS;
  824. return ret;
  825. }
  826. /**
  827. * __ttm_bo_unreserve
  828. * @bo: A pointer to a struct ttm_buffer_object.
  829. *
  830. * Unreserve a previous reservation of @bo where the buffer object is
  831. * already on lru lists.
  832. */
  833. static inline void __ttm_bo_unreserve(struct ttm_buffer_object *bo)
  834. {
  835. ww_mutex_unlock(&bo->resv->lock);
  836. }
  837. /**
  838. * ttm_bo_unreserve
  839. *
  840. * @bo: A pointer to a struct ttm_buffer_object.
  841. *
  842. * Unreserve a previous reservation of @bo.
  843. */
  844. static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
  845. {
  846. if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  847. spin_lock(&bo->glob->lru_lock);
  848. ttm_bo_add_to_lru(bo);
  849. spin_unlock(&bo->glob->lru_lock);
  850. }
  851. __ttm_bo_unreserve(bo);
  852. }
  853. /**
  854. * ttm_bo_unreserve_ticket
  855. * @bo: A pointer to a struct ttm_buffer_object.
  856. * @ticket: ww_acquire_ctx used for reserving
  857. *
  858. * Unreserve a previous reservation of @bo made with @ticket.
  859. */
  860. static inline void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo,
  861. struct ww_acquire_ctx *t)
  862. {
  863. ttm_bo_unreserve(bo);
  864. }
  865. /*
  866. * ttm_bo_util.c
  867. */
  868. int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
  869. struct ttm_mem_reg *mem);
  870. void ttm_mem_io_free(struct ttm_bo_device *bdev,
  871. struct ttm_mem_reg *mem);
  872. /**
  873. * ttm_bo_move_ttm
  874. *
  875. * @bo: A pointer to a struct ttm_buffer_object.
  876. * @interruptible: Sleep interruptible if waiting.
  877. * @no_wait_gpu: Return immediately if the GPU is busy.
  878. * @new_mem: struct ttm_mem_reg indicating where to move.
  879. *
  880. * Optimized move function for a buffer object with both old and
  881. * new placement backed by a TTM. The function will, if successful,
  882. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  883. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  884. * data remains untouched, and it's up to the caller to free the
  885. * memory space indicated by @new_mem.
  886. * Returns:
  887. * !0: Failure.
  888. */
  889. extern int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
  890. bool interruptible, bool no_wait_gpu,
  891. struct ttm_mem_reg *new_mem);
  892. /**
  893. * ttm_bo_move_memcpy
  894. *
  895. * @bo: A pointer to a struct ttm_buffer_object.
  896. * @interruptible: Sleep interruptible if waiting.
  897. * @no_wait_gpu: Return immediately if the GPU is busy.
  898. * @new_mem: struct ttm_mem_reg indicating where to move.
  899. *
  900. * Fallback move function for a mappable buffer object in mappable memory.
  901. * The function will, if successful,
  902. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  903. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  904. * data remains untouched, and it's up to the caller to free the
  905. * memory space indicated by @new_mem.
  906. * Returns:
  907. * !0: Failure.
  908. */
  909. extern int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  910. bool interruptible, bool no_wait_gpu,
  911. struct ttm_mem_reg *new_mem);
  912. /**
  913. * ttm_bo_free_old_node
  914. *
  915. * @bo: A pointer to a struct ttm_buffer_object.
  916. *
  917. * Utility function to free an old placement after a successful move.
  918. */
  919. extern void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
  920. /**
  921. * ttm_bo_move_accel_cleanup.
  922. *
  923. * @bo: A pointer to a struct ttm_buffer_object.
  924. * @fence: A fence object that signals when moving is complete.
  925. * @evict: This is an evict move. Don't return until the buffer is idle.
  926. * @new_mem: struct ttm_mem_reg indicating where to move.
  927. *
  928. * Accelerated move function to be called when an accelerated move
  929. * has been scheduled. The function will create a new temporary buffer object
  930. * representing the old placement, and put the sync object on both buffer
  931. * objects. After that the newly created buffer object is unref'd to be
  932. * destroyed when the move is complete. This will help pipeline
  933. * buffer moves.
  934. */
  935. extern int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  936. struct dma_fence *fence, bool evict,
  937. struct ttm_mem_reg *new_mem);
  938. /**
  939. * ttm_bo_pipeline_move.
  940. *
  941. * @bo: A pointer to a struct ttm_buffer_object.
  942. * @fence: A fence object that signals when moving is complete.
  943. * @evict: This is an evict move. Don't return until the buffer is idle.
  944. * @new_mem: struct ttm_mem_reg indicating where to move.
  945. *
  946. * Function for pipelining accelerated moves. Either free the memory
  947. * immediately or hang it on a temporary buffer object.
  948. */
  949. int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
  950. struct dma_fence *fence, bool evict,
  951. struct ttm_mem_reg *new_mem);
  952. /**
  953. * ttm_io_prot
  954. *
  955. * @c_state: Caching state.
  956. * @tmp: Page protection flag for a normal, cached mapping.
  957. *
  958. * Utility function that returns the pgprot_t that should be used for
  959. * setting up a PTE with the caching model indicated by @c_state.
  960. */
  961. extern pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);
  962. extern const struct ttm_mem_type_manager_func ttm_bo_manager_func;
  963. #if IS_ENABLED(CONFIG_AGP)
  964. #include <linux/agp_backend.h>
  965. /**
  966. * ttm_agp_tt_create
  967. *
  968. * @bdev: Pointer to a struct ttm_bo_device.
  969. * @bridge: The agp bridge this device is sitting on.
  970. * @size: Size of the data needed backing.
  971. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  972. * @dummy_read_page: See struct ttm_bo_device.
  973. *
  974. *
  975. * Create a TTM backend that uses the indicated AGP bridge as an aperture
  976. * for TT memory. This function uses the linux agpgart interface to
  977. * bind and unbind memory backing a ttm_tt.
  978. */
  979. extern struct ttm_tt *ttm_agp_tt_create(struct ttm_bo_device *bdev,
  980. struct agp_bridge_data *bridge,
  981. unsigned long size, uint32_t page_flags,
  982. struct page *dummy_read_page);
  983. int ttm_agp_tt_populate(struct ttm_tt *ttm);
  984. void ttm_agp_tt_unpopulate(struct ttm_tt *ttm);
  985. #endif
  986. #endif