ttm_bo_driver.h 32 KB

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