ttm_bo_driver.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. * @fence_lock: Protects the synchronizing members on *all* bos belonging
  465. * to this device.
  466. * @addr_space_mm: Range manager for the device address space.
  467. * lru_lock: Spinlock that protects the buffer+device lru lists and
  468. * ddestroy lists.
  469. * @nice_mode: Try nicely to wait for buffer idle when cleaning a manager.
  470. * If a GPU lockup has been detected, this is forced to 0.
  471. * @dev_mapping: A pointer to the struct address_space representing the
  472. * device address space.
  473. * @wq: Work queue structure for the delayed delete workqueue.
  474. *
  475. */
  476. struct ttm_bo_device {
  477. /*
  478. * Constant after bo device init / atomic.
  479. */
  480. struct list_head device_list;
  481. struct ttm_bo_global *glob;
  482. struct ttm_bo_driver *driver;
  483. rwlock_t vm_lock;
  484. struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];
  485. spinlock_t fence_lock;
  486. /*
  487. * Protected by the vm lock.
  488. */
  489. struct rb_root addr_space_rb;
  490. struct drm_mm addr_space_mm;
  491. /*
  492. * Protected by the global:lru lock.
  493. */
  494. struct list_head ddestroy;
  495. /*
  496. * Protected by load / firstopen / lastclose /unload sync.
  497. */
  498. bool nice_mode;
  499. struct address_space *dev_mapping;
  500. /*
  501. * Internal protection.
  502. */
  503. struct delayed_work wq;
  504. bool need_dma32;
  505. };
  506. /**
  507. * ttm_flag_masked
  508. *
  509. * @old: Pointer to the result and original value.
  510. * @new: New value of bits.
  511. * @mask: Mask of bits to change.
  512. *
  513. * Convenience function to change a number of bits identified by a mask.
  514. */
  515. static inline uint32_t
  516. ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
  517. {
  518. *old ^= (*old ^ new) & mask;
  519. return *old;
  520. }
  521. /**
  522. * ttm_tt_create
  523. *
  524. * @bdev: pointer to a struct ttm_bo_device:
  525. * @size: Size of the data needed backing.
  526. * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
  527. * @dummy_read_page: See struct ttm_bo_device.
  528. *
  529. * Create a struct ttm_tt to back data with system memory pages.
  530. * No pages are actually allocated.
  531. * Returns:
  532. * NULL: Out of memory.
  533. */
  534. extern struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev,
  535. unsigned long size,
  536. uint32_t page_flags,
  537. struct page *dummy_read_page);
  538. /**
  539. * ttm_tt_set_user:
  540. *
  541. * @ttm: The struct ttm_tt to populate.
  542. * @tsk: A struct task_struct for which @start is a valid user-space address.
  543. * @start: A valid user-space address.
  544. * @num_pages: Size in pages of the user memory area.
  545. *
  546. * Populate a struct ttm_tt with a user-space memory area after first pinning
  547. * the pages backing it.
  548. * Returns:
  549. * !0: Error.
  550. */
  551. extern int ttm_tt_set_user(struct ttm_tt *ttm,
  552. struct task_struct *tsk,
  553. unsigned long start, unsigned long num_pages);
  554. /**
  555. * ttm_ttm_bind:
  556. *
  557. * @ttm: The struct ttm_tt containing backing pages.
  558. * @bo_mem: The struct ttm_mem_reg identifying the binding location.
  559. *
  560. * Bind the pages of @ttm to an aperture location identified by @bo_mem
  561. */
  562. extern int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
  563. /**
  564. * ttm_tt_populate:
  565. *
  566. * @ttm: The struct ttm_tt to contain the backing pages.
  567. *
  568. * Add backing pages to all of @ttm
  569. */
  570. extern int ttm_tt_populate(struct ttm_tt *ttm);
  571. /**
  572. * ttm_ttm_destroy:
  573. *
  574. * @ttm: The struct ttm_tt.
  575. *
  576. * Unbind, unpopulate and destroy a struct ttm_tt.
  577. */
  578. extern void ttm_tt_destroy(struct ttm_tt *ttm);
  579. /**
  580. * ttm_ttm_unbind:
  581. *
  582. * @ttm: The struct ttm_tt.
  583. *
  584. * Unbind a struct ttm_tt.
  585. */
  586. extern void ttm_tt_unbind(struct ttm_tt *ttm);
  587. /**
  588. * ttm_ttm_destroy:
  589. *
  590. * @ttm: The struct ttm_tt.
  591. * @index: Index of the desired page.
  592. *
  593. * Return a pointer to the struct page backing @ttm at page
  594. * index @index. If the page is unpopulated, one will be allocated to
  595. * populate that index.
  596. *
  597. * Returns:
  598. * NULL on OOM.
  599. */
  600. extern struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index);
  601. /**
  602. * ttm_tt_cache_flush:
  603. *
  604. * @pages: An array of pointers to struct page:s to flush.
  605. * @num_pages: Number of pages to flush.
  606. *
  607. * Flush the data of the indicated pages from the cpu caches.
  608. * This is used when changing caching attributes of the pages from
  609. * cache-coherent.
  610. */
  611. extern void ttm_tt_cache_flush(struct page *pages[], unsigned long num_pages);
  612. /**
  613. * ttm_tt_set_placement_caching:
  614. *
  615. * @ttm A struct ttm_tt the backing pages of which will change caching policy.
  616. * @placement: Flag indicating the desired caching policy.
  617. *
  618. * This function will change caching policy of any default kernel mappings of
  619. * the pages backing @ttm. If changing from cached to uncached or
  620. * write-combined,
  621. * all CPU caches will first be flushed to make sure the data of the pages
  622. * hit RAM. This function may be very costly as it involves global TLB
  623. * and cache flushes and potential page splitting / combining.
  624. */
  625. extern int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);
  626. extern int ttm_tt_swapout(struct ttm_tt *ttm,
  627. struct file *persistant_swap_storage);
  628. /*
  629. * ttm_bo.c
  630. */
  631. /**
  632. * ttm_mem_reg_is_pci
  633. *
  634. * @bdev: Pointer to a struct ttm_bo_device.
  635. * @mem: A valid struct ttm_mem_reg.
  636. *
  637. * Returns true if the memory described by @mem is PCI memory,
  638. * false otherwise.
  639. */
  640. extern bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev,
  641. struct ttm_mem_reg *mem);
  642. /**
  643. * ttm_bo_mem_space
  644. *
  645. * @bo: Pointer to a struct ttm_buffer_object. the data of which
  646. * we want to allocate space for.
  647. * @proposed_placement: Proposed new placement for the buffer object.
  648. * @mem: A struct ttm_mem_reg.
  649. * @interruptible: Sleep interruptible when sliping.
  650. * @no_wait_reserve: Return immediately if other buffers are busy.
  651. * @no_wait_gpu: Return immediately if the GPU is busy.
  652. *
  653. * Allocate memory space for the buffer object pointed to by @bo, using
  654. * the placement flags in @mem, potentially evicting other idle buffer objects.
  655. * This function may sleep while waiting for space to become available.
  656. * Returns:
  657. * -EBUSY: No space available (only if no_wait == 1).
  658. * -ENOMEM: Could not allocate memory for the buffer object, either due to
  659. * fragmentation or concurrent allocators.
  660. * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
  661. */
  662. extern int ttm_bo_mem_space(struct ttm_buffer_object *bo,
  663. struct ttm_placement *placement,
  664. struct ttm_mem_reg *mem,
  665. bool interruptible,
  666. bool no_wait_reserve, bool no_wait_gpu);
  667. extern void ttm_bo_mem_put(struct ttm_buffer_object *bo,
  668. struct ttm_mem_reg *mem);
  669. extern void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo,
  670. struct ttm_mem_reg *mem);
  671. /**
  672. * ttm_bo_wait_for_cpu
  673. *
  674. * @bo: Pointer to a struct ttm_buffer_object.
  675. * @no_wait: Don't sleep while waiting.
  676. *
  677. * Wait until a buffer object is no longer sync'ed for CPU access.
  678. * Returns:
  679. * -EBUSY: Buffer object was sync'ed for CPU access. (only if no_wait == 1).
  680. * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
  681. */
  682. extern int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait);
  683. /**
  684. * ttm_bo_pci_offset - Get the PCI offset for the buffer object memory.
  685. *
  686. * @bo Pointer to a struct ttm_buffer_object.
  687. * @bus_base On return the base of the PCI region
  688. * @bus_offset On return the byte offset into the PCI region
  689. * @bus_size On return the byte size of the buffer object or zero if
  690. * the buffer object memory is not accessible through a PCI region.
  691. *
  692. * Returns:
  693. * -EINVAL if the buffer object is currently not mappable.
  694. * 0 otherwise.
  695. */
  696. extern int ttm_bo_pci_offset(struct ttm_bo_device *bdev,
  697. struct ttm_mem_reg *mem,
  698. unsigned long *bus_base,
  699. unsigned long *bus_offset,
  700. unsigned long *bus_size);
  701. extern int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
  702. struct ttm_mem_reg *mem);
  703. extern void ttm_mem_io_free(struct ttm_bo_device *bdev,
  704. struct ttm_mem_reg *mem);
  705. extern void ttm_bo_global_release(struct drm_global_reference *ref);
  706. extern int ttm_bo_global_init(struct drm_global_reference *ref);
  707. extern int ttm_bo_device_release(struct ttm_bo_device *bdev);
  708. /**
  709. * ttm_bo_device_init
  710. *
  711. * @bdev: A pointer to a struct ttm_bo_device to initialize.
  712. * @mem_global: A pointer to an initialized struct ttm_mem_global.
  713. * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
  714. * @file_page_offset: Offset into the device address space that is available
  715. * for buffer data. This ensures compatibility with other users of the
  716. * address space.
  717. *
  718. * Initializes a struct ttm_bo_device:
  719. * Returns:
  720. * !0: Failure.
  721. */
  722. extern int ttm_bo_device_init(struct ttm_bo_device *bdev,
  723. struct ttm_bo_global *glob,
  724. struct ttm_bo_driver *driver,
  725. uint64_t file_page_offset, bool need_dma32);
  726. /**
  727. * ttm_bo_unmap_virtual
  728. *
  729. * @bo: tear down the virtual mappings for this BO
  730. */
  731. extern void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
  732. /**
  733. * ttm_bo_reserve:
  734. *
  735. * @bo: A pointer to a struct ttm_buffer_object.
  736. * @interruptible: Sleep interruptible if waiting.
  737. * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  738. * @use_sequence: If @bo is already reserved, Only sleep waiting for
  739. * it to become unreserved if @sequence < (@bo)->sequence.
  740. *
  741. * Locks a buffer object for validation. (Or prevents other processes from
  742. * locking it for validation) and removes it from lru lists, while taking
  743. * a number of measures to prevent deadlocks.
  744. *
  745. * Deadlocks may occur when two processes try to reserve multiple buffers in
  746. * different order, either by will or as a result of a buffer being evicted
  747. * to make room for a buffer already reserved. (Buffers are reserved before
  748. * they are evicted). The following algorithm prevents such deadlocks from
  749. * occuring:
  750. * 1) Buffers are reserved with the lru spinlock held. Upon successful
  751. * reservation they are removed from the lru list. This stops a reserved buffer
  752. * from being evicted. However the lru spinlock is released between the time
  753. * a buffer is selected for eviction and the time it is reserved.
  754. * Therefore a check is made when a buffer is reserved for eviction, that it
  755. * is still the first buffer in the lru list, before it is removed from the
  756. * list. @check_lru == 1 forces this check. If it fails, the function returns
  757. * -EINVAL, and the caller should then choose a new buffer to evict and repeat
  758. * the procedure.
  759. * 2) Processes attempting to reserve multiple buffers other than for eviction,
  760. * (typically execbuf), should first obtain a unique 32-bit
  761. * validation sequence number,
  762. * and call this function with @use_sequence == 1 and @sequence == the unique
  763. * sequence number. If upon call of this function, the buffer object is already
  764. * reserved, the validation sequence is checked against the validation
  765. * sequence of the process currently reserving the buffer,
  766. * and if the current validation sequence is greater than that of the process
  767. * holding the reservation, the function returns -EAGAIN. Otherwise it sleeps
  768. * waiting for the buffer to become unreserved, after which it retries
  769. * reserving.
  770. * The caller should, when receiving an -EAGAIN error
  771. * release all its buffer reservations, wait for @bo to become unreserved, and
  772. * then rerun the validation with the same validation sequence. This procedure
  773. * will always guarantee that the process with the lowest validation sequence
  774. * will eventually succeed, preventing both deadlocks and starvation.
  775. *
  776. * Returns:
  777. * -EAGAIN: The reservation may cause a deadlock.
  778. * Release all buffer reservations, wait for @bo to become unreserved and
  779. * try again. (only if use_sequence == 1).
  780. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  781. * a signal. Release all buffer reservations and return to user-space.
  782. * -EBUSY: The function needed to sleep, but @no_wait was true
  783. * -EDEADLK: Bo already reserved using @sequence. This error code will only
  784. * be returned if @use_sequence is set to true.
  785. */
  786. extern int ttm_bo_reserve(struct ttm_buffer_object *bo,
  787. bool interruptible,
  788. bool no_wait, bool use_sequence, uint32_t sequence);
  789. /**
  790. * ttm_bo_reserve_locked:
  791. *
  792. * @bo: A pointer to a struct ttm_buffer_object.
  793. * @interruptible: Sleep interruptible if waiting.
  794. * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  795. * @use_sequence: If @bo is already reserved, Only sleep waiting for
  796. * it to become unreserved if @sequence < (@bo)->sequence.
  797. *
  798. * Must be called with struct ttm_bo_global::lru_lock held,
  799. * and will not remove reserved buffers from the lru lists.
  800. * The function may release the LRU spinlock if it needs to sleep.
  801. * Otherwise identical to ttm_bo_reserve.
  802. *
  803. * Returns:
  804. * -EAGAIN: The reservation may cause a deadlock.
  805. * Release all buffer reservations, wait for @bo to become unreserved and
  806. * try again. (only if use_sequence == 1).
  807. * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  808. * a signal. Release all buffer reservations and return to user-space.
  809. * -EBUSY: The function needed to sleep, but @no_wait was true
  810. * -EDEADLK: Bo already reserved using @sequence. This error code will only
  811. * be returned if @use_sequence is set to true.
  812. */
  813. extern int ttm_bo_reserve_locked(struct ttm_buffer_object *bo,
  814. bool interruptible,
  815. bool no_wait, bool use_sequence,
  816. uint32_t sequence);
  817. /**
  818. * ttm_bo_unreserve
  819. *
  820. * @bo: A pointer to a struct ttm_buffer_object.
  821. *
  822. * Unreserve a previous reservation of @bo.
  823. */
  824. extern void ttm_bo_unreserve(struct ttm_buffer_object *bo);
  825. /**
  826. * ttm_bo_wait_unreserved
  827. *
  828. * @bo: A pointer to a struct ttm_buffer_object.
  829. *
  830. * Wait for a struct ttm_buffer_object to become unreserved.
  831. * This is typically used in the execbuf code to relax cpu-usage when
  832. * a potential deadlock condition backoff.
  833. */
  834. extern int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo,
  835. bool interruptible);
  836. /*
  837. * ttm_bo_util.c
  838. */
  839. /**
  840. * ttm_bo_move_ttm
  841. *
  842. * @bo: A pointer to a struct ttm_buffer_object.
  843. * @evict: 1: This is an eviction. Don't try to pipeline.
  844. * @no_wait_reserve: Return immediately if other buffers are busy.
  845. * @no_wait_gpu: Return immediately if the GPU is busy.
  846. * @new_mem: struct ttm_mem_reg indicating where to move.
  847. *
  848. * Optimized move function for a buffer object with both old and
  849. * new placement backed by a TTM. The function will, if successful,
  850. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  851. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  852. * data remains untouched, and it's up to the caller to free the
  853. * memory space indicated by @new_mem.
  854. * Returns:
  855. * !0: Failure.
  856. */
  857. extern int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
  858. bool evict, bool no_wait_reserve,
  859. bool no_wait_gpu, struct ttm_mem_reg *new_mem);
  860. /**
  861. * ttm_bo_move_memcpy
  862. *
  863. * @bo: A pointer to a struct ttm_buffer_object.
  864. * @evict: 1: This is an eviction. Don't try to pipeline.
  865. * @no_wait_reserve: Return immediately if other buffers are busy.
  866. * @no_wait_gpu: Return immediately if the GPU is busy.
  867. * @new_mem: struct ttm_mem_reg indicating where to move.
  868. *
  869. * Fallback move function for a mappable buffer object in mappable memory.
  870. * The function will, if successful,
  871. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  872. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  873. * data remains untouched, and it's up to the caller to free the
  874. * memory space indicated by @new_mem.
  875. * Returns:
  876. * !0: Failure.
  877. */
  878. extern int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  879. bool evict, bool no_wait_reserve,
  880. bool no_wait_gpu, struct ttm_mem_reg *new_mem);
  881. /**
  882. * ttm_bo_free_old_node
  883. *
  884. * @bo: A pointer to a struct ttm_buffer_object.
  885. *
  886. * Utility function to free an old placement after a successful move.
  887. */
  888. extern void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
  889. /**
  890. * ttm_bo_move_accel_cleanup.
  891. *
  892. * @bo: A pointer to a struct ttm_buffer_object.
  893. * @sync_obj: A sync object that signals when moving is complete.
  894. * @sync_obj_arg: An argument to pass to the sync object idle / wait
  895. * functions.
  896. * @evict: This is an evict move. Don't return until the buffer is idle.
  897. * @no_wait_reserve: Return immediately if other buffers are busy.
  898. * @no_wait_gpu: Return immediately if the GPU is busy.
  899. * @new_mem: struct ttm_mem_reg indicating where to move.
  900. *
  901. * Accelerated move function to be called when an accelerated move
  902. * has been scheduled. The function will create a new temporary buffer object
  903. * representing the old placement, and put the sync object on both buffer
  904. * objects. After that the newly created buffer object is unref'd to be
  905. * destroyed when the move is complete. This will help pipeline
  906. * buffer moves.
  907. */
  908. extern int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  909. void *sync_obj,
  910. void *sync_obj_arg,
  911. bool evict, bool no_wait_reserve,
  912. bool no_wait_gpu,
  913. struct ttm_mem_reg *new_mem);
  914. /**
  915. * ttm_io_prot
  916. *
  917. * @c_state: Caching state.
  918. * @tmp: Page protection flag for a normal, cached mapping.
  919. *
  920. * Utility function that returns the pgprot_t that should be used for
  921. * setting up a PTE with the caching model indicated by @c_state.
  922. */
  923. extern pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);
  924. extern const struct ttm_mem_type_manager_func ttm_bo_manager_func;
  925. #if (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))
  926. #define TTM_HAS_AGP
  927. #include <linux/agp_backend.h>
  928. /**
  929. * ttm_agp_backend_init
  930. *
  931. * @bdev: Pointer to a struct ttm_bo_device.
  932. * @bridge: The agp bridge this device is sitting on.
  933. *
  934. * Create a TTM backend that uses the indicated AGP bridge as an aperture
  935. * for TT memory. This function uses the linux agpgart interface to
  936. * bind and unbind memory backing a ttm_tt.
  937. */
  938. extern struct ttm_backend *ttm_agp_backend_init(struct ttm_bo_device *bdev,
  939. struct agp_bridge_data *bridge);
  940. #endif
  941. #endif