ttm_bo_driver.h 30 KB

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