ttm_bo_driver.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 "linux/workqueue.h"
  37. #include "linux/fs.h"
  38. #include "linux/spinlock.h"
  39. struct ttm_backend;
  40. struct ttm_backend_func {
  41. /**
  42. * struct ttm_backend_func member populate
  43. *
  44. * @backend: Pointer to a struct ttm_backend.
  45. * @num_pages: Number of pages to populate.
  46. * @pages: Array of pointers to ttm pages.
  47. * @dummy_read_page: Page to be used instead of NULL pages in the
  48. * array @pages.
  49. *
  50. * Populate the backend with ttm pages. Depending on the backend,
  51. * it may or may not copy the @pages array.
  52. */
  53. int (*populate) (struct ttm_backend *backend,
  54. unsigned long num_pages, struct page **pages,
  55. struct page *dummy_read_page);
  56. /**
  57. * struct ttm_backend_func member clear
  58. *
  59. * @backend: Pointer to a struct ttm_backend.
  60. *
  61. * This is an "unpopulate" function. Release all resources
  62. * allocated with populate.
  63. */
  64. void (*clear) (struct ttm_backend *backend);
  65. /**
  66. * struct ttm_backend_func member bind
  67. *
  68. * @backend: Pointer to a struct ttm_backend.
  69. * @bo_mem: Pointer to a struct ttm_mem_reg describing the
  70. * memory type and location for binding.
  71. *
  72. * Bind the backend pages into the aperture in the location
  73. * indicated by @bo_mem. This function should be able to handle
  74. * differences between aperture- and system page sizes.
  75. */
  76. int (*bind) (struct ttm_backend *backend, struct ttm_mem_reg *bo_mem);
  77. /**
  78. * struct ttm_backend_func member unbind
  79. *
  80. * @backend: Pointer to a struct ttm_backend.
  81. *
  82. * Unbind previously bound backend pages. This function should be
  83. * able to handle differences between aperture- and system page sizes.
  84. */
  85. int (*unbind) (struct ttm_backend *backend);
  86. /**
  87. * struct ttm_backend_func member destroy
  88. *
  89. * @backend: Pointer to a struct ttm_backend.
  90. *
  91. * Destroy the backend.
  92. */
  93. void (*destroy) (struct ttm_backend *backend);
  94. };
  95. /**
  96. * struct ttm_backend
  97. *
  98. * @bdev: Pointer to a struct ttm_bo_device.
  99. * @flags: For driver use.
  100. * @func: Pointer to a struct ttm_backend_func that describes
  101. * the backend methods.
  102. *
  103. */
  104. struct ttm_backend {
  105. struct ttm_bo_device *bdev;
  106. uint32_t flags;
  107. struct ttm_backend_func *func;
  108. };
  109. #define TTM_PAGE_FLAG_USER (1 << 1)
  110. #define TTM_PAGE_FLAG_USER_DIRTY (1 << 2)
  111. #define TTM_PAGE_FLAG_WRITE (1 << 3)
  112. #define TTM_PAGE_FLAG_SWAPPED (1 << 4)
  113. #define TTM_PAGE_FLAG_PERSISTANT_SWAP (1 << 5)
  114. #define TTM_PAGE_FLAG_ZERO_ALLOC (1 << 6)
  115. #define TTM_PAGE_FLAG_DMA32 (1 << 7)
  116. enum ttm_caching_state {
  117. tt_uncached,
  118. tt_wc,
  119. tt_cached
  120. };
  121. /**
  122. * struct ttm_tt
  123. *
  124. * @dummy_read_page: Page to map where the ttm_tt page array contains a NULL
  125. * pointer.
  126. * @pages: Array of pages backing the data.
  127. * @first_himem_page: Himem pages are put last in the page array, which
  128. * enables us to run caching attribute changes on only the first part
  129. * of the page array containing lomem pages. This is the index of the
  130. * first himem page.
  131. * @last_lomem_page: Index of the last lomem page in the page array.
  132. * @num_pages: Number of pages in the page array.
  133. * @bdev: Pointer to the current struct ttm_bo_device.
  134. * @be: Pointer to the ttm backend.
  135. * @tsk: The task for user ttm.
  136. * @start: virtual address for user ttm.
  137. * @swap_storage: Pointer to shmem struct file for swap storage.
  138. * @caching_state: The current caching state of the pages.
  139. * @state: The current binding state of the pages.
  140. *
  141. * This is a structure holding the pages, caching- and aperture binding
  142. * status for a buffer object that isn't backed by fixed (VRAM / AGP)
  143. * memory.
  144. */
  145. struct ttm_tt {
  146. struct page *dummy_read_page;
  147. struct page **pages;
  148. long first_himem_page;
  149. long last_lomem_page;
  150. uint32_t page_flags;
  151. unsigned long num_pages;
  152. struct ttm_bo_global *glob;
  153. struct ttm_backend *be;
  154. struct task_struct *tsk;
  155. unsigned long start;
  156. struct file *swap_storage;
  157. enum ttm_caching_state caching_state;
  158. enum {
  159. tt_bound,
  160. tt_unbound,
  161. tt_unpopulated,
  162. } state;
  163. };
  164. #define TTM_MEMTYPE_FLAG_FIXED (1 << 0) /* Fixed (on-card) PCI memory */
  165. #define TTM_MEMTYPE_FLAG_MAPPABLE (1 << 1) /* Memory mappable */
  166. #define TTM_MEMTYPE_FLAG_CMA (1 << 3) /* Can't map aperture */
  167. /**
  168. * struct ttm_mem_type_manager
  169. *
  170. * @has_type: The memory type has been initialized.
  171. * @use_type: The memory type is enabled.
  172. * @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory
  173. * managed by this memory type.
  174. * @gpu_offset: If used, the GPU offset of the first managed page of
  175. * fixed memory or the first managed location in an aperture.
  176. * @size: Size of the managed region.
  177. * @available_caching: A mask of available caching types, TTM_PL_FLAG_XX,
  178. * as defined in ttm_placement_common.h
  179. * @default_caching: The default caching policy used for a buffer object
  180. * placed in this memory type if the user doesn't provide one.
  181. * @manager: The range manager used for this memory type. FIXME: If the aperture
  182. * has a page size different from the underlying system, the granularity
  183. * of this manager should take care of this. But the range allocating code
  184. * in ttm_bo.c needs to be modified for this.
  185. * @lru: The lru list for this memory type.
  186. *
  187. * This structure is used to identify and manage memory types for a device.
  188. * It's set up by the ttm_bo_driver::init_mem_type method.
  189. */
  190. struct ttm_mem_type_manager {
  191. /*
  192. * No protection. Constant from start.
  193. */
  194. bool has_type;
  195. bool use_type;
  196. uint32_t flags;
  197. unsigned long gpu_offset;
  198. uint64_t size;
  199. uint32_t available_caching;
  200. uint32_t default_caching;
  201. /*
  202. * Protected by the bdev->lru_lock.
  203. * TODO: Consider one lru_lock per ttm_mem_type_manager.
  204. * Plays ill with list removal, though.
  205. */
  206. struct drm_mm manager;
  207. struct list_head lru;
  208. };
  209. /**
  210. * struct ttm_bo_driver
  211. *
  212. * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
  213. * @invalidate_caches: Callback to invalidate read caches when a buffer object
  214. * has been evicted.
  215. * @init_mem_type: Callback to initialize a struct ttm_mem_type_manager
  216. * structure.
  217. * @evict_flags: Callback to obtain placement flags when a buffer is evicted.
  218. * @move: Callback for a driver to hook in accelerated functions to
  219. * move a buffer.
  220. * If set to NULL, a potentially slow memcpy() move is used.
  221. * @sync_obj_signaled: See ttm_fence_api.h
  222. * @sync_obj_wait: See ttm_fence_api.h
  223. * @sync_obj_flush: See ttm_fence_api.h
  224. * @sync_obj_unref: See ttm_fence_api.h
  225. * @sync_obj_ref: See ttm_fence_api.h
  226. */
  227. struct ttm_bo_driver {
  228. /**
  229. * struct ttm_bo_driver member create_ttm_backend_entry
  230. *
  231. * @bdev: The buffer object device.
  232. *
  233. * Create a driver specific struct ttm_backend.
  234. */
  235. struct ttm_backend *(*create_ttm_backend_entry)
  236. (struct ttm_bo_device *bdev);
  237. /**
  238. * struct ttm_bo_driver member invalidate_caches
  239. *
  240. * @bdev: the buffer object device.
  241. * @flags: new placement of the rebound buffer object.
  242. *
  243. * A previosly evicted buffer has been rebound in a
  244. * potentially new location. Tell the driver that it might
  245. * consider invalidating read (texture) caches on the next command
  246. * submission as a consequence.
  247. */
  248. int (*invalidate_caches) (struct ttm_bo_device *bdev, uint32_t flags);
  249. int (*init_mem_type) (struct ttm_bo_device *bdev, uint32_t type,
  250. struct ttm_mem_type_manager *man);
  251. /**
  252. * struct ttm_bo_driver member evict_flags:
  253. *
  254. * @bo: the buffer object to be evicted
  255. *
  256. * Return the bo flags for a buffer which is not mapped to the hardware.
  257. * These will be placed in proposed_flags so that when the move is
  258. * finished, they'll end up in bo->mem.flags
  259. */
  260. void(*evict_flags) (struct ttm_buffer_object *bo,
  261. struct ttm_placement *placement);
  262. /**
  263. * struct ttm_bo_driver member move:
  264. *
  265. * @bo: the buffer to move
  266. * @evict: whether this motion is evicting the buffer from
  267. * the graphics address space
  268. * @interruptible: Use interruptible sleeps if possible when sleeping.
  269. * @no_wait: whether this should give up and return -EBUSY
  270. * if this move would require sleeping
  271. * @new_mem: the new memory region receiving the buffer
  272. *
  273. * Move a buffer between two memory regions.
  274. */
  275. int (*move) (struct ttm_buffer_object *bo,
  276. bool evict, bool interruptible,
  277. bool no_wait_reserve, bool no_wait_gpu,
  278. struct ttm_mem_reg *new_mem);
  279. /**
  280. * struct ttm_bo_driver_member verify_access
  281. *
  282. * @bo: Pointer to a buffer object.
  283. * @filp: Pointer to a struct file trying to access the object.
  284. *
  285. * Called from the map / write / read methods to verify that the
  286. * caller is permitted to access the buffer object.
  287. * This member may be set to NULL, which will refuse this kind of
  288. * access for all buffer objects.
  289. * This function should return 0 if access is granted, -EPERM otherwise.
  290. */
  291. int (*verify_access) (struct ttm_buffer_object *bo,
  292. struct file *filp);
  293. /**
  294. * In case a driver writer dislikes the TTM fence objects,
  295. * the driver writer can replace those with sync objects of
  296. * his / her own. If it turns out that no driver writer is
  297. * using these. I suggest we remove these hooks and plug in
  298. * fences directly. The bo driver needs the following functionality:
  299. * See the corresponding functions in the fence object API
  300. * documentation.
  301. */
  302. bool (*sync_obj_signaled) (void *sync_obj, void *sync_arg);
  303. int (*sync_obj_wait) (void *sync_obj, void *sync_arg,
  304. bool lazy, bool interruptible);
  305. int (*sync_obj_flush) (void *sync_obj, void *sync_arg);
  306. void (*sync_obj_unref) (void **sync_obj);
  307. void *(*sync_obj_ref) (void *sync_obj);
  308. /* hook to notify driver about a driver move so it
  309. * can do tiling things */
  310. void (*move_notify)(struct ttm_buffer_object *bo,
  311. struct ttm_mem_reg *new_mem);
  312. /* notify the driver we are taking a fault on this BO
  313. * and have reserved it */
  314. int (*fault_reserve_notify)(struct ttm_buffer_object *bo);
  315. /**
  316. * notify the driver that we're about to swap out this bo
  317. */
  318. void (*swap_notify) (struct ttm_buffer_object *bo);
  319. /**
  320. * Driver callback on when mapping io memory (for bo_move_memcpy
  321. * for instance). TTM will take care to call io_mem_free whenever
  322. * the mapping is not use anymore. io_mem_reserve & io_mem_free
  323. * are balanced.
  324. */
  325. int (*io_mem_reserve)(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem);
  326. void (*io_mem_free)(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem);
  327. };
  328. /**
  329. * struct ttm_bo_global_ref - Argument to initialize a struct ttm_bo_global.
  330. */
  331. struct ttm_bo_global_ref {
  332. struct ttm_global_reference ref;
  333. struct ttm_mem_global *mem_glob;
  334. };
  335. /**
  336. * struct ttm_bo_global - Buffer object driver global data.
  337. *
  338. * @mem_glob: Pointer to a struct ttm_mem_global object for accounting.
  339. * @dummy_read_page: Pointer to a dummy page used for mapping requests
  340. * of unpopulated pages.
  341. * @shrink: A shrink callback object used for buffer object swap.
  342. * @ttm_bo_extra_size: Extra size (sizeof(struct ttm_buffer_object) excluded)
  343. * used by a buffer object. This is excluding page arrays and backing pages.
  344. * @ttm_bo_size: This is @ttm_bo_extra_size + sizeof(struct ttm_buffer_object).
  345. * @device_list_mutex: Mutex protecting the device list.
  346. * This mutex is held while traversing the device list for pm options.
  347. * @lru_lock: Spinlock protecting the bo subsystem lru lists.
  348. * @device_list: List of buffer object devices.
  349. * @swap_lru: Lru list of buffer objects used for swapping.
  350. */
  351. struct ttm_bo_global {
  352. /**
  353. * Constant after init.
  354. */
  355. struct kobject kobj;
  356. struct ttm_mem_global *mem_glob;
  357. struct page *dummy_read_page;
  358. struct ttm_mem_shrink shrink;
  359. size_t ttm_bo_extra_size;
  360. size_t ttm_bo_size;
  361. struct mutex device_list_mutex;
  362. spinlock_t lru_lock;
  363. /**
  364. * Protected by device_list_mutex.
  365. */
  366. struct list_head device_list;
  367. /**
  368. * Protected by the lru_lock.
  369. */
  370. struct list_head swap_lru;
  371. /**
  372. * Internal protection.
  373. */
  374. atomic_t bo_count;
  375. };
  376. #define TTM_NUM_MEM_TYPES 8
  377. #define TTM_BO_PRIV_FLAG_MOVING 0 /* Buffer object is moving and needs
  378. idling before CPU mapping */
  379. #define TTM_BO_PRIV_FLAG_MAX 1
  380. /**
  381. * struct ttm_bo_device - Buffer object driver device-specific data.
  382. *
  383. * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
  384. * @man: An array of mem_type_managers.
  385. * @addr_space_mm: Range manager for the device address space.
  386. * lru_lock: Spinlock that protects the buffer+device lru lists and
  387. * ddestroy lists.
  388. * @nice_mode: Try nicely to wait for buffer idle when cleaning a manager.
  389. * If a GPU lockup has been detected, this is forced to 0.
  390. * @dev_mapping: A pointer to the struct address_space representing the
  391. * device address space.
  392. * @wq: Work queue structure for the delayed delete workqueue.
  393. *
  394. */
  395. struct ttm_bo_device {
  396. /*
  397. * Constant after bo device init / atomic.
  398. */
  399. struct list_head device_list;
  400. struct ttm_bo_global *glob;
  401. struct ttm_bo_driver *driver;
  402. rwlock_t vm_lock;
  403. struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];
  404. /*
  405. * Protected by the vm lock.
  406. */
  407. struct rb_root addr_space_rb;
  408. struct drm_mm addr_space_mm;
  409. /*
  410. * Protected by the global:lru lock.
  411. */
  412. struct list_head ddestroy;
  413. /*
  414. * Protected by load / firstopen / lastclose /unload sync.
  415. */
  416. bool nice_mode;
  417. struct address_space *dev_mapping;
  418. /*
  419. * Internal protection.
  420. */
  421. struct delayed_work wq;
  422. bool need_dma32;
  423. };
  424. /**
  425. * ttm_flag_masked
  426. *
  427. * @old: Pointer to the result and original value.
  428. * @new: New value of bits.
  429. * @mask: Mask of bits to change.
  430. *
  431. * Convenience function to change a number of bits identified by a mask.
  432. */
  433. static inline uint32_t
  434. ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
  435. {
  436. *old ^= (*old ^ new) & mask;
  437. return *old;
  438. }
  439. /**
  440. * ttm_tt_create
  441. *
  442. * @bdev: pointer to a struct ttm_bo_device:
  443. * @size: Size of the data needed backing.
  444. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  445. * @dummy_read_page: See struct ttm_bo_device.
  446. *
  447. * Create a struct ttm_tt to back data with system memory pages.
  448. * No pages are actually allocated.
  449. * Returns:
  450. * NULL: Out of memory.
  451. */
  452. extern struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev,
  453. unsigned long size,
  454. uint32_t page_flags,
  455. struct page *dummy_read_page);
  456. /**
  457. * ttm_tt_set_user:
  458. *
  459. * @ttm: The struct ttm_tt to populate.
  460. * @tsk: A struct task_struct for which @start is a valid user-space address.
  461. * @start: A valid user-space address.
  462. * @num_pages: Size in pages of the user memory area.
  463. *
  464. * Populate a struct ttm_tt with a user-space memory area after first pinning
  465. * the pages backing it.
  466. * Returns:
  467. * !0: Error.
  468. */
  469. extern int ttm_tt_set_user(struct ttm_tt *ttm,
  470. struct task_struct *tsk,
  471. unsigned long start, unsigned long num_pages);
  472. /**
  473. * ttm_ttm_bind:
  474. *
  475. * @ttm: The struct ttm_tt containing backing pages.
  476. * @bo_mem: The struct ttm_mem_reg identifying the binding location.
  477. *
  478. * Bind the pages of @ttm to an aperture location identified by @bo_mem
  479. */
  480. extern int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
  481. /**
  482. * ttm_tt_populate:
  483. *
  484. * @ttm: The struct ttm_tt to contain the backing pages.
  485. *
  486. * Add backing pages to all of @ttm
  487. */
  488. extern int ttm_tt_populate(struct ttm_tt *ttm);
  489. /**
  490. * ttm_ttm_destroy:
  491. *
  492. * @ttm: The struct ttm_tt.
  493. *
  494. * Unbind, unpopulate and destroy a struct ttm_tt.
  495. */
  496. extern void ttm_tt_destroy(struct ttm_tt *ttm);
  497. /**
  498. * ttm_ttm_unbind:
  499. *
  500. * @ttm: The struct ttm_tt.
  501. *
  502. * Unbind a struct ttm_tt.
  503. */
  504. extern void ttm_tt_unbind(struct ttm_tt *ttm);
  505. /**
  506. * ttm_ttm_destroy:
  507. *
  508. * @ttm: The struct ttm_tt.
  509. * @index: Index of the desired page.
  510. *
  511. * Return a pointer to the struct page backing @ttm at page
  512. * index @index. If the page is unpopulated, one will be allocated to
  513. * populate that index.
  514. *
  515. * Returns:
  516. * NULL on OOM.
  517. */
  518. extern struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index);
  519. /**
  520. * ttm_tt_cache_flush:
  521. *
  522. * @pages: An array of pointers to struct page:s to flush.
  523. * @num_pages: Number of pages to flush.
  524. *
  525. * Flush the data of the indicated pages from the cpu caches.
  526. * This is used when changing caching attributes of the pages from
  527. * cache-coherent.
  528. */
  529. extern void ttm_tt_cache_flush(struct page *pages[], unsigned long num_pages);
  530. /**
  531. * ttm_tt_set_placement_caching:
  532. *
  533. * @ttm A struct ttm_tt the backing pages of which will change caching policy.
  534. * @placement: Flag indicating the desired caching policy.
  535. *
  536. * This function will change caching policy of any default kernel mappings of
  537. * the pages backing @ttm. If changing from cached to uncached or
  538. * write-combined,
  539. * all CPU caches will first be flushed to make sure the data of the pages
  540. * hit RAM. This function may be very costly as it involves global TLB
  541. * and cache flushes and potential page splitting / combining.
  542. */
  543. extern int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);
  544. extern int ttm_tt_swapout(struct ttm_tt *ttm,
  545. struct file *persistant_swap_storage);
  546. /*
  547. * ttm_bo.c
  548. */
  549. /**
  550. * ttm_mem_reg_is_pci
  551. *
  552. * @bdev: Pointer to a struct ttm_bo_device.
  553. * @mem: A valid struct ttm_mem_reg.
  554. *
  555. * Returns true if the memory described by @mem is PCI memory,
  556. * false otherwise.
  557. */
  558. extern bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev,
  559. struct ttm_mem_reg *mem);
  560. /**
  561. * ttm_bo_mem_space
  562. *
  563. * @bo: Pointer to a struct ttm_buffer_object. the data of which
  564. * we want to allocate space for.
  565. * @proposed_placement: Proposed new placement for the buffer object.
  566. * @mem: A struct ttm_mem_reg.
  567. * @interruptible: Sleep interruptible when sliping.
  568. * @no_wait_reserve: Return immediately if other buffers are busy.
  569. * @no_wait_gpu: Return immediately if the GPU is busy.
  570. *
  571. * Allocate memory space for the buffer object pointed to by @bo, using
  572. * the placement flags in @mem, potentially evicting other idle buffer objects.
  573. * This function may sleep while waiting for space to become available.
  574. * Returns:
  575. * -EBUSY: No space available (only if no_wait == 1).
  576. * -ENOMEM: Could not allocate memory for the buffer object, either due to
  577. * fragmentation or concurrent allocators.
  578. * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
  579. */
  580. extern int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  581. struct ttm_placement *placement,
  582. struct ttm_mem_reg *mem,
  583. bool interruptible,
  584. bool no_wait_reserve, bool no_wait_gpu);
  585. /**
  586. * ttm_bo_wait_for_cpu
  587. *
  588. * @bo: Pointer to a struct ttm_buffer_object.
  589. * @no_wait: Don't sleep while waiting.
  590. *
  591. * Wait until a buffer object is no longer sync'ed for CPU access.
  592. * Returns:
  593. * -EBUSY: Buffer object was sync'ed for CPU access. (only if no_wait == 1).
  594. * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
  595. */
  596. extern int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait);
  597. /**
  598. * ttm_bo_pci_offset - Get the PCI offset for the buffer object memory.
  599. *
  600. * @bo Pointer to a struct ttm_buffer_object.
  601. * @bus_base On return the base of the PCI region
  602. * @bus_offset On return the byte offset into the PCI region
  603. * @bus_size On return the byte size of the buffer object or zero if
  604. * the buffer object memory is not accessible through a PCI region.
  605. *
  606. * Returns:
  607. * -EINVAL if the buffer object is currently not mappable.
  608. * 0 otherwise.
  609. */
  610. extern int ttm_bo_pci_offset(struct ttm_bo_device *bdev,
  611. struct ttm_mem_reg *mem,
  612. unsigned long *bus_base,
  613. unsigned long *bus_offset,
  614. unsigned long *bus_size);
  615. extern int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
  616. struct ttm_mem_reg *mem);
  617. extern void ttm_mem_io_free(struct ttm_bo_device *bdev,
  618. struct ttm_mem_reg *mem);
  619. extern void ttm_bo_global_release(struct ttm_global_reference *ref);
  620. extern int ttm_bo_global_init(struct ttm_global_reference *ref);
  621. extern int ttm_bo_device_release(struct ttm_bo_device *bdev);
  622. /**
  623. * ttm_bo_device_init
  624. *
  625. * @bdev: A pointer to a struct ttm_bo_device to initialize.
  626. * @mem_global: A pointer to an initialized struct ttm_mem_global.
  627. * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
  628. * @file_page_offset: Offset into the device address space that is available
  629. * for buffer data. This ensures compatibility with other users of the
  630. * address space.
  631. *
  632. * Initializes a struct ttm_bo_device:
  633. * Returns:
  634. * !0: Failure.
  635. */
  636. extern int ttm_bo_device_init(struct ttm_bo_device *bdev,
  637. struct ttm_bo_global *glob,
  638. struct ttm_bo_driver *driver,
  639. uint64_t file_page_offset, bool need_dma32);
  640. /**
  641. * ttm_bo_unmap_virtual
  642. *
  643. * @bo: tear down the virtual mappings for this BO
  644. */
  645. extern void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
  646. /**
  647. * ttm_bo_reserve:
  648. *
  649. * @bo: A pointer to a struct ttm_buffer_object.
  650. * @interruptible: Sleep interruptible if waiting.
  651. * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  652. * @use_sequence: If @bo is already reserved, Only sleep waiting for
  653. * it to become unreserved if @sequence < (@bo)->sequence.
  654. *
  655. * Locks a buffer object for validation. (Or prevents other processes from
  656. * locking it for validation) and removes it from lru lists, while taking
  657. * a number of measures to prevent deadlocks.
  658. *
  659. * Deadlocks may occur when two processes try to reserve multiple buffers in
  660. * different order, either by will or as a result of a buffer being evicted
  661. * to make room for a buffer already reserved. (Buffers are reserved before
  662. * they are evicted). The following algorithm prevents such deadlocks from
  663. * occuring:
  664. * 1) Buffers are reserved with the lru spinlock held. Upon successful
  665. * reservation they are removed from the lru list. This stops a reserved buffer
  666. * from being evicted. However the lru spinlock is released between the time
  667. * a buffer is selected for eviction and the time it is reserved.
  668. * Therefore a check is made when a buffer is reserved for eviction, that it
  669. * is still the first buffer in the lru list, before it is removed from the
  670. * list. @check_lru == 1 forces this check. If it fails, the function returns
  671. * -EINVAL, and the caller should then choose a new buffer to evict and repeat
  672. * the procedure.
  673. * 2) Processes attempting to reserve multiple buffers other than for eviction,
  674. * (typically execbuf), should first obtain a unique 32-bit
  675. * validation sequence number,
  676. * and call this function with @use_sequence == 1 and @sequence == the unique
  677. * sequence number. If upon call of this function, the buffer object is already
  678. * reserved, the validation sequence is checked against the validation
  679. * sequence of the process currently reserving the buffer,
  680. * and if the current validation sequence is greater than that of the process
  681. * holding the reservation, the function returns -EAGAIN. Otherwise it sleeps
  682. * waiting for the buffer to become unreserved, after which it retries
  683. * reserving.
  684. * The caller should, when receiving an -EAGAIN error
  685. * release all its buffer reservations, wait for @bo to become unreserved, and
  686. * then rerun the validation with the same validation sequence. This procedure
  687. * will always guarantee that the process with the lowest validation sequence
  688. * will eventually succeed, preventing both deadlocks and starvation.
  689. *
  690. * Returns:
  691. * -EAGAIN: The reservation may cause a deadlock.
  692. * Release all buffer reservations, wait for @bo to become unreserved and
  693. * try again. (only if use_sequence == 1).
  694. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  695. * a signal. Release all buffer reservations and return to user-space.
  696. */
  697. extern int ttm_bo_reserve(struct ttm_buffer_object *bo,
  698. bool interruptible,
  699. bool no_wait, bool use_sequence, uint32_t sequence);
  700. /**
  701. * ttm_bo_unreserve
  702. *
  703. * @bo: A pointer to a struct ttm_buffer_object.
  704. *
  705. * Unreserve a previous reservation of @bo.
  706. */
  707. extern void ttm_bo_unreserve(struct ttm_buffer_object *bo);
  708. /**
  709. * ttm_bo_wait_unreserved
  710. *
  711. * @bo: A pointer to a struct ttm_buffer_object.
  712. *
  713. * Wait for a struct ttm_buffer_object to become unreserved.
  714. * This is typically used in the execbuf code to relax cpu-usage when
  715. * a potential deadlock condition backoff.
  716. */
  717. extern int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo,
  718. bool interruptible);
  719. /*
  720. * ttm_bo_util.c
  721. */
  722. /**
  723. * ttm_bo_move_ttm
  724. *
  725. * @bo: A pointer to a struct ttm_buffer_object.
  726. * @evict: 1: This is an eviction. Don't try to pipeline.
  727. * @no_wait_reserve: Return immediately if other buffers are busy.
  728. * @no_wait_gpu: Return immediately if the GPU is busy.
  729. * @new_mem: struct ttm_mem_reg indicating where to move.
  730. *
  731. * Optimized move function for a buffer object with both old and
  732. * new placement backed by a TTM. The function will, if successful,
  733. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  734. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  735. * data remains untouched, and it's up to the caller to free the
  736. * memory space indicated by @new_mem.
  737. * Returns:
  738. * !0: Failure.
  739. */
  740. extern int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
  741. bool evict, bool no_wait_reserve,
  742. bool no_wait_gpu, struct ttm_mem_reg *new_mem);
  743. /**
  744. * ttm_bo_move_memcpy
  745. *
  746. * @bo: A pointer to a struct ttm_buffer_object.
  747. * @evict: 1: This is an eviction. Don't try to pipeline.
  748. * @no_wait_reserve: Return immediately if other buffers are busy.
  749. * @no_wait_gpu: Return immediately if the GPU is busy.
  750. * @new_mem: struct ttm_mem_reg indicating where to move.
  751. *
  752. * Fallback move function for a mappable buffer object in mappable memory.
  753. * The function will, if successful,
  754. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  755. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  756. * data remains untouched, and it's up to the caller to free the
  757. * memory space indicated by @new_mem.
  758. * Returns:
  759. * !0: Failure.
  760. */
  761. extern int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  762. bool evict, bool no_wait_reserve,
  763. bool no_wait_gpu, struct ttm_mem_reg *new_mem);
  764. /**
  765. * ttm_bo_free_old_node
  766. *
  767. * @bo: A pointer to a struct ttm_buffer_object.
  768. *
  769. * Utility function to free an old placement after a successful move.
  770. */
  771. extern void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
  772. /**
  773. * ttm_bo_move_accel_cleanup.
  774. *
  775. * @bo: A pointer to a struct ttm_buffer_object.
  776. * @sync_obj: A sync object that signals when moving is complete.
  777. * @sync_obj_arg: An argument to pass to the sync object idle / wait
  778. * functions.
  779. * @evict: This is an evict move. Don't return until the buffer is idle.
  780. * @no_wait_reserve: Return immediately if other buffers are busy.
  781. * @no_wait_gpu: Return immediately if the GPU is busy.
  782. * @new_mem: struct ttm_mem_reg indicating where to move.
  783. *
  784. * Accelerated move function to be called when an accelerated move
  785. * has been scheduled. The function will create a new temporary buffer object
  786. * representing the old placement, and put the sync object on both buffer
  787. * objects. After that the newly created buffer object is unref'd to be
  788. * destroyed when the move is complete. This will help pipeline
  789. * buffer moves.
  790. */
  791. extern int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  792. void *sync_obj,
  793. void *sync_obj_arg,
  794. bool evict, bool no_wait_reserve,
  795. bool no_wait_gpu,
  796. struct ttm_mem_reg *new_mem);
  797. /**
  798. * ttm_io_prot
  799. *
  800. * @c_state: Caching state.
  801. * @tmp: Page protection flag for a normal, cached mapping.
  802. *
  803. * Utility function that returns the pgprot_t that should be used for
  804. * setting up a PTE with the caching model indicated by @c_state.
  805. */
  806. extern pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);
  807. #if (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))
  808. #define TTM_HAS_AGP
  809. #include <linux/agp_backend.h>
  810. /**
  811. * ttm_agp_backend_init
  812. *
  813. * @bdev: Pointer to a struct ttm_bo_device.
  814. * @bridge: The agp bridge this device is sitting on.
  815. *
  816. * Create a TTM backend that uses the indicated AGP bridge as an aperture
  817. * for TT memory. This function uses the linux agpgart interface to
  818. * bind and unbind memory backing a ttm_tt.
  819. */
  820. extern struct ttm_backend *ttm_agp_backend_init(struct ttm_bo_device *bdev,
  821. struct agp_bridge_data *bridge);
  822. #endif
  823. #endif