ttm_bo_driver.h 34 KB

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