ttm_bo_driver.h 34 KB

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