drm_gpuvm.h 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. /* SPDX-License-Identifier: GPL-2.0-only OR MIT */
  2. #ifndef __DRM_GPUVM_H__
  3. #define __DRM_GPUVM_H__
  4. /*
  5. * Copyright (c) 2022 Red Hat.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <linux/dma-resv.h>
  26. #include <linux/list.h>
  27. #include <linux/llist.h>
  28. #include <linux/rbtree.h>
  29. #include <linux/types.h>
  30. #include <drm/drm_device.h>
  31. #include <drm/drm_gem.h>
  32. #include <drm/drm_exec.h>
  33. struct drm_gpuvm;
  34. struct drm_gpuvm_bo;
  35. struct drm_gpuvm_ops;
  36. /**
  37. * enum drm_gpuva_flags - flags for struct drm_gpuva
  38. */
  39. enum drm_gpuva_flags {
  40. /**
  41. * @DRM_GPUVA_INVALIDATED:
  42. *
  43. * Flag indicating that the &drm_gpuva's backing GEM is invalidated.
  44. */
  45. DRM_GPUVA_INVALIDATED = (1 << 0),
  46. /**
  47. * @DRM_GPUVA_SPARSE:
  48. *
  49. * Flag indicating that the &drm_gpuva is a sparse mapping.
  50. */
  51. DRM_GPUVA_SPARSE = (1 << 1),
  52. /**
  53. * @DRM_GPUVA_USERBITS: user defined bits
  54. */
  55. DRM_GPUVA_USERBITS = (1 << 2),
  56. };
  57. /**
  58. * struct drm_gpuva - structure to track a GPU VA mapping
  59. *
  60. * This structure represents a GPU VA mapping and is associated with a
  61. * &drm_gpuvm.
  62. *
  63. * Typically, this structure is embedded in bigger driver structures.
  64. */
  65. struct drm_gpuva {
  66. /**
  67. * @vm: the &drm_gpuvm this object is associated with
  68. */
  69. struct drm_gpuvm *vm;
  70. /**
  71. * @vm_bo: the &drm_gpuvm_bo abstraction for the mapped
  72. * &drm_gem_object
  73. */
  74. struct drm_gpuvm_bo *vm_bo;
  75. /**
  76. * @flags: the &drm_gpuva_flags for this mapping
  77. */
  78. enum drm_gpuva_flags flags;
  79. /**
  80. * @va: structure containing the address and range of the &drm_gpuva
  81. */
  82. struct {
  83. /**
  84. * @va.addr: the start address
  85. */
  86. u64 addr;
  87. /*
  88. * @range: the range
  89. */
  90. u64 range;
  91. } va;
  92. /**
  93. * @gem: structure containing the &drm_gem_object and its offset
  94. */
  95. struct {
  96. /**
  97. * @gem.offset: the offset within the &drm_gem_object
  98. */
  99. u64 offset;
  100. /**
  101. * @gem.obj: the mapped &drm_gem_object
  102. */
  103. struct drm_gem_object *obj;
  104. /**
  105. * @gem.entry: the &list_head to attach this object to a &drm_gpuvm_bo
  106. */
  107. struct list_head entry;
  108. } gem;
  109. /**
  110. * @rb: structure containing data to store &drm_gpuvas in a rb-tree
  111. */
  112. struct {
  113. /**
  114. * @rb.node: the rb-tree node
  115. */
  116. struct rb_node node;
  117. /**
  118. * @rb.entry: The &list_head to additionally connect &drm_gpuvas
  119. * in the same order they appear in the interval tree. This is
  120. * useful to keep iterating &drm_gpuvas from a start node found
  121. * through the rb-tree while doing modifications on the rb-tree
  122. * itself.
  123. */
  124. struct list_head entry;
  125. /**
  126. * @rb.__subtree_last: needed by the interval tree, holding last-in-subtree
  127. */
  128. u64 __subtree_last;
  129. } rb;
  130. };
  131. int drm_gpuva_insert(struct drm_gpuvm *gpuvm, struct drm_gpuva *va);
  132. void drm_gpuva_remove(struct drm_gpuva *va);
  133. void drm_gpuva_link(struct drm_gpuva *va, struct drm_gpuvm_bo *vm_bo);
  134. void drm_gpuva_unlink(struct drm_gpuva *va);
  135. void drm_gpuva_unlink_defer(struct drm_gpuva *va);
  136. struct drm_gpuva *drm_gpuva_find(struct drm_gpuvm *gpuvm,
  137. u64 addr, u64 range);
  138. struct drm_gpuva *drm_gpuva_find_first(struct drm_gpuvm *gpuvm,
  139. u64 addr, u64 range);
  140. struct drm_gpuva *drm_gpuva_find_prev(struct drm_gpuvm *gpuvm, u64 start);
  141. struct drm_gpuva *drm_gpuva_find_next(struct drm_gpuvm *gpuvm, u64 end);
  142. /**
  143. * drm_gpuva_invalidate() - sets whether the backing GEM of this &drm_gpuva is
  144. * invalidated
  145. * @va: the &drm_gpuva to set the invalidate flag for
  146. * @invalidate: indicates whether the &drm_gpuva is invalidated
  147. */
  148. static inline void drm_gpuva_invalidate(struct drm_gpuva *va, bool invalidate)
  149. {
  150. if (invalidate)
  151. va->flags |= DRM_GPUVA_INVALIDATED;
  152. else
  153. va->flags &= ~DRM_GPUVA_INVALIDATED;
  154. }
  155. /**
  156. * drm_gpuva_invalidated() - indicates whether the backing BO of this &drm_gpuva
  157. * is invalidated
  158. * @va: the &drm_gpuva to check
  159. *
  160. * Returns: %true if the GPU VA is invalidated, %false otherwise
  161. */
  162. static inline bool drm_gpuva_invalidated(struct drm_gpuva *va)
  163. {
  164. return va->flags & DRM_GPUVA_INVALIDATED;
  165. }
  166. /**
  167. * enum drm_gpuvm_flags - flags for struct drm_gpuvm
  168. */
  169. enum drm_gpuvm_flags {
  170. /**
  171. * @DRM_GPUVM_RESV_PROTECTED: GPUVM is protected externally by the
  172. * GPUVM's &dma_resv lock
  173. */
  174. DRM_GPUVM_RESV_PROTECTED = BIT(0),
  175. /**
  176. * @DRM_GPUVM_IMMEDIATE_MODE: use the locking scheme for GEMs designed
  177. * for modifying the GPUVM during the fence signalling path
  178. *
  179. * When set, gpuva.lock is used to protect gpuva.list in all GEM
  180. * objects associated with this GPUVM. Otherwise, the GEMs dma-resv is
  181. * used.
  182. */
  183. DRM_GPUVM_IMMEDIATE_MODE = BIT(1),
  184. /**
  185. * @DRM_GPUVM_USERBITS: user defined bits
  186. */
  187. DRM_GPUVM_USERBITS = BIT(2),
  188. };
  189. /**
  190. * struct drm_gpuvm - DRM GPU VA Manager
  191. *
  192. * The DRM GPU VA Manager keeps track of a GPU's virtual address space by using
  193. * &maple_tree structures. Typically, this structure is embedded in bigger
  194. * driver structures.
  195. *
  196. * Drivers can pass addresses and ranges in an arbitrary unit, e.g. bytes or
  197. * pages.
  198. *
  199. * There should be one manager instance per GPU virtual address space.
  200. */
  201. struct drm_gpuvm {
  202. /**
  203. * @name: the name of the DRM GPU VA space
  204. */
  205. const char *name;
  206. /**
  207. * @flags: the &drm_gpuvm_flags of this GPUVM
  208. */
  209. enum drm_gpuvm_flags flags;
  210. /**
  211. * @drm: the &drm_device this VM lives in
  212. */
  213. struct drm_device *drm;
  214. /**
  215. * @mm_start: start of the VA space
  216. */
  217. u64 mm_start;
  218. /**
  219. * @mm_range: length of the VA space
  220. */
  221. u64 mm_range;
  222. /**
  223. * @rb: structures to track &drm_gpuva entries
  224. */
  225. struct {
  226. /**
  227. * @rb.tree: the rb-tree to track GPU VA mappings
  228. */
  229. struct rb_root_cached tree;
  230. /**
  231. * @rb.list: the &list_head to track GPU VA mappings
  232. */
  233. struct list_head list;
  234. } rb;
  235. /**
  236. * @kref: reference count of this object
  237. */
  238. struct kref kref;
  239. /**
  240. * @kernel_alloc_node:
  241. *
  242. * &drm_gpuva representing the address space cutout reserved for
  243. * the kernel
  244. */
  245. struct drm_gpuva kernel_alloc_node;
  246. /**
  247. * @ops: &drm_gpuvm_ops providing the split/merge steps to drivers
  248. */
  249. const struct drm_gpuvm_ops *ops;
  250. /**
  251. * @r_obj: Resv GEM object; representing the GPUVM's common &dma_resv.
  252. */
  253. struct drm_gem_object *r_obj;
  254. /**
  255. * @extobj: structure holding the extobj list
  256. */
  257. struct {
  258. /**
  259. * @extobj.list: &list_head storing &drm_gpuvm_bos serving as
  260. * external object
  261. */
  262. struct list_head list;
  263. /**
  264. * @extobj.local_list: pointer to the local list temporarily
  265. * storing entries from the external object list
  266. */
  267. struct list_head *local_list;
  268. /**
  269. * @extobj.lock: spinlock to protect the extobj list
  270. */
  271. spinlock_t lock;
  272. } extobj;
  273. /**
  274. * @evict: structure holding the evict list and evict list lock
  275. */
  276. struct {
  277. /**
  278. * @evict.list: &list_head storing &drm_gpuvm_bos currently
  279. * being evicted
  280. */
  281. struct list_head list;
  282. /**
  283. * @evict.local_list: pointer to the local list temporarily
  284. * storing entries from the evicted object list
  285. */
  286. struct list_head *local_list;
  287. /**
  288. * @evict.lock: spinlock to protect the evict list
  289. */
  290. spinlock_t lock;
  291. } evict;
  292. /**
  293. * @bo_defer: structure holding vm_bos that need to be destroyed
  294. */
  295. struct llist_head bo_defer;
  296. };
  297. void drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name,
  298. enum drm_gpuvm_flags flags,
  299. struct drm_device *drm,
  300. struct drm_gem_object *r_obj,
  301. u64 start_offset, u64 range,
  302. u64 reserve_offset, u64 reserve_range,
  303. const struct drm_gpuvm_ops *ops);
  304. /**
  305. * drm_gpuvm_get() - acquire a struct drm_gpuvm reference
  306. * @gpuvm: the &drm_gpuvm to acquire the reference of
  307. *
  308. * This function acquires an additional reference to @gpuvm. It is illegal to
  309. * call this without already holding a reference. No locks required.
  310. *
  311. * Returns: the &struct drm_gpuvm pointer
  312. */
  313. static inline struct drm_gpuvm *
  314. drm_gpuvm_get(struct drm_gpuvm *gpuvm)
  315. {
  316. kref_get(&gpuvm->kref);
  317. return gpuvm;
  318. }
  319. void drm_gpuvm_put(struct drm_gpuvm *gpuvm);
  320. bool drm_gpuvm_range_valid(struct drm_gpuvm *gpuvm, u64 addr, u64 range);
  321. bool drm_gpuvm_interval_empty(struct drm_gpuvm *gpuvm, u64 addr, u64 range);
  322. struct drm_gem_object *
  323. drm_gpuvm_resv_object_alloc(struct drm_device *drm);
  324. /**
  325. * drm_gpuvm_resv_protected() - indicates whether &DRM_GPUVM_RESV_PROTECTED is
  326. * set
  327. * @gpuvm: the &drm_gpuvm
  328. *
  329. * Returns: true if &DRM_GPUVM_RESV_PROTECTED is set, false otherwise.
  330. */
  331. static inline bool
  332. drm_gpuvm_resv_protected(struct drm_gpuvm *gpuvm)
  333. {
  334. return gpuvm->flags & DRM_GPUVM_RESV_PROTECTED;
  335. }
  336. /**
  337. * drm_gpuvm_immediate_mode() - indicates whether &DRM_GPUVM_IMMEDIATE_MODE is
  338. * set
  339. * @gpuvm: the &drm_gpuvm
  340. *
  341. * Returns: true if &DRM_GPUVM_IMMEDIATE_MODE is set, false otherwise.
  342. */
  343. static inline bool
  344. drm_gpuvm_immediate_mode(struct drm_gpuvm *gpuvm)
  345. {
  346. return gpuvm->flags & DRM_GPUVM_IMMEDIATE_MODE;
  347. }
  348. /**
  349. * drm_gpuvm_resv() - returns the &drm_gpuvm's &dma_resv
  350. * @gpuvm__: the &drm_gpuvm
  351. *
  352. * Returns: a pointer to the &drm_gpuvm's shared &dma_resv
  353. */
  354. #define drm_gpuvm_resv(gpuvm__) ((gpuvm__)->r_obj->resv)
  355. /**
  356. * drm_gpuvm_resv_obj() - returns the &drm_gem_object holding the &drm_gpuvm's
  357. * &dma_resv
  358. * @gpuvm__: the &drm_gpuvm
  359. *
  360. * Returns: a pointer to the &drm_gem_object holding the &drm_gpuvm's shared
  361. * &dma_resv
  362. */
  363. #define drm_gpuvm_resv_obj(gpuvm__) ((gpuvm__)->r_obj)
  364. #define drm_gpuvm_resv_held(gpuvm__) \
  365. dma_resv_held(drm_gpuvm_resv(gpuvm__))
  366. #define drm_gpuvm_resv_assert_held(gpuvm__) \
  367. dma_resv_assert_held(drm_gpuvm_resv(gpuvm__))
  368. #define drm_gpuvm_resv_held(gpuvm__) \
  369. dma_resv_held(drm_gpuvm_resv(gpuvm__))
  370. #define drm_gpuvm_resv_assert_held(gpuvm__) \
  371. dma_resv_assert_held(drm_gpuvm_resv(gpuvm__))
  372. /**
  373. * drm_gpuvm_is_extobj() - indicates whether the given &drm_gem_object is an
  374. * external object
  375. * @gpuvm: the &drm_gpuvm to check
  376. * @obj: the &drm_gem_object to check
  377. *
  378. * Returns: true if the &drm_gem_object &dma_resv differs from the
  379. * &drm_gpuvms &dma_resv, false otherwise
  380. */
  381. static inline bool
  382. drm_gpuvm_is_extobj(struct drm_gpuvm *gpuvm,
  383. struct drm_gem_object *obj)
  384. {
  385. return obj && obj->resv != drm_gpuvm_resv(gpuvm);
  386. }
  387. static inline struct drm_gpuva *
  388. __drm_gpuva_next(struct drm_gpuva *va)
  389. {
  390. if (va && !list_is_last(&va->rb.entry, &va->vm->rb.list))
  391. return list_next_entry(va, rb.entry);
  392. return NULL;
  393. }
  394. /**
  395. * drm_gpuvm_for_each_va_range() - iterate over a range of &drm_gpuvas
  396. * @va__: &drm_gpuva structure to assign to in each iteration step
  397. * @gpuvm__: &drm_gpuvm to walk over
  398. * @start__: starting offset, the first gpuva will overlap this
  399. * @end__: ending offset, the last gpuva will start before this (but may
  400. * overlap)
  401. *
  402. * This iterator walks over all &drm_gpuvas in the &drm_gpuvm that lie
  403. * between @start__ and @end__. It is implemented similarly to list_for_each(),
  404. * but is using the &drm_gpuvm's internal interval tree to accelerate
  405. * the search for the starting &drm_gpuva, and hence isn't safe against removal
  406. * of elements. It assumes that @end__ is within (or is the upper limit of) the
  407. * &drm_gpuvm. This iterator does not skip over the &drm_gpuvm's
  408. * @kernel_alloc_node.
  409. */
  410. #define drm_gpuvm_for_each_va_range(va__, gpuvm__, start__, end__) \
  411. for (va__ = drm_gpuva_find_first((gpuvm__), (start__), (end__) - (start__)); \
  412. va__ && (va__->va.addr < (end__)); \
  413. va__ = __drm_gpuva_next(va__))
  414. /**
  415. * drm_gpuvm_for_each_va_range_safe() - safely iterate over a range of
  416. * &drm_gpuvas
  417. * @va__: &drm_gpuva to assign to in each iteration step
  418. * @next__: another &drm_gpuva to use as temporary storage
  419. * @gpuvm__: &drm_gpuvm to walk over
  420. * @start__: starting offset, the first gpuva will overlap this
  421. * @end__: ending offset, the last gpuva will start before this (but may
  422. * overlap)
  423. *
  424. * This iterator walks over all &drm_gpuvas in the &drm_gpuvm that lie
  425. * between @start__ and @end__. It is implemented similarly to
  426. * list_for_each_safe(), but is using the &drm_gpuvm's internal interval
  427. * tree to accelerate the search for the starting &drm_gpuva, and hence is safe
  428. * against removal of elements. It assumes that @end__ is within (or is the
  429. * upper limit of) the &drm_gpuvm. This iterator does not skip over the
  430. * &drm_gpuvm's @kernel_alloc_node.
  431. */
  432. #define drm_gpuvm_for_each_va_range_safe(va__, next__, gpuvm__, start__, end__) \
  433. for (va__ = drm_gpuva_find_first((gpuvm__), (start__), (end__) - (start__)), \
  434. next__ = __drm_gpuva_next(va__); \
  435. va__ && (va__->va.addr < (end__)); \
  436. va__ = next__, next__ = __drm_gpuva_next(va__))
  437. /**
  438. * drm_gpuvm_for_each_va() - iterate over all &drm_gpuvas
  439. * @va__: &drm_gpuva to assign to in each iteration step
  440. * @gpuvm__: &drm_gpuvm to walk over
  441. *
  442. * This iterator walks over all &drm_gpuva structures associated with the given
  443. * &drm_gpuvm.
  444. */
  445. #define drm_gpuvm_for_each_va(va__, gpuvm__) \
  446. list_for_each_entry(va__, &(gpuvm__)->rb.list, rb.entry)
  447. /**
  448. * drm_gpuvm_for_each_va_safe() - safely iterate over all &drm_gpuvas
  449. * @va__: &drm_gpuva to assign to in each iteration step
  450. * @next__: another &drm_gpuva to use as temporary storage
  451. * @gpuvm__: &drm_gpuvm to walk over
  452. *
  453. * This iterator walks over all &drm_gpuva structures associated with the given
  454. * &drm_gpuvm. It is implemented with list_for_each_entry_safe(), and
  455. * hence safe against the removal of elements.
  456. */
  457. #define drm_gpuvm_for_each_va_safe(va__, next__, gpuvm__) \
  458. list_for_each_entry_safe(va__, next__, &(gpuvm__)->rb.list, rb.entry)
  459. /**
  460. * struct drm_gpuvm_exec - &drm_gpuvm abstraction of &drm_exec
  461. *
  462. * This structure should be created on the stack as &drm_exec should be.
  463. *
  464. * Optionally, @extra can be set in order to lock additional &drm_gem_objects.
  465. */
  466. struct drm_gpuvm_exec {
  467. /**
  468. * @exec: the &drm_exec structure
  469. */
  470. struct drm_exec exec;
  471. /**
  472. * @flags: the flags for the struct drm_exec
  473. */
  474. u32 flags;
  475. /**
  476. * @vm: the &drm_gpuvm to lock its DMA reservations
  477. */
  478. struct drm_gpuvm *vm;
  479. /**
  480. * @num_fences: the number of fences to reserve for the &dma_resv of the
  481. * locked &drm_gem_objects
  482. */
  483. unsigned int num_fences;
  484. /**
  485. * @extra: Callback and corresponding private data for the driver to
  486. * lock arbitrary additional &drm_gem_objects.
  487. */
  488. struct {
  489. /**
  490. * @extra.fn: The driver callback to lock additional
  491. * &drm_gem_objects.
  492. */
  493. int (*fn)(struct drm_gpuvm_exec *vm_exec);
  494. /**
  495. * @extra.priv: driver private data for the @fn callback
  496. */
  497. void *priv;
  498. } extra;
  499. };
  500. int drm_gpuvm_prepare_vm(struct drm_gpuvm *gpuvm,
  501. struct drm_exec *exec,
  502. unsigned int num_fences);
  503. int drm_gpuvm_prepare_objects(struct drm_gpuvm *gpuvm,
  504. struct drm_exec *exec,
  505. unsigned int num_fences);
  506. int drm_gpuvm_prepare_range(struct drm_gpuvm *gpuvm,
  507. struct drm_exec *exec,
  508. u64 addr, u64 range,
  509. unsigned int num_fences);
  510. int drm_gpuvm_exec_lock(struct drm_gpuvm_exec *vm_exec);
  511. int drm_gpuvm_exec_lock_array(struct drm_gpuvm_exec *vm_exec,
  512. struct drm_gem_object **objs,
  513. unsigned int num_objs);
  514. int drm_gpuvm_exec_lock_range(struct drm_gpuvm_exec *vm_exec,
  515. u64 addr, u64 range);
  516. /**
  517. * drm_gpuvm_exec_unlock() - lock all dma-resv of all assoiciated BOs
  518. * @vm_exec: the &drm_gpuvm_exec wrapper
  519. *
  520. * Releases all dma-resv locks of all &drm_gem_objects previously acquired
  521. * through drm_gpuvm_exec_lock() or its variants.
  522. *
  523. * Returns: 0 on success, negative error code on failure.
  524. */
  525. static inline void
  526. drm_gpuvm_exec_unlock(struct drm_gpuvm_exec *vm_exec)
  527. {
  528. drm_exec_fini(&vm_exec->exec);
  529. }
  530. int drm_gpuvm_validate(struct drm_gpuvm *gpuvm, struct drm_exec *exec);
  531. void drm_gpuvm_resv_add_fence(struct drm_gpuvm *gpuvm,
  532. struct drm_exec *exec,
  533. struct dma_fence *fence,
  534. enum dma_resv_usage private_usage,
  535. enum dma_resv_usage extobj_usage);
  536. /**
  537. * drm_gpuvm_exec_resv_add_fence() - add fence to private and all extobj
  538. * @vm_exec: the &drm_gpuvm_exec wrapper
  539. * @fence: fence to add
  540. * @private_usage: private dma-resv usage
  541. * @extobj_usage: extobj dma-resv usage
  542. *
  543. * See drm_gpuvm_resv_add_fence().
  544. */
  545. static inline void
  546. drm_gpuvm_exec_resv_add_fence(struct drm_gpuvm_exec *vm_exec,
  547. struct dma_fence *fence,
  548. enum dma_resv_usage private_usage,
  549. enum dma_resv_usage extobj_usage)
  550. {
  551. drm_gpuvm_resv_add_fence(vm_exec->vm, &vm_exec->exec, fence,
  552. private_usage, extobj_usage);
  553. }
  554. /**
  555. * drm_gpuvm_exec_validate() - validate all BOs marked as evicted
  556. * @vm_exec: the &drm_gpuvm_exec wrapper
  557. *
  558. * See drm_gpuvm_validate().
  559. *
  560. * Returns: 0 on success, negative error code on failure.
  561. */
  562. static inline int
  563. drm_gpuvm_exec_validate(struct drm_gpuvm_exec *vm_exec)
  564. {
  565. return drm_gpuvm_validate(vm_exec->vm, &vm_exec->exec);
  566. }
  567. /**
  568. * struct drm_gpuvm_bo - structure representing a &drm_gpuvm and
  569. * &drm_gem_object combination
  570. *
  571. * This structure is an abstraction representing a &drm_gpuvm and
  572. * &drm_gem_object combination. It serves as an indirection to accelerate
  573. * iterating all &drm_gpuvas within a &drm_gpuvm backed by the same
  574. * &drm_gem_object.
  575. *
  576. * Furthermore it is used cache evicted GEM objects for a certain GPU-VM to
  577. * accelerate validation.
  578. *
  579. * Typically, drivers want to create an instance of a struct drm_gpuvm_bo once
  580. * a GEM object is mapped first in a GPU-VM and release the instance once the
  581. * last mapping of the GEM object in this GPU-VM is unmapped.
  582. */
  583. struct drm_gpuvm_bo {
  584. /**
  585. * @vm: The &drm_gpuvm the @obj is mapped in. This is a reference
  586. * counted pointer.
  587. */
  588. struct drm_gpuvm *vm;
  589. /**
  590. * @obj: The &drm_gem_object being mapped in @vm. This is a reference
  591. * counted pointer.
  592. */
  593. struct drm_gem_object *obj;
  594. /**
  595. * @evicted: Indicates whether the &drm_gem_object is evicted; field
  596. * protected by the &drm_gem_object's dma-resv lock.
  597. */
  598. bool evicted;
  599. /**
  600. * @kref: The reference count for this &drm_gpuvm_bo.
  601. */
  602. struct kref kref;
  603. /**
  604. * @list: Structure containing all &list_heads.
  605. */
  606. struct {
  607. /**
  608. * @list.gpuva: The list of linked &drm_gpuvas.
  609. *
  610. * It is safe to access entries from this list as long as the
  611. * GEM's gpuva lock is held. See also struct drm_gem_object.
  612. */
  613. struct list_head gpuva;
  614. /**
  615. * @list.entry: Structure containing all &list_heads serving as
  616. * entry.
  617. */
  618. struct {
  619. /**
  620. * @list.entry.gem: List entry to attach to the
  621. * &drm_gem_objects gpuva list.
  622. */
  623. struct list_head gem;
  624. /**
  625. * @list.entry.evict: List entry to attach to the
  626. * &drm_gpuvms extobj list.
  627. */
  628. struct list_head extobj;
  629. /**
  630. * @list.entry.evict: List entry to attach to the
  631. * &drm_gpuvms evict list.
  632. */
  633. struct list_head evict;
  634. /**
  635. * @list.entry.bo_defer: List entry to attach to
  636. * the &drm_gpuvms bo_defer list.
  637. */
  638. struct llist_node bo_defer;
  639. } entry;
  640. } list;
  641. };
  642. struct drm_gpuvm_bo *
  643. drm_gpuvm_bo_create(struct drm_gpuvm *gpuvm,
  644. struct drm_gem_object *obj);
  645. struct drm_gpuvm_bo *
  646. drm_gpuvm_bo_obtain_locked(struct drm_gpuvm *gpuvm,
  647. struct drm_gem_object *obj);
  648. struct drm_gpuvm_bo *
  649. drm_gpuvm_bo_obtain_prealloc(struct drm_gpuvm_bo *vm_bo);
  650. /**
  651. * drm_gpuvm_bo_get() - acquire a struct drm_gpuvm_bo reference
  652. * @vm_bo: the &drm_gpuvm_bo to acquire the reference of
  653. *
  654. * This function acquires an additional reference to @vm_bo. It is illegal to
  655. * call this without already holding a reference. No locks required.
  656. *
  657. * Returns: the &struct vm_bo pointer
  658. */
  659. static inline struct drm_gpuvm_bo *
  660. drm_gpuvm_bo_get(struct drm_gpuvm_bo *vm_bo)
  661. {
  662. kref_get(&vm_bo->kref);
  663. return vm_bo;
  664. }
  665. bool drm_gpuvm_bo_put(struct drm_gpuvm_bo *vm_bo);
  666. bool drm_gpuvm_bo_put_deferred(struct drm_gpuvm_bo *vm_bo);
  667. void drm_gpuvm_bo_deferred_cleanup(struct drm_gpuvm *gpuvm);
  668. struct drm_gpuvm_bo *
  669. drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm,
  670. struct drm_gem_object *obj);
  671. void drm_gpuvm_bo_evict(struct drm_gpuvm_bo *vm_bo, bool evict);
  672. /**
  673. * drm_gpuvm_bo_gem_evict() - add/remove all &drm_gpuvm_bo's in the list
  674. * to/from the &drm_gpuvms evicted list
  675. * @obj: the &drm_gem_object
  676. * @evict: indicates whether @obj is evicted
  677. *
  678. * See drm_gpuvm_bo_evict().
  679. */
  680. static inline void
  681. drm_gpuvm_bo_gem_evict(struct drm_gem_object *obj, bool evict)
  682. {
  683. struct drm_gpuvm_bo *vm_bo;
  684. drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
  685. drm_gem_gpuva_assert_lock_held(vm_bo->vm, obj);
  686. drm_gpuvm_bo_evict(vm_bo, evict);
  687. }
  688. }
  689. void drm_gpuvm_bo_extobj_add(struct drm_gpuvm_bo *vm_bo);
  690. /**
  691. * drm_gpuvm_bo_for_each_va() - iterator to walk over a list of &drm_gpuva
  692. * @va__: &drm_gpuva structure to assign to in each iteration step
  693. * @vm_bo__: the &drm_gpuvm_bo the &drm_gpuva to walk are associated with
  694. *
  695. * This iterator walks over all &drm_gpuva structures associated with the
  696. * &drm_gpuvm_bo.
  697. *
  698. * The caller must hold the GEM's gpuva lock.
  699. */
  700. #define drm_gpuvm_bo_for_each_va(va__, vm_bo__) \
  701. list_for_each_entry(va__, &(vm_bo)->list.gpuva, gem.entry)
  702. /**
  703. * drm_gpuvm_bo_for_each_va_safe() - iterator to safely walk over a list of
  704. * &drm_gpuva
  705. * @va__: &drm_gpuva structure to assign to in each iteration step
  706. * @next__: &next &drm_gpuva to store the next step
  707. * @vm_bo__: the &drm_gpuvm_bo the &drm_gpuva to walk are associated with
  708. *
  709. * This iterator walks over all &drm_gpuva structures associated with the
  710. * &drm_gpuvm_bo. It is implemented with list_for_each_entry_safe(), hence
  711. * it is save against removal of elements.
  712. *
  713. * The caller must hold the GEM's gpuva lock.
  714. */
  715. #define drm_gpuvm_bo_for_each_va_safe(va__, next__, vm_bo__) \
  716. list_for_each_entry_safe(va__, next__, &(vm_bo)->list.gpuva, gem.entry)
  717. /**
  718. * enum drm_gpuva_op_type - GPU VA operation type
  719. *
  720. * Operations to alter the GPU VA mappings tracked by the &drm_gpuvm.
  721. */
  722. enum drm_gpuva_op_type {
  723. /**
  724. * @DRM_GPUVA_OP_MAP: the map op type
  725. */
  726. DRM_GPUVA_OP_MAP,
  727. /**
  728. * @DRM_GPUVA_OP_REMAP: the remap op type
  729. */
  730. DRM_GPUVA_OP_REMAP,
  731. /**
  732. * @DRM_GPUVA_OP_UNMAP: the unmap op type
  733. */
  734. DRM_GPUVA_OP_UNMAP,
  735. /**
  736. * @DRM_GPUVA_OP_PREFETCH: the prefetch op type
  737. */
  738. DRM_GPUVA_OP_PREFETCH,
  739. /**
  740. * @DRM_GPUVA_OP_DRIVER: the driver defined op type
  741. */
  742. DRM_GPUVA_OP_DRIVER,
  743. };
  744. /**
  745. * struct drm_gpuva_op_map - GPU VA map operation
  746. *
  747. * This structure represents a single map operation generated by the
  748. * DRM GPU VA manager.
  749. */
  750. struct drm_gpuva_op_map {
  751. /**
  752. * @va: structure containing address and range of a map
  753. * operation
  754. */
  755. struct {
  756. /**
  757. * @va.addr: the base address of the new mapping
  758. */
  759. u64 addr;
  760. /**
  761. * @va.range: the range of the new mapping
  762. */
  763. u64 range;
  764. } va;
  765. /**
  766. * @gem: structure containing the &drm_gem_object and its offset
  767. */
  768. struct {
  769. /**
  770. * @gem.offset: the offset within the &drm_gem_object
  771. */
  772. u64 offset;
  773. /**
  774. * @gem.obj: the &drm_gem_object to map
  775. */
  776. struct drm_gem_object *obj;
  777. } gem;
  778. };
  779. /**
  780. * struct drm_gpuva_op_unmap - GPU VA unmap operation
  781. *
  782. * This structure represents a single unmap operation generated by the
  783. * DRM GPU VA manager.
  784. */
  785. struct drm_gpuva_op_unmap {
  786. /**
  787. * @va: the &drm_gpuva to unmap
  788. */
  789. struct drm_gpuva *va;
  790. /**
  791. * @keep:
  792. *
  793. * Indicates whether this &drm_gpuva is physically contiguous with the
  794. * original mapping request.
  795. *
  796. * Optionally, if &keep is set, drivers may keep the actual page table
  797. * mappings for this &drm_gpuva, adding the missing page table entries
  798. * only and update the &drm_gpuvm accordingly.
  799. */
  800. bool keep;
  801. };
  802. /**
  803. * struct drm_gpuva_op_remap - GPU VA remap operation
  804. *
  805. * This represents a single remap operation generated by the DRM GPU VA manager.
  806. *
  807. * A remap operation is generated when an existing GPU VA mmapping is split up
  808. * by inserting a new GPU VA mapping or by partially unmapping existent
  809. * mapping(s), hence it consists of a maximum of two map and one unmap
  810. * operation.
  811. *
  812. * The @unmap operation takes care of removing the original existing mapping.
  813. * @prev is used to remap the preceding part, @next the subsequent part.
  814. *
  815. * If either a new mapping's start address is aligned with the start address
  816. * of the old mapping or the new mapping's end address is aligned with the
  817. * end address of the old mapping, either @prev or @next is NULL.
  818. *
  819. * Note, the reason for a dedicated remap operation, rather than arbitrary
  820. * unmap and map operations, is to give drivers the chance of extracting driver
  821. * specific data for creating the new mappings from the unmap operations's
  822. * &drm_gpuva structure which typically is embedded in larger driver specific
  823. * structures.
  824. */
  825. struct drm_gpuva_op_remap {
  826. /**
  827. * @prev: the preceding part of a split mapping
  828. */
  829. struct drm_gpuva_op_map *prev;
  830. /**
  831. * @next: the subsequent part of a split mapping
  832. */
  833. struct drm_gpuva_op_map *next;
  834. /**
  835. * @unmap: the unmap operation for the original existing mapping
  836. */
  837. struct drm_gpuva_op_unmap *unmap;
  838. };
  839. /**
  840. * struct drm_gpuva_op_prefetch - GPU VA prefetch operation
  841. *
  842. * This structure represents a single prefetch operation generated by the
  843. * DRM GPU VA manager.
  844. */
  845. struct drm_gpuva_op_prefetch {
  846. /**
  847. * @va: the &drm_gpuva to prefetch
  848. */
  849. struct drm_gpuva *va;
  850. };
  851. /**
  852. * struct drm_gpuva_op - GPU VA operation
  853. *
  854. * This structure represents a single generic operation.
  855. *
  856. * The particular type of the operation is defined by @op.
  857. */
  858. struct drm_gpuva_op {
  859. /**
  860. * @entry:
  861. *
  862. * The &list_head used to distribute instances of this struct within
  863. * &drm_gpuva_ops.
  864. */
  865. struct list_head entry;
  866. /**
  867. * @op: the type of the operation
  868. */
  869. enum drm_gpuva_op_type op;
  870. union {
  871. /**
  872. * @map: the map operation
  873. */
  874. struct drm_gpuva_op_map map;
  875. /**
  876. * @remap: the remap operation
  877. */
  878. struct drm_gpuva_op_remap remap;
  879. /**
  880. * @unmap: the unmap operation
  881. */
  882. struct drm_gpuva_op_unmap unmap;
  883. /**
  884. * @prefetch: the prefetch operation
  885. */
  886. struct drm_gpuva_op_prefetch prefetch;
  887. };
  888. };
  889. /**
  890. * struct drm_gpuva_ops - wraps a list of &drm_gpuva_op
  891. */
  892. struct drm_gpuva_ops {
  893. /**
  894. * @list: the &list_head
  895. */
  896. struct list_head list;
  897. };
  898. /**
  899. * drm_gpuva_for_each_op() - iterator to walk over &drm_gpuva_ops
  900. * @op: &drm_gpuva_op to assign in each iteration step
  901. * @ops: &drm_gpuva_ops to walk
  902. *
  903. * This iterator walks over all ops within a given list of operations.
  904. */
  905. #define drm_gpuva_for_each_op(op, ops) list_for_each_entry(op, &(ops)->list, entry)
  906. /**
  907. * drm_gpuva_for_each_op_safe() - iterator to safely walk over &drm_gpuva_ops
  908. * @op: &drm_gpuva_op to assign in each iteration step
  909. * @next: &next &drm_gpuva_op to store the next step
  910. * @ops: &drm_gpuva_ops to walk
  911. *
  912. * This iterator walks over all ops within a given list of operations. It is
  913. * implemented with list_for_each_safe(), so save against removal of elements.
  914. */
  915. #define drm_gpuva_for_each_op_safe(op, next, ops) \
  916. list_for_each_entry_safe(op, next, &(ops)->list, entry)
  917. /**
  918. * drm_gpuva_for_each_op_from_reverse() - iterate backwards from the given point
  919. * @op: &drm_gpuva_op to assign in each iteration step
  920. * @ops: &drm_gpuva_ops to walk
  921. *
  922. * This iterator walks over all ops within a given list of operations beginning
  923. * from the given operation in reverse order.
  924. */
  925. #define drm_gpuva_for_each_op_from_reverse(op, ops) \
  926. list_for_each_entry_from_reverse(op, &(ops)->list, entry)
  927. /**
  928. * drm_gpuva_for_each_op_reverse - iterator to walk over &drm_gpuva_ops in reverse
  929. * @op: &drm_gpuva_op to assign in each iteration step
  930. * @ops: &drm_gpuva_ops to walk
  931. *
  932. * This iterator walks over all ops within a given list of operations in reverse
  933. */
  934. #define drm_gpuva_for_each_op_reverse(op, ops) \
  935. list_for_each_entry_reverse(op, &(ops)->list, entry)
  936. /**
  937. * drm_gpuva_first_op() - returns the first &drm_gpuva_op from &drm_gpuva_ops
  938. * @ops: the &drm_gpuva_ops to get the fist &drm_gpuva_op from
  939. */
  940. #define drm_gpuva_first_op(ops) \
  941. list_first_entry(&(ops)->list, struct drm_gpuva_op, entry)
  942. /**
  943. * drm_gpuva_last_op() - returns the last &drm_gpuva_op from &drm_gpuva_ops
  944. * @ops: the &drm_gpuva_ops to get the last &drm_gpuva_op from
  945. */
  946. #define drm_gpuva_last_op(ops) \
  947. list_last_entry(&(ops)->list, struct drm_gpuva_op, entry)
  948. /**
  949. * drm_gpuva_prev_op() - previous &drm_gpuva_op in the list
  950. * @op: the current &drm_gpuva_op
  951. */
  952. #define drm_gpuva_prev_op(op) list_prev_entry(op, entry)
  953. /**
  954. * drm_gpuva_next_op() - next &drm_gpuva_op in the list
  955. * @op: the current &drm_gpuva_op
  956. */
  957. #define drm_gpuva_next_op(op) list_next_entry(op, entry)
  958. /**
  959. * struct drm_gpuvm_map_req - arguments passed to drm_gpuvm_sm_map[_ops_create]()
  960. */
  961. struct drm_gpuvm_map_req {
  962. /**
  963. * @map: struct drm_gpuva_op_map
  964. */
  965. struct drm_gpuva_op_map map;
  966. };
  967. struct drm_gpuva_ops *
  968. drm_gpuvm_sm_map_ops_create(struct drm_gpuvm *gpuvm,
  969. const struct drm_gpuvm_map_req *req);
  970. struct drm_gpuva_ops *
  971. drm_gpuvm_madvise_ops_create(struct drm_gpuvm *gpuvm,
  972. const struct drm_gpuvm_map_req *req);
  973. struct drm_gpuva_ops *
  974. drm_gpuvm_sm_unmap_ops_create(struct drm_gpuvm *gpuvm,
  975. u64 addr, u64 range);
  976. struct drm_gpuva_ops *
  977. drm_gpuvm_prefetch_ops_create(struct drm_gpuvm *gpuvm,
  978. u64 addr, u64 range);
  979. struct drm_gpuva_ops *
  980. drm_gpuvm_bo_unmap_ops_create(struct drm_gpuvm_bo *vm_bo);
  981. void drm_gpuva_ops_free(struct drm_gpuvm *gpuvm,
  982. struct drm_gpuva_ops *ops);
  983. static inline void drm_gpuva_init_from_op(struct drm_gpuva *va,
  984. const struct drm_gpuva_op_map *op)
  985. {
  986. va->va.addr = op->va.addr;
  987. va->va.range = op->va.range;
  988. va->gem.obj = op->gem.obj;
  989. va->gem.offset = op->gem.offset;
  990. }
  991. /**
  992. * struct drm_gpuvm_ops - callbacks for split/merge steps
  993. *
  994. * This structure defines the callbacks used by &drm_gpuvm_sm_map and
  995. * &drm_gpuvm_sm_unmap to provide the split/merge steps for map and unmap
  996. * operations to drivers.
  997. */
  998. struct drm_gpuvm_ops {
  999. /**
  1000. * @vm_free: called when the last reference of a struct drm_gpuvm is
  1001. * dropped
  1002. *
  1003. * This callback is mandatory.
  1004. */
  1005. void (*vm_free)(struct drm_gpuvm *gpuvm);
  1006. /**
  1007. * @op_alloc: called when the &drm_gpuvm allocates
  1008. * a struct drm_gpuva_op
  1009. *
  1010. * Some drivers may want to embed struct drm_gpuva_op into driver
  1011. * specific structures. By implementing this callback drivers can
  1012. * allocate memory accordingly.
  1013. *
  1014. * This callback is optional.
  1015. */
  1016. struct drm_gpuva_op *(*op_alloc)(void);
  1017. /**
  1018. * @op_free: called when the &drm_gpuvm frees a
  1019. * struct drm_gpuva_op
  1020. *
  1021. * Some drivers may want to embed struct drm_gpuva_op into driver
  1022. * specific structures. By implementing this callback drivers can
  1023. * free the previously allocated memory accordingly.
  1024. *
  1025. * This callback is optional.
  1026. */
  1027. void (*op_free)(struct drm_gpuva_op *op);
  1028. /**
  1029. * @vm_bo_alloc: called when the &drm_gpuvm allocates
  1030. * a struct drm_gpuvm_bo
  1031. *
  1032. * Some drivers may want to embed struct drm_gpuvm_bo into driver
  1033. * specific structures. By implementing this callback drivers can
  1034. * allocate memory accordingly.
  1035. *
  1036. * This callback is optional.
  1037. */
  1038. struct drm_gpuvm_bo *(*vm_bo_alloc)(void);
  1039. /**
  1040. * @vm_bo_free: called when the &drm_gpuvm frees a
  1041. * struct drm_gpuvm_bo
  1042. *
  1043. * Some drivers may want to embed struct drm_gpuvm_bo into driver
  1044. * specific structures. By implementing this callback drivers can
  1045. * free the previously allocated memory accordingly.
  1046. *
  1047. * This callback is optional.
  1048. */
  1049. void (*vm_bo_free)(struct drm_gpuvm_bo *vm_bo);
  1050. /**
  1051. * @vm_bo_validate: called from drm_gpuvm_validate()
  1052. *
  1053. * Drivers receive this callback for every evicted &drm_gem_object being
  1054. * mapped in the corresponding &drm_gpuvm.
  1055. *
  1056. * Typically, drivers would call their driver specific variant of
  1057. * ttm_bo_validate() from within this callback.
  1058. */
  1059. int (*vm_bo_validate)(struct drm_gpuvm_bo *vm_bo,
  1060. struct drm_exec *exec);
  1061. /**
  1062. * @sm_step_map: called from &drm_gpuvm_sm_map to finally insert the
  1063. * mapping once all previous steps were completed
  1064. *
  1065. * The &priv pointer matches the one the driver passed to
  1066. * &drm_gpuvm_sm_map or &drm_gpuvm_sm_unmap, respectively.
  1067. *
  1068. * Can be NULL if &drm_gpuvm_sm_map is used.
  1069. */
  1070. int (*sm_step_map)(struct drm_gpuva_op *op, void *priv);
  1071. /**
  1072. * @sm_step_remap: called from &drm_gpuvm_sm_map and
  1073. * &drm_gpuvm_sm_unmap to split up an existent mapping
  1074. *
  1075. * This callback is called when existent mapping needs to be split up.
  1076. * This is the case when either a newly requested mapping overlaps or
  1077. * is enclosed by an existent mapping or a partial unmap of an existent
  1078. * mapping is requested.
  1079. *
  1080. * The &priv pointer matches the one the driver passed to
  1081. * &drm_gpuvm_sm_map or &drm_gpuvm_sm_unmap, respectively.
  1082. *
  1083. * Can be NULL if neither &drm_gpuvm_sm_map nor &drm_gpuvm_sm_unmap is
  1084. * used.
  1085. */
  1086. int (*sm_step_remap)(struct drm_gpuva_op *op, void *priv);
  1087. /**
  1088. * @sm_step_unmap: called from &drm_gpuvm_sm_map and
  1089. * &drm_gpuvm_sm_unmap to unmap an existing mapping
  1090. *
  1091. * This callback is called when existing mapping needs to be unmapped.
  1092. * This is the case when either a newly requested mapping encloses an
  1093. * existing mapping or an unmap of an existing mapping is requested.
  1094. *
  1095. * The &priv pointer matches the one the driver passed to
  1096. * &drm_gpuvm_sm_map or &drm_gpuvm_sm_unmap, respectively.
  1097. *
  1098. * Can be NULL if neither &drm_gpuvm_sm_map nor &drm_gpuvm_sm_unmap is
  1099. * used.
  1100. */
  1101. int (*sm_step_unmap)(struct drm_gpuva_op *op, void *priv);
  1102. };
  1103. int drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm, void *priv,
  1104. const struct drm_gpuvm_map_req *req);
  1105. int drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv,
  1106. u64 addr, u64 range);
  1107. int drm_gpuvm_sm_map_exec_lock(struct drm_gpuvm *gpuvm,
  1108. struct drm_exec *exec, unsigned int num_fences,
  1109. struct drm_gpuvm_map_req *req);
  1110. int drm_gpuvm_sm_unmap_exec_lock(struct drm_gpuvm *gpuvm, struct drm_exec *exec,
  1111. u64 req_addr, u64 req_range);
  1112. void drm_gpuva_map(struct drm_gpuvm *gpuvm,
  1113. struct drm_gpuva *va,
  1114. const struct drm_gpuva_op_map *op);
  1115. void drm_gpuva_remap(struct drm_gpuva *prev,
  1116. struct drm_gpuva *next,
  1117. const struct drm_gpuva_op_remap *op);
  1118. void drm_gpuva_unmap(const struct drm_gpuva_op_unmap *op);
  1119. /**
  1120. * drm_gpuva_op_remap_to_unmap_range() - Helper to get the start and range of
  1121. * the unmap stage of a remap op.
  1122. * @op: Remap op.
  1123. * @start_addr: Output pointer for the start of the required unmap.
  1124. * @range: Output pointer for the length of the required unmap.
  1125. *
  1126. * The given start address and range will be set such that they represent the
  1127. * range of the address space that was previously covered by the mapping being
  1128. * re-mapped, but is now empty.
  1129. */
  1130. static inline void
  1131. drm_gpuva_op_remap_to_unmap_range(const struct drm_gpuva_op_remap *op,
  1132. u64 *start_addr, u64 *range)
  1133. {
  1134. const u64 va_start = op->prev ?
  1135. op->prev->va.addr + op->prev->va.range :
  1136. op->unmap->va->va.addr;
  1137. const u64 va_end = op->next ?
  1138. op->next->va.addr :
  1139. op->unmap->va->va.addr + op->unmap->va->va.range;
  1140. if (start_addr)
  1141. *start_addr = va_start;
  1142. if (range)
  1143. *range = va_end - va_start;
  1144. }
  1145. #endif /* __DRM_GPUVM_H__ */