ttm_bo_driver.h 28 KB

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