ttm_bo_driver.h 33 KB

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