ttm_bo_driver.h 32 KB

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