ttm_bo_driver.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. struct ttm_placement *placement,
  198. uint32_t flags,
  199. struct ttm_mem_reg *mem);
  200. /**
  201. * struct ttm_mem_type_manager member put_node
  202. *
  203. * @man: Pointer to a memory type manager.
  204. * @mem: Pointer to a struct ttm_mem_reg to be filled in.
  205. *
  206. * This function frees memory type resources previously allocated
  207. * and that are identified by @mem::mm_node and @mem::start. May not
  208. * be called from within atomic context.
  209. */
  210. void (*put_node)(struct ttm_mem_type_manager *man,
  211. struct ttm_mem_reg *mem);
  212. /**
  213. * struct ttm_mem_type_manager member debug
  214. *
  215. * @man: Pointer to a memory type manager.
  216. * @prefix: Prefix to be used in printout to identify the caller.
  217. *
  218. * This function is called to print out the state of the memory
  219. * type manager to aid debugging of out-of-memory conditions.
  220. * It may not be called from within atomic context.
  221. */
  222. void (*debug)(struct ttm_mem_type_manager *man, const char *prefix);
  223. };
  224. /**
  225. * struct ttm_mem_type_manager
  226. *
  227. * @has_type: The memory type has been initialized.
  228. * @use_type: The memory type is enabled.
  229. * @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory
  230. * managed by this memory type.
  231. * @gpu_offset: If used, the GPU offset of the first managed page of
  232. * fixed memory or the first managed location in an aperture.
  233. * @size: Size of the managed region.
  234. * @available_caching: A mask of available caching types, TTM_PL_FLAG_XX,
  235. * as defined in ttm_placement_common.h
  236. * @default_caching: The default caching policy used for a buffer object
  237. * placed in this memory type if the user doesn't provide one.
  238. * @func: structure pointer implementing the range manager. See above
  239. * @priv: Driver private closure for @func.
  240. * @io_reserve_mutex: Mutex optionally protecting shared io_reserve structures
  241. * @use_io_reserve_lru: Use an lru list to try to unreserve io_mem_regions
  242. * reserved by the TTM vm system.
  243. * @io_reserve_lru: Optional lru list for unreserving io mem regions.
  244. * @io_reserve_fastpath: Only use bdev::driver::io_mem_reserve to obtain
  245. * static information. bdev::driver::io_mem_free is never used.
  246. * @lru: The lru list for this memory type.
  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. unsigned long gpu_offset;
  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. /*
  269. * Protected by @io_reserve_mutex:
  270. */
  271. struct list_head io_reserve_lru;
  272. /*
  273. * Protected by the global->lru_lock.
  274. */
  275. struct list_head lru;
  276. };
  277. /**
  278. * struct ttm_bo_driver
  279. *
  280. * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
  281. * @invalidate_caches: Callback to invalidate read caches when a buffer object
  282. * has been evicted.
  283. * @init_mem_type: Callback to initialize a struct ttm_mem_type_manager
  284. * structure.
  285. * @evict_flags: Callback to obtain placement flags when a buffer is evicted.
  286. * @move: Callback for a driver to hook in accelerated functions to
  287. * move a buffer.
  288. * If set to NULL, a potentially slow memcpy() move is used.
  289. * @sync_obj_signaled: See ttm_fence_api.h
  290. * @sync_obj_wait: See ttm_fence_api.h
  291. * @sync_obj_flush: See ttm_fence_api.h
  292. * @sync_obj_unref: See ttm_fence_api.h
  293. * @sync_obj_ref: See ttm_fence_api.h
  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 evict_flags:
  347. *
  348. * @bo: the buffer object to be evicted
  349. *
  350. * Return the bo flags for a buffer which is not mapped to the hardware.
  351. * These will be placed in proposed_flags so that when the move is
  352. * finished, they'll end up in bo->mem.flags
  353. */
  354. void(*evict_flags) (struct ttm_buffer_object *bo,
  355. struct ttm_placement *placement);
  356. /**
  357. * struct ttm_bo_driver member move:
  358. *
  359. * @bo: the buffer to move
  360. * @evict: whether this motion is evicting the buffer from
  361. * the graphics address space
  362. * @interruptible: Use interruptible sleeps if possible when sleeping.
  363. * @no_wait: whether this should give up and return -EBUSY
  364. * if this move would require sleeping
  365. * @new_mem: the new memory region receiving the buffer
  366. *
  367. * Move a buffer between two memory regions.
  368. */
  369. int (*move) (struct ttm_buffer_object *bo,
  370. bool evict, bool interruptible,
  371. bool no_wait_gpu,
  372. struct ttm_mem_reg *new_mem);
  373. /**
  374. * struct ttm_bo_driver_member verify_access
  375. *
  376. * @bo: Pointer to a buffer object.
  377. * @filp: Pointer to a struct file trying to access the object.
  378. *
  379. * Called from the map / write / read methods to verify that the
  380. * caller is permitted to access the buffer object.
  381. * This member may be set to NULL, which will refuse this kind of
  382. * access for all buffer objects.
  383. * This function should return 0 if access is granted, -EPERM otherwise.
  384. */
  385. int (*verify_access) (struct ttm_buffer_object *bo,
  386. struct file *filp);
  387. /**
  388. * In case a driver writer dislikes the TTM fence objects,
  389. * the driver writer can replace those with sync objects of
  390. * his / her own. If it turns out that no driver writer is
  391. * using these. I suggest we remove these hooks and plug in
  392. * fences directly. The bo driver needs the following functionality:
  393. * See the corresponding functions in the fence object API
  394. * documentation.
  395. */
  396. bool (*sync_obj_signaled) (void *sync_obj);
  397. int (*sync_obj_wait) (void *sync_obj,
  398. bool lazy, bool interruptible);
  399. int (*sync_obj_flush) (void *sync_obj);
  400. void (*sync_obj_unref) (void **sync_obj);
  401. void *(*sync_obj_ref) (void *sync_obj);
  402. /* hook to notify driver about a driver move so it
  403. * can do tiling things */
  404. void (*move_notify)(struct ttm_buffer_object *bo,
  405. struct ttm_mem_reg *new_mem);
  406. /* notify the driver we are taking a fault on this BO
  407. * and have reserved it */
  408. int (*fault_reserve_notify)(struct ttm_buffer_object *bo);
  409. /**
  410. * notify the driver that we're about to swap out this bo
  411. */
  412. void (*swap_notify) (struct ttm_buffer_object *bo);
  413. /**
  414. * Driver callback on when mapping io memory (for bo_move_memcpy
  415. * for instance). TTM will take care to call io_mem_free whenever
  416. * the mapping is not use anymore. io_mem_reserve & io_mem_free
  417. * are balanced.
  418. */
  419. int (*io_mem_reserve)(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem);
  420. void (*io_mem_free)(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem);
  421. };
  422. /**
  423. * struct ttm_bo_global_ref - Argument to initialize a struct ttm_bo_global.
  424. */
  425. struct ttm_bo_global_ref {
  426. struct drm_global_reference ref;
  427. struct ttm_mem_global *mem_glob;
  428. };
  429. /**
  430. * struct ttm_bo_global - Buffer object driver global data.
  431. *
  432. * @mem_glob: Pointer to a struct ttm_mem_global object for accounting.
  433. * @dummy_read_page: Pointer to a dummy page used for mapping requests
  434. * of unpopulated pages.
  435. * @shrink: A shrink callback object used for buffer object swap.
  436. * @device_list_mutex: Mutex protecting the device list.
  437. * This mutex is held while traversing the device list for pm options.
  438. * @lru_lock: Spinlock protecting the bo subsystem lru lists.
  439. * @device_list: List of buffer object devices.
  440. * @swap_lru: Lru list of buffer objects used for swapping.
  441. */
  442. struct ttm_bo_global {
  443. /**
  444. * Constant after init.
  445. */
  446. struct kobject kobj;
  447. struct ttm_mem_global *mem_glob;
  448. struct page *dummy_read_page;
  449. struct ttm_mem_shrink shrink;
  450. struct mutex device_list_mutex;
  451. spinlock_t lru_lock;
  452. /**
  453. * Protected by device_list_mutex.
  454. */
  455. struct list_head device_list;
  456. /**
  457. * Protected by the lru_lock.
  458. */
  459. struct list_head swap_lru;
  460. /**
  461. * Internal protection.
  462. */
  463. atomic_t bo_count;
  464. };
  465. #define TTM_NUM_MEM_TYPES 8
  466. #define TTM_BO_PRIV_FLAG_MOVING 0 /* Buffer object is moving and needs
  467. idling before CPU mapping */
  468. #define TTM_BO_PRIV_FLAG_MAX 1
  469. /**
  470. * struct ttm_bo_device - Buffer object driver device-specific data.
  471. *
  472. * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
  473. * @man: An array of mem_type_managers.
  474. * @fence_lock: Protects the synchronizing members on *all* bos belonging
  475. * to this device.
  476. * @vma_manager: Address space manager
  477. * lru_lock: Spinlock that protects the buffer+device lru lists and
  478. * ddestroy lists.
  479. * @val_seq: Current validation sequence.
  480. * @dev_mapping: A pointer to the struct address_space representing the
  481. * device address space.
  482. * @wq: Work queue structure for the delayed delete workqueue.
  483. *
  484. */
  485. struct ttm_bo_device {
  486. /*
  487. * Constant after bo device init / atomic.
  488. */
  489. struct list_head device_list;
  490. struct ttm_bo_global *glob;
  491. struct ttm_bo_driver *driver;
  492. struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];
  493. spinlock_t fence_lock;
  494. /*
  495. * Protected by internal locks.
  496. */
  497. struct drm_vma_offset_manager vma_manager;
  498. /*
  499. * Protected by the global:lru lock.
  500. */
  501. struct list_head ddestroy;
  502. uint32_t val_seq;
  503. /*
  504. * Protected by load / firstopen / lastclose /unload sync.
  505. */
  506. struct address_space *dev_mapping;
  507. /*
  508. * Internal protection.
  509. */
  510. struct delayed_work wq;
  511. bool need_dma32;
  512. };
  513. /**
  514. * ttm_flag_masked
  515. *
  516. * @old: Pointer to the result and original value.
  517. * @new: New value of bits.
  518. * @mask: Mask of bits to change.
  519. *
  520. * Convenience function to change a number of bits identified by a mask.
  521. */
  522. static inline uint32_t
  523. ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
  524. {
  525. *old ^= (*old ^ new) & mask;
  526. return *old;
  527. }
  528. /**
  529. * ttm_tt_init
  530. *
  531. * @ttm: The struct ttm_tt.
  532. * @bdev: pointer to a struct ttm_bo_device:
  533. * @size: Size of the data needed backing.
  534. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  535. * @dummy_read_page: See struct ttm_bo_device.
  536. *
  537. * Create a struct ttm_tt to back data with system memory pages.
  538. * No pages are actually allocated.
  539. * Returns:
  540. * NULL: Out of memory.
  541. */
  542. extern int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
  543. unsigned long size, uint32_t page_flags,
  544. struct page *dummy_read_page);
  545. extern int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
  546. unsigned long size, uint32_t page_flags,
  547. struct page *dummy_read_page);
  548. /**
  549. * ttm_tt_fini
  550. *
  551. * @ttm: the ttm_tt structure.
  552. *
  553. * Free memory of ttm_tt structure
  554. */
  555. extern void ttm_tt_fini(struct ttm_tt *ttm);
  556. extern void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma);
  557. /**
  558. * ttm_ttm_bind:
  559. *
  560. * @ttm: The struct ttm_tt containing backing pages.
  561. * @bo_mem: The struct ttm_mem_reg identifying the binding location.
  562. *
  563. * Bind the pages of @ttm to an aperture location identified by @bo_mem
  564. */
  565. extern int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
  566. /**
  567. * ttm_ttm_destroy:
  568. *
  569. * @ttm: The struct ttm_tt.
  570. *
  571. * Unbind, unpopulate and destroy common struct ttm_tt.
  572. */
  573. extern void ttm_tt_destroy(struct ttm_tt *ttm);
  574. /**
  575. * ttm_ttm_unbind:
  576. *
  577. * @ttm: The struct ttm_tt.
  578. *
  579. * Unbind a struct ttm_tt.
  580. */
  581. extern void ttm_tt_unbind(struct ttm_tt *ttm);
  582. /**
  583. * ttm_tt_swapin:
  584. *
  585. * @ttm: The struct ttm_tt.
  586. *
  587. * Swap in a previously swap out ttm_tt.
  588. */
  589. extern int ttm_tt_swapin(struct ttm_tt *ttm);
  590. /**
  591. * ttm_tt_set_placement_caching:
  592. *
  593. * @ttm A struct ttm_tt the backing pages of which will change caching policy.
  594. * @placement: Flag indicating the desired caching policy.
  595. *
  596. * This function will change caching policy of any default kernel mappings of
  597. * the pages backing @ttm. If changing from cached to uncached or
  598. * write-combined,
  599. * all CPU caches will first be flushed to make sure the data of the pages
  600. * hit RAM. This function may be very costly as it involves global TLB
  601. * and cache flushes and potential page splitting / combining.
  602. */
  603. extern int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);
  604. extern int ttm_tt_swapout(struct ttm_tt *ttm,
  605. struct file *persistent_swap_storage);
  606. /**
  607. * ttm_tt_unpopulate - free pages from a ttm
  608. *
  609. * @ttm: Pointer to the ttm_tt structure
  610. *
  611. * Calls the driver method to free all pages from a ttm
  612. */
  613. extern void ttm_tt_unpopulate(struct ttm_tt *ttm);
  614. /*
  615. * ttm_bo.c
  616. */
  617. /**
  618. * ttm_mem_reg_is_pci
  619. *
  620. * @bdev: Pointer to a struct ttm_bo_device.
  621. * @mem: A valid struct ttm_mem_reg.
  622. *
  623. * Returns true if the memory described by @mem is PCI memory,
  624. * false otherwise.
  625. */
  626. extern bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev,
  627. struct ttm_mem_reg *mem);
  628. /**
  629. * ttm_bo_mem_space
  630. *
  631. * @bo: Pointer to a struct ttm_buffer_object. the data of which
  632. * we want to allocate space for.
  633. * @proposed_placement: Proposed new placement for the buffer object.
  634. * @mem: A struct ttm_mem_reg.
  635. * @interruptible: Sleep interruptible when sliping.
  636. * @no_wait_gpu: Return immediately if the GPU is busy.
  637. *
  638. * Allocate memory space for the buffer object pointed to by @bo, using
  639. * the placement flags in @mem, potentially evicting other idle buffer objects.
  640. * This function may sleep while waiting for space to become available.
  641. * Returns:
  642. * -EBUSY: No space available (only if no_wait == 1).
  643. * -ENOMEM: Could not allocate memory for the buffer object, either due to
  644. * fragmentation or concurrent allocators.
  645. * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
  646. */
  647. extern int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  648. struct ttm_placement *placement,
  649. struct ttm_mem_reg *mem,
  650. bool interruptible,
  651. bool no_wait_gpu);
  652. extern void ttm_bo_mem_put(struct ttm_buffer_object *bo,
  653. struct ttm_mem_reg *mem);
  654. extern void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo,
  655. struct ttm_mem_reg *mem);
  656. extern void ttm_bo_global_release(struct drm_global_reference *ref);
  657. extern int ttm_bo_global_init(struct drm_global_reference *ref);
  658. extern int ttm_bo_device_release(struct ttm_bo_device *bdev);
  659. /**
  660. * ttm_bo_device_init
  661. *
  662. * @bdev: A pointer to a struct ttm_bo_device to initialize.
  663. * @glob: A pointer to an initialized struct ttm_bo_global.
  664. * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
  665. * @mapping: The address space to use for this bo.
  666. * @file_page_offset: Offset into the device address space that is available
  667. * for buffer data. This ensures compatibility with other users of the
  668. * address space.
  669. *
  670. * Initializes a struct ttm_bo_device:
  671. * Returns:
  672. * !0: Failure.
  673. */
  674. extern int ttm_bo_device_init(struct ttm_bo_device *bdev,
  675. struct ttm_bo_global *glob,
  676. struct ttm_bo_driver *driver,
  677. struct address_space *mapping,
  678. uint64_t file_page_offset, bool need_dma32);
  679. /**
  680. * ttm_bo_unmap_virtual
  681. *
  682. * @bo: tear down the virtual mappings for this BO
  683. */
  684. extern void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
  685. /**
  686. * ttm_bo_unmap_virtual
  687. *
  688. * @bo: tear down the virtual mappings for this BO
  689. *
  690. * The caller must take ttm_mem_io_lock before calling this function.
  691. */
  692. extern void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo);
  693. extern int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo);
  694. extern void ttm_mem_io_free_vm(struct ttm_buffer_object *bo);
  695. extern int ttm_mem_io_lock(struct ttm_mem_type_manager *man,
  696. bool interruptible);
  697. extern void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
  698. extern void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo);
  699. extern void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);
  700. /**
  701. * __ttm_bo_reserve:
  702. *
  703. * @bo: A pointer to a struct ttm_buffer_object.
  704. * @interruptible: Sleep interruptible if waiting.
  705. * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  706. * @use_ticket: If @bo is already reserved, Only sleep waiting for
  707. * it to become unreserved if @ticket->stamp is older.
  708. *
  709. * Will not remove reserved buffers from the lru lists.
  710. * Otherwise identical to ttm_bo_reserve.
  711. *
  712. * Returns:
  713. * -EDEADLK: The reservation may cause a deadlock.
  714. * Release all buffer reservations, wait for @bo to become unreserved and
  715. * try again. (only if use_sequence == 1).
  716. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  717. * a signal. Release all buffer reservations and return to user-space.
  718. * -EBUSY: The function needed to sleep, but @no_wait was true
  719. * -EALREADY: Bo already reserved using @ticket. This error code will only
  720. * be returned if @use_ticket is set to true.
  721. */
  722. static inline int __ttm_bo_reserve(struct ttm_buffer_object *bo,
  723. bool interruptible,
  724. bool no_wait, bool use_ticket,
  725. struct ww_acquire_ctx *ticket)
  726. {
  727. int ret = 0;
  728. if (no_wait) {
  729. bool success;
  730. if (WARN_ON(ticket))
  731. return -EBUSY;
  732. success = ww_mutex_trylock(&bo->resv->lock);
  733. return success ? 0 : -EBUSY;
  734. }
  735. if (interruptible)
  736. ret = ww_mutex_lock_interruptible(&bo->resv->lock, ticket);
  737. else
  738. ret = ww_mutex_lock(&bo->resv->lock, ticket);
  739. if (ret == -EINTR)
  740. return -ERESTARTSYS;
  741. return ret;
  742. }
  743. /**
  744. * ttm_bo_reserve:
  745. *
  746. * @bo: A pointer to a struct ttm_buffer_object.
  747. * @interruptible: Sleep interruptible if waiting.
  748. * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  749. * @use_ticket: If @bo is already reserved, Only sleep waiting for
  750. * it to become unreserved if @ticket->stamp is older.
  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 -EAGAIN. Otherwise it sleeps
  770. * waiting for the buffer to become unreserved, after which it retries
  771. * reserving.
  772. * The caller should, when receiving an -EAGAIN 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,
  790. bool no_wait, bool use_ticket,
  791. struct ww_acquire_ctx *ticket)
  792. {
  793. int ret;
  794. WARN_ON(!atomic_read(&bo->kref.refcount));
  795. ret = __ttm_bo_reserve(bo, interruptible, no_wait, use_ticket, ticket);
  796. if (likely(ret == 0))
  797. ttm_bo_del_sub_from_lru(bo);
  798. return ret;
  799. }
  800. /**
  801. * ttm_bo_reserve_slowpath:
  802. * @bo: A pointer to a struct ttm_buffer_object.
  803. * @interruptible: Sleep interruptible if waiting.
  804. * @sequence: Set (@bo)->sequence to this value after lock
  805. *
  806. * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
  807. * from all our other reservations. Because there are no other reservations
  808. * held by us, this function cannot deadlock any more.
  809. */
  810. static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
  811. bool interruptible,
  812. struct ww_acquire_ctx *ticket)
  813. {
  814. int ret = 0;
  815. WARN_ON(!atomic_read(&bo->kref.refcount));
  816. if (interruptible)
  817. ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
  818. ticket);
  819. else
  820. ww_mutex_lock_slow(&bo->resv->lock, ticket);
  821. if (likely(ret == 0))
  822. ttm_bo_del_sub_from_lru(bo);
  823. else if (ret == -EINTR)
  824. ret = -ERESTARTSYS;
  825. return ret;
  826. }
  827. /**
  828. * __ttm_bo_unreserve
  829. * @bo: A pointer to a struct ttm_buffer_object.
  830. *
  831. * Unreserve a previous reservation of @bo where the buffer object is
  832. * already on lru lists.
  833. */
  834. static inline void __ttm_bo_unreserve(struct ttm_buffer_object *bo)
  835. {
  836. ww_mutex_unlock(&bo->resv->lock);
  837. }
  838. /**
  839. * ttm_bo_unreserve
  840. *
  841. * @bo: A pointer to a struct ttm_buffer_object.
  842. *
  843. * Unreserve a previous reservation of @bo.
  844. */
  845. static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
  846. {
  847. if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
  848. spin_lock(&bo->glob->lru_lock);
  849. ttm_bo_add_to_lru(bo);
  850. spin_unlock(&bo->glob->lru_lock);
  851. }
  852. __ttm_bo_unreserve(bo);
  853. }
  854. /**
  855. * ttm_bo_unreserve_ticket
  856. * @bo: A pointer to a struct ttm_buffer_object.
  857. * @ticket: ww_acquire_ctx used for reserving
  858. *
  859. * Unreserve a previous reservation of @bo made with @ticket.
  860. */
  861. static inline void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo,
  862. struct ww_acquire_ctx *t)
  863. {
  864. ttm_bo_unreserve(bo);
  865. }
  866. /*
  867. * ttm_bo_util.c
  868. */
  869. int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
  870. struct ttm_mem_reg *mem);
  871. void ttm_mem_io_free(struct ttm_bo_device *bdev,
  872. struct ttm_mem_reg *mem);
  873. /**
  874. * ttm_bo_move_ttm
  875. *
  876. * @bo: A pointer to a struct ttm_buffer_object.
  877. * @evict: 1: This is an eviction. Don't try to pipeline.
  878. * @no_wait_gpu: Return immediately if the GPU is busy.
  879. * @new_mem: struct ttm_mem_reg indicating where to move.
  880. *
  881. * Optimized move function for a buffer object with both old and
  882. * new placement backed by a TTM. The function will, if successful,
  883. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  884. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  885. * data remains untouched, and it's up to the caller to free the
  886. * memory space indicated by @new_mem.
  887. * Returns:
  888. * !0: Failure.
  889. */
  890. extern int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
  891. bool evict, bool no_wait_gpu,
  892. struct ttm_mem_reg *new_mem);
  893. /**
  894. * ttm_bo_move_memcpy
  895. *
  896. * @bo: A pointer to a struct ttm_buffer_object.
  897. * @evict: 1: This is an eviction. Don't try to pipeline.
  898. * @no_wait_gpu: Return immediately if the GPU is busy.
  899. * @new_mem: struct ttm_mem_reg indicating where to move.
  900. *
  901. * Fallback move function for a mappable buffer object in mappable memory.
  902. * The function will, if successful,
  903. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  904. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  905. * data remains untouched, and it's up to the caller to free the
  906. * memory space indicated by @new_mem.
  907. * Returns:
  908. * !0: Failure.
  909. */
  910. extern int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  911. bool evict, bool no_wait_gpu,
  912. struct ttm_mem_reg *new_mem);
  913. /**
  914. * ttm_bo_free_old_node
  915. *
  916. * @bo: A pointer to a struct ttm_buffer_object.
  917. *
  918. * Utility function to free an old placement after a successful move.
  919. */
  920. extern void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
  921. /**
  922. * ttm_bo_move_accel_cleanup.
  923. *
  924. * @bo: A pointer to a struct ttm_buffer_object.
  925. * @sync_obj: A sync object that signals when moving is complete.
  926. * @evict: This is an evict move. Don't return until the buffer is idle.
  927. * @no_wait_gpu: Return immediately if the GPU is busy.
  928. * @new_mem: struct ttm_mem_reg indicating where to move.
  929. *
  930. * Accelerated move function to be called when an accelerated move
  931. * has been scheduled. The function will create a new temporary buffer object
  932. * representing the old placement, and put the sync object on both buffer
  933. * objects. After that the newly created buffer object is unref'd to be
  934. * destroyed when the move is complete. This will help pipeline
  935. * buffer moves.
  936. */
  937. extern int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  938. void *sync_obj,
  939. bool evict, bool no_wait_gpu,
  940. struct ttm_mem_reg *new_mem);
  941. /**
  942. * ttm_io_prot
  943. *
  944. * @c_state: Caching state.
  945. * @tmp: Page protection flag for a normal, cached mapping.
  946. *
  947. * Utility function that returns the pgprot_t that should be used for
  948. * setting up a PTE with the caching model indicated by @c_state.
  949. */
  950. extern pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);
  951. extern const struct ttm_mem_type_manager_func ttm_bo_manager_func;
  952. #if (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))
  953. #define TTM_HAS_AGP
  954. #include <linux/agp_backend.h>
  955. /**
  956. * ttm_agp_tt_create
  957. *
  958. * @bdev: Pointer to a struct ttm_bo_device.
  959. * @bridge: The agp bridge this device is sitting on.
  960. * @size: Size of the data needed backing.
  961. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  962. * @dummy_read_page: See struct ttm_bo_device.
  963. *
  964. *
  965. * Create a TTM backend that uses the indicated AGP bridge as an aperture
  966. * for TT memory. This function uses the linux agpgart interface to
  967. * bind and unbind memory backing a ttm_tt.
  968. */
  969. extern struct ttm_tt *ttm_agp_tt_create(struct ttm_bo_device *bdev,
  970. struct agp_bridge_data *bridge,
  971. unsigned long size, uint32_t page_flags,
  972. struct page *dummy_read_page);
  973. int ttm_agp_tt_populate(struct ttm_tt *ttm);
  974. void ttm_agp_tt_unpopulate(struct ttm_tt *ttm);
  975. #endif
  976. #endif