ttm_bo_driver.h 33 KB

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