drm_gpuvm.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752
  1. // SPDX-License-Identifier: GPL-2.0-only OR MIT
  2. /*
  3. * Copyright (c) 2022 Red Hat.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Danilo Krummrich <dakr@redhat.com>
  25. *
  26. */
  27. #include <drm/drm_gpuvm.h>
  28. #include <linux/interval_tree_generic.h>
  29. #include <linux/mm.h>
  30. /**
  31. * DOC: Overview
  32. *
  33. * The DRM GPU VA Manager, represented by struct drm_gpuvm keeps track of a
  34. * GPU's virtual address (VA) space and manages the corresponding virtual
  35. * mappings represented by &drm_gpuva objects. It also keeps track of the
  36. * mapping's backing &drm_gem_object buffers.
  37. *
  38. * &drm_gem_object buffers maintain a list of &drm_gpuva objects representing
  39. * all existent GPU VA mappings using this &drm_gem_object as backing buffer.
  40. *
  41. * GPU VAs can be flagged as sparse, such that drivers may use GPU VAs to also
  42. * keep track of sparse PTEs in order to support Vulkan 'Sparse Resources'.
  43. *
  44. * The GPU VA manager internally uses a rb-tree to manage the
  45. * &drm_gpuva mappings within a GPU's virtual address space.
  46. *
  47. * The &drm_gpuvm structure contains a special &drm_gpuva representing the
  48. * portion of VA space reserved by the kernel. This node is initialized together
  49. * with the GPU VA manager instance and removed when the GPU VA manager is
  50. * destroyed.
  51. *
  52. * In a typical application drivers would embed struct drm_gpuvm and
  53. * struct drm_gpuva within their own driver specific structures, there won't be
  54. * any memory allocations of its own nor memory allocations of &drm_gpuva
  55. * entries.
  56. *
  57. * The data structures needed to store &drm_gpuvas within the &drm_gpuvm are
  58. * contained within struct drm_gpuva already. Hence, for inserting &drm_gpuva
  59. * entries from within dma-fence signalling critical sections it is enough to
  60. * pre-allocate the &drm_gpuva structures.
  61. *
  62. * &drm_gem_objects which are private to a single VM can share a common
  63. * &dma_resv in order to improve locking efficiency (e.g. with &drm_exec).
  64. * For this purpose drivers must pass a &drm_gem_object to drm_gpuvm_init(), in
  65. * the following called 'resv object', which serves as the container of the
  66. * GPUVM's shared &dma_resv. This resv object can be a driver specific
  67. * &drm_gem_object, such as the &drm_gem_object containing the root page table,
  68. * but it can also be a 'dummy' object, which can be allocated with
  69. * drm_gpuvm_resv_object_alloc().
  70. *
  71. * In order to connect a struct drm_gpuva its backing &drm_gem_object each
  72. * &drm_gem_object maintains a list of &drm_gpuvm_bo structures, and each
  73. * &drm_gpuvm_bo contains a list of &drm_gpuva structures.
  74. *
  75. * A &drm_gpuvm_bo is an abstraction that represents a combination of a
  76. * &drm_gpuvm and a &drm_gem_object. Every such combination should be unique.
  77. * This is ensured by the API through drm_gpuvm_bo_obtain() and
  78. * drm_gpuvm_bo_obtain_prealloc() which first look into the corresponding
  79. * &drm_gem_object list of &drm_gpuvm_bos for an existing instance of this
  80. * particular combination. If not existent a new instance is created and linked
  81. * to the &drm_gem_object.
  82. *
  83. * &drm_gpuvm_bo structures, since unique for a given &drm_gpuvm, are also used
  84. * as entry for the &drm_gpuvm's lists of external and evicted objects. Those
  85. * lists are maintained in order to accelerate locking of dma-resv locks and
  86. * validation of evicted objects bound in a &drm_gpuvm. For instance, all
  87. * &drm_gem_object's &dma_resv of a given &drm_gpuvm can be locked by calling
  88. * drm_gpuvm_exec_lock(). Once locked drivers can call drm_gpuvm_validate() in
  89. * order to validate all evicted &drm_gem_objects. It is also possible to lock
  90. * additional &drm_gem_objects by providing the corresponding parameters to
  91. * drm_gpuvm_exec_lock() as well as open code the &drm_exec loop while making
  92. * use of helper functions such as drm_gpuvm_prepare_range() or
  93. * drm_gpuvm_prepare_objects().
  94. *
  95. * Every bound &drm_gem_object is treated as external object when its &dma_resv
  96. * structure is different than the &drm_gpuvm's common &dma_resv structure.
  97. */
  98. /**
  99. * DOC: Split and Merge
  100. *
  101. * Besides its capability to manage and represent a GPU VA space, the
  102. * GPU VA manager also provides functions to let the &drm_gpuvm calculate a
  103. * sequence of operations to satisfy a given map or unmap request.
  104. *
  105. * Therefore the DRM GPU VA manager provides an algorithm implementing splitting
  106. * and merging of existent GPU VA mappings with the ones that are requested to
  107. * be mapped or unmapped. This feature is required by the Vulkan API to
  108. * implement Vulkan 'Sparse Memory Bindings' - drivers UAPIs often refer to this
  109. * as VM BIND.
  110. *
  111. * Drivers can call drm_gpuvm_sm_map() to receive a sequence of callbacks
  112. * containing map, unmap and remap operations for a given newly requested
  113. * mapping. The sequence of callbacks represents the set of operations to
  114. * execute in order to integrate the new mapping cleanly into the current state
  115. * of the GPU VA space.
  116. *
  117. * Depending on how the new GPU VA mapping intersects with the existent mappings
  118. * of the GPU VA space the &drm_gpuvm_ops callbacks contain an arbitrary amount
  119. * of unmap operations, a maximum of two remap operations and a single map
  120. * operation. The caller might receive no callback at all if no operation is
  121. * required, e.g. if the requested mapping already exists in the exact same way.
  122. *
  123. * The single map operation represents the original map operation requested by
  124. * the caller.
  125. *
  126. * &drm_gpuva_op_unmap contains a 'keep' field, which indicates whether the
  127. * &drm_gpuva to unmap is physically contiguous with the original mapping
  128. * request. Optionally, if 'keep' is set, drivers may keep the actual page table
  129. * entries for this &drm_gpuva, adding the missing page table entries only and
  130. * update the &drm_gpuvm's view of things accordingly.
  131. *
  132. * Drivers may do the same optimization, namely delta page table updates, also
  133. * for remap operations. This is possible since &drm_gpuva_op_remap consists of
  134. * one unmap operation and one or two map operations, such that drivers can
  135. * derive the page table update delta accordingly.
  136. *
  137. * Note that there can't be more than two existent mappings to split up, one at
  138. * the beginning and one at the end of the new mapping, hence there is a
  139. * maximum of two remap operations.
  140. *
  141. * Analogous to drm_gpuvm_sm_map() drm_gpuvm_sm_unmap() uses &drm_gpuvm_ops to
  142. * call back into the driver in order to unmap a range of GPU VA space. The
  143. * logic behind this function is way simpler though: For all existent mappings
  144. * enclosed by the given range unmap operations are created. For mappings which
  145. * are only partically located within the given range, remap operations are
  146. * created such that those mappings are split up and re-mapped partically.
  147. *
  148. * As an alternative to drm_gpuvm_sm_map() and drm_gpuvm_sm_unmap(),
  149. * drm_gpuvm_sm_map_ops_create() and drm_gpuvm_sm_unmap_ops_create() can be used
  150. * to directly obtain an instance of struct drm_gpuva_ops containing a list of
  151. * &drm_gpuva_op, which can be iterated with drm_gpuva_for_each_op(). This list
  152. * contains the &drm_gpuva_ops analogous to the callbacks one would receive when
  153. * calling drm_gpuvm_sm_map() or drm_gpuvm_sm_unmap(). While this way requires
  154. * more memory (to allocate the &drm_gpuva_ops), it provides drivers a way to
  155. * iterate the &drm_gpuva_op multiple times, e.g. once in a context where memory
  156. * allocations are possible (e.g. to allocate GPU page tables) and once in the
  157. * dma-fence signalling critical path.
  158. *
  159. * To update the &drm_gpuvm's view of the GPU VA space drm_gpuva_insert() and
  160. * drm_gpuva_remove() may be used. These functions can safely be used from
  161. * &drm_gpuvm_ops callbacks originating from drm_gpuvm_sm_map() or
  162. * drm_gpuvm_sm_unmap(). However, it might be more convenient to use the
  163. * provided helper functions drm_gpuva_map(), drm_gpuva_remap() and
  164. * drm_gpuva_unmap() instead.
  165. *
  166. * The following diagram depicts the basic relationships of existent GPU VA
  167. * mappings, a newly requested mapping and the resulting mappings as implemented
  168. * by drm_gpuvm_sm_map() - it doesn't cover any arbitrary combinations of these.
  169. *
  170. * 1) Requested mapping is identical. Replace it, but indicate the backing PTEs
  171. * could be kept.
  172. *
  173. * ::
  174. *
  175. * 0 a 1
  176. * old: |-----------| (bo_offset=n)
  177. *
  178. * 0 a 1
  179. * req: |-----------| (bo_offset=n)
  180. *
  181. * 0 a 1
  182. * new: |-----------| (bo_offset=n)
  183. *
  184. *
  185. * 2) Requested mapping is identical, except for the BO offset, hence replace
  186. * the mapping.
  187. *
  188. * ::
  189. *
  190. * 0 a 1
  191. * old: |-----------| (bo_offset=n)
  192. *
  193. * 0 a 1
  194. * req: |-----------| (bo_offset=m)
  195. *
  196. * 0 a 1
  197. * new: |-----------| (bo_offset=m)
  198. *
  199. *
  200. * 3) Requested mapping is identical, except for the backing BO, hence replace
  201. * the mapping.
  202. *
  203. * ::
  204. *
  205. * 0 a 1
  206. * old: |-----------| (bo_offset=n)
  207. *
  208. * 0 b 1
  209. * req: |-----------| (bo_offset=n)
  210. *
  211. * 0 b 1
  212. * new: |-----------| (bo_offset=n)
  213. *
  214. *
  215. * 4) Existent mapping is a left aligned subset of the requested one, hence
  216. * replace the existent one.
  217. *
  218. * ::
  219. *
  220. * 0 a 1
  221. * old: |-----| (bo_offset=n)
  222. *
  223. * 0 a 2
  224. * req: |-----------| (bo_offset=n)
  225. *
  226. * 0 a 2
  227. * new: |-----------| (bo_offset=n)
  228. *
  229. * .. note::
  230. * We expect to see the same result for a request with a different BO
  231. * and/or non-contiguous BO offset.
  232. *
  233. *
  234. * 5) Requested mapping's range is a left aligned subset of the existent one,
  235. * but backed by a different BO. Hence, map the requested mapping and split
  236. * the existent one adjusting its BO offset.
  237. *
  238. * ::
  239. *
  240. * 0 a 2
  241. * old: |-----------| (bo_offset=n)
  242. *
  243. * 0 b 1
  244. * req: |-----| (bo_offset=n)
  245. *
  246. * 0 b 1 a' 2
  247. * new: |-----|-----| (b.bo_offset=n, a.bo_offset=n+1)
  248. *
  249. * .. note::
  250. * We expect to see the same result for a request with a different BO
  251. * and/or non-contiguous BO offset.
  252. *
  253. *
  254. * 6) Existent mapping is a superset of the requested mapping. Split it up, but
  255. * indicate that the backing PTEs could be kept.
  256. *
  257. * ::
  258. *
  259. * 0 a 2
  260. * old: |-----------| (bo_offset=n)
  261. *
  262. * 0 a 1
  263. * req: |-----| (bo_offset=n)
  264. *
  265. * 0 a 1 a' 2
  266. * new: |-----|-----| (a.bo_offset=n, a'.bo_offset=n+1)
  267. *
  268. *
  269. * 7) Requested mapping's range is a right aligned subset of the existent one,
  270. * but backed by a different BO. Hence, map the requested mapping and split
  271. * the existent one, without adjusting the BO offset.
  272. *
  273. * ::
  274. *
  275. * 0 a 2
  276. * old: |-----------| (bo_offset=n)
  277. *
  278. * 1 b 2
  279. * req: |-----| (bo_offset=m)
  280. *
  281. * 0 a 1 b 2
  282. * new: |-----|-----| (a.bo_offset=n,b.bo_offset=m)
  283. *
  284. *
  285. * 8) Existent mapping is a superset of the requested mapping. Split it up, but
  286. * indicate that the backing PTEs could be kept.
  287. *
  288. * ::
  289. *
  290. * 0 a 2
  291. * old: |-----------| (bo_offset=n)
  292. *
  293. * 1 a 2
  294. * req: |-----| (bo_offset=n+1)
  295. *
  296. * 0 a' 1 a 2
  297. * new: |-----|-----| (a'.bo_offset=n, a.bo_offset=n+1)
  298. *
  299. *
  300. * 9) Existent mapping is overlapped at the end by the requested mapping backed
  301. * by a different BO. Hence, map the requested mapping and split up the
  302. * existent one, without adjusting the BO offset.
  303. *
  304. * ::
  305. *
  306. * 0 a 2
  307. * old: |-----------| (bo_offset=n)
  308. *
  309. * 1 b 3
  310. * req: |-----------| (bo_offset=m)
  311. *
  312. * 0 a 1 b 3
  313. * new: |-----|-----------| (a.bo_offset=n,b.bo_offset=m)
  314. *
  315. *
  316. * 10) Existent mapping is overlapped by the requested mapping, both having the
  317. * same backing BO with a contiguous offset. Indicate the backing PTEs of
  318. * the old mapping could be kept.
  319. *
  320. * ::
  321. *
  322. * 0 a 2
  323. * old: |-----------| (bo_offset=n)
  324. *
  325. * 1 a 3
  326. * req: |-----------| (bo_offset=n+1)
  327. *
  328. * 0 a' 1 a 3
  329. * new: |-----|-----------| (a'.bo_offset=n, a.bo_offset=n+1)
  330. *
  331. *
  332. * 11) Requested mapping's range is a centered subset of the existent one
  333. * having a different backing BO. Hence, map the requested mapping and split
  334. * up the existent one in two mappings, adjusting the BO offset of the right
  335. * one accordingly.
  336. *
  337. * ::
  338. *
  339. * 0 a 3
  340. * old: |-----------------| (bo_offset=n)
  341. *
  342. * 1 b 2
  343. * req: |-----| (bo_offset=m)
  344. *
  345. * 0 a 1 b 2 a' 3
  346. * new: |-----|-----|-----| (a.bo_offset=n,b.bo_offset=m,a'.bo_offset=n+2)
  347. *
  348. *
  349. * 12) Requested mapping is a contiguous subset of the existent one. Split it
  350. * up, but indicate that the backing PTEs could be kept.
  351. *
  352. * ::
  353. *
  354. * 0 a 3
  355. * old: |-----------------| (bo_offset=n)
  356. *
  357. * 1 a 2
  358. * req: |-----| (bo_offset=n+1)
  359. *
  360. * 0 a' 1 a 2 a'' 3
  361. * old: |-----|-----|-----| (a'.bo_offset=n, a.bo_offset=n+1, a''.bo_offset=n+2)
  362. *
  363. *
  364. * 13) Existent mapping is a right aligned subset of the requested one, hence
  365. * replace the existent one.
  366. *
  367. * ::
  368. *
  369. * 1 a 2
  370. * old: |-----| (bo_offset=n+1)
  371. *
  372. * 0 a 2
  373. * req: |-----------| (bo_offset=n)
  374. *
  375. * 0 a 2
  376. * new: |-----------| (bo_offset=n)
  377. *
  378. * .. note::
  379. * We expect to see the same result for a request with a different bo
  380. * and/or non-contiguous bo_offset.
  381. *
  382. *
  383. * 14) Existent mapping is a centered subset of the requested one, hence
  384. * replace the existent one.
  385. *
  386. * ::
  387. *
  388. * 1 a 2
  389. * old: |-----| (bo_offset=n+1)
  390. *
  391. * 0 a 3
  392. * req: |----------------| (bo_offset=n)
  393. *
  394. * 0 a 3
  395. * new: |----------------| (bo_offset=n)
  396. *
  397. * .. note::
  398. * We expect to see the same result for a request with a different bo
  399. * and/or non-contiguous bo_offset.
  400. *
  401. *
  402. * 15) Existent mappings is overlapped at the beginning by the requested mapping
  403. * backed by a different BO. Hence, map the requested mapping and split up
  404. * the existent one, adjusting its BO offset accordingly.
  405. *
  406. * ::
  407. *
  408. * 1 a 3
  409. * old: |-----------| (bo_offset=n)
  410. *
  411. * 0 b 2
  412. * req: |-----------| (bo_offset=m)
  413. *
  414. * 0 b 2 a' 3
  415. * new: |-----------|-----| (b.bo_offset=m,a.bo_offset=n+2)
  416. */
  417. /**
  418. * DOC: Locking
  419. *
  420. * In terms of managing &drm_gpuva entries DRM GPUVM does not take care of
  421. * locking itself, it is the drivers responsibility to take care about locking.
  422. * Drivers might want to protect the following operations: inserting, removing
  423. * and iterating &drm_gpuva objects as well as generating all kinds of
  424. * operations, such as split / merge or prefetch.
  425. *
  426. * DRM GPUVM also does not take care of the locking of the backing
  427. * &drm_gem_object buffers GPU VA lists and &drm_gpuvm_bo abstractions by
  428. * itself; drivers are responsible to enforce mutual exclusion using either the
  429. * GEMs dma_resv lock or alternatively a driver specific external lock. For the
  430. * latter see also drm_gem_gpuva_set_lock().
  431. *
  432. * However, DRM GPUVM contains lockdep checks to ensure callers of its API hold
  433. * the corresponding lock whenever the &drm_gem_objects GPU VA list is accessed
  434. * by functions such as drm_gpuva_link() or drm_gpuva_unlink(), but also
  435. * drm_gpuvm_bo_obtain() and drm_gpuvm_bo_put().
  436. *
  437. * The latter is required since on creation and destruction of a &drm_gpuvm_bo
  438. * the &drm_gpuvm_bo is attached / removed from the &drm_gem_objects gpuva list.
  439. * Subsequent calls to drm_gpuvm_bo_obtain() for the same &drm_gpuvm and
  440. * &drm_gem_object must be able to observe previous creations and destructions
  441. * of &drm_gpuvm_bos in order to keep instances unique.
  442. *
  443. * The &drm_gpuvm's lists for keeping track of external and evicted objects are
  444. * protected against concurrent insertion / removal and iteration internally.
  445. *
  446. * However, drivers still need ensure to protect concurrent calls to functions
  447. * iterating those lists, namely drm_gpuvm_prepare_objects() and
  448. * drm_gpuvm_validate().
  449. *
  450. * Alternatively, drivers can set the &DRM_GPUVM_RESV_PROTECTED flag to indicate
  451. * that the corresponding &dma_resv locks are held in order to protect the
  452. * lists. If &DRM_GPUVM_RESV_PROTECTED is set, internal locking is disabled and
  453. * the corresponding lockdep checks are enabled. This is an optimization for
  454. * drivers which are capable of taking the corresponding &dma_resv locks and
  455. * hence do not require internal locking.
  456. */
  457. /**
  458. * DOC: Examples
  459. *
  460. * This section gives two examples on how to let the DRM GPUVA Manager generate
  461. * &drm_gpuva_op in order to satisfy a given map or unmap request and how to
  462. * make use of them.
  463. *
  464. * The below code is strictly limited to illustrate the generic usage pattern.
  465. * To maintain simplicitly, it doesn't make use of any abstractions for common
  466. * code, different (asyncronous) stages with fence signalling critical paths,
  467. * any other helpers or error handling in terms of freeing memory and dropping
  468. * previously taken locks.
  469. *
  470. * 1) Obtain a list of &drm_gpuva_op to create a new mapping::
  471. *
  472. * // Allocates a new &drm_gpuva.
  473. * struct drm_gpuva * driver_gpuva_alloc(void);
  474. *
  475. * // Typically drivers would embedd the &drm_gpuvm and &drm_gpuva
  476. * // structure in individual driver structures and lock the dma-resv with
  477. * // drm_exec or similar helpers.
  478. * int driver_mapping_create(struct drm_gpuvm *gpuvm,
  479. * u64 addr, u64 range,
  480. * struct drm_gem_object *obj, u64 offset)
  481. * {
  482. * struct drm_gpuva_ops *ops;
  483. * struct drm_gpuva_op *op
  484. * struct drm_gpuvm_bo *vm_bo;
  485. *
  486. * driver_lock_va_space();
  487. * ops = drm_gpuvm_sm_map_ops_create(gpuvm, addr, range,
  488. * obj, offset);
  489. * if (IS_ERR(ops))
  490. * return PTR_ERR(ops);
  491. *
  492. * vm_bo = drm_gpuvm_bo_obtain(gpuvm, obj);
  493. * if (IS_ERR(vm_bo))
  494. * return PTR_ERR(vm_bo);
  495. *
  496. * drm_gpuva_for_each_op(op, ops) {
  497. * struct drm_gpuva *va;
  498. *
  499. * switch (op->op) {
  500. * case DRM_GPUVA_OP_MAP:
  501. * va = driver_gpuva_alloc();
  502. * if (!va)
  503. * ; // unwind previous VA space updates,
  504. * // free memory and unlock
  505. *
  506. * driver_vm_map();
  507. * drm_gpuva_map(gpuvm, va, &op->map);
  508. * drm_gpuva_link(va, vm_bo);
  509. *
  510. * break;
  511. * case DRM_GPUVA_OP_REMAP: {
  512. * struct drm_gpuva *prev = NULL, *next = NULL;
  513. *
  514. * va = op->remap.unmap->va;
  515. *
  516. * if (op->remap.prev) {
  517. * prev = driver_gpuva_alloc();
  518. * if (!prev)
  519. * ; // unwind previous VA space
  520. * // updates, free memory and
  521. * // unlock
  522. * }
  523. *
  524. * if (op->remap.next) {
  525. * next = driver_gpuva_alloc();
  526. * if (!next)
  527. * ; // unwind previous VA space
  528. * // updates, free memory and
  529. * // unlock
  530. * }
  531. *
  532. * driver_vm_remap();
  533. * drm_gpuva_remap(prev, next, &op->remap);
  534. *
  535. * if (prev)
  536. * drm_gpuva_link(prev, va->vm_bo);
  537. * if (next)
  538. * drm_gpuva_link(next, va->vm_bo);
  539. * drm_gpuva_unlink(va);
  540. *
  541. * break;
  542. * }
  543. * case DRM_GPUVA_OP_UNMAP:
  544. * va = op->unmap->va;
  545. *
  546. * driver_vm_unmap();
  547. * drm_gpuva_unlink(va);
  548. * drm_gpuva_unmap(&op->unmap);
  549. *
  550. * break;
  551. * default:
  552. * break;
  553. * }
  554. * }
  555. * drm_gpuvm_bo_put(vm_bo);
  556. * driver_unlock_va_space();
  557. *
  558. * return 0;
  559. * }
  560. *
  561. * 2) Receive a callback for each &drm_gpuva_op to create a new mapping::
  562. *
  563. * struct driver_context {
  564. * struct drm_gpuvm *gpuvm;
  565. * struct drm_gpuvm_bo *vm_bo;
  566. * struct drm_gpuva *new_va;
  567. * struct drm_gpuva *prev_va;
  568. * struct drm_gpuva *next_va;
  569. * };
  570. *
  571. * // ops to pass to drm_gpuvm_init()
  572. * static const struct drm_gpuvm_ops driver_gpuvm_ops = {
  573. * .sm_step_map = driver_gpuva_map,
  574. * .sm_step_remap = driver_gpuva_remap,
  575. * .sm_step_unmap = driver_gpuva_unmap,
  576. * };
  577. *
  578. * // Typically drivers would embedd the &drm_gpuvm and &drm_gpuva
  579. * // structure in individual driver structures and lock the dma-resv with
  580. * // drm_exec or similar helpers.
  581. * int driver_mapping_create(struct drm_gpuvm *gpuvm,
  582. * u64 addr, u64 range,
  583. * struct drm_gem_object *obj, u64 offset)
  584. * {
  585. * struct driver_context ctx;
  586. * struct drm_gpuvm_bo *vm_bo;
  587. * struct drm_gpuva_ops *ops;
  588. * struct drm_gpuva_op *op;
  589. * int ret = 0;
  590. *
  591. * ctx.gpuvm = gpuvm;
  592. *
  593. * ctx.new_va = kzalloc(sizeof(*ctx.new_va), GFP_KERNEL);
  594. * ctx.prev_va = kzalloc(sizeof(*ctx.prev_va), GFP_KERNEL);
  595. * ctx.next_va = kzalloc(sizeof(*ctx.next_va), GFP_KERNEL);
  596. * ctx.vm_bo = drm_gpuvm_bo_create(gpuvm, obj);
  597. * if (!ctx.new_va || !ctx.prev_va || !ctx.next_va || !vm_bo) {
  598. * ret = -ENOMEM;
  599. * goto out;
  600. * }
  601. *
  602. * // Typically protected with a driver specific GEM gpuva lock
  603. * // used in the fence signaling path for drm_gpuva_link() and
  604. * // drm_gpuva_unlink(), hence pre-allocate.
  605. * ctx.vm_bo = drm_gpuvm_bo_obtain_prealloc(ctx.vm_bo);
  606. *
  607. * driver_lock_va_space();
  608. * ret = drm_gpuvm_sm_map(gpuvm, &ctx, addr, range, obj, offset);
  609. * driver_unlock_va_space();
  610. *
  611. * out:
  612. * drm_gpuvm_bo_put(ctx.vm_bo);
  613. * kfree(ctx.new_va);
  614. * kfree(ctx.prev_va);
  615. * kfree(ctx.next_va);
  616. * return ret;
  617. * }
  618. *
  619. * int driver_gpuva_map(struct drm_gpuva_op *op, void *__ctx)
  620. * {
  621. * struct driver_context *ctx = __ctx;
  622. *
  623. * drm_gpuva_map(ctx->vm, ctx->new_va, &op->map);
  624. *
  625. * drm_gpuva_link(ctx->new_va, ctx->vm_bo);
  626. *
  627. * // prevent the new GPUVA from being freed in
  628. * // driver_mapping_create()
  629. * ctx->new_va = NULL;
  630. *
  631. * return 0;
  632. * }
  633. *
  634. * int driver_gpuva_remap(struct drm_gpuva_op *op, void *__ctx)
  635. * {
  636. * struct driver_context *ctx = __ctx;
  637. * struct drm_gpuva *va = op->remap.unmap->va;
  638. *
  639. * drm_gpuva_remap(ctx->prev_va, ctx->next_va, &op->remap);
  640. *
  641. * if (op->remap.prev) {
  642. * drm_gpuva_link(ctx->prev_va, va->vm_bo);
  643. * ctx->prev_va = NULL;
  644. * }
  645. *
  646. * if (op->remap.next) {
  647. * drm_gpuva_link(ctx->next_va, va->vm_bo);
  648. * ctx->next_va = NULL;
  649. * }
  650. *
  651. * drm_gpuva_unlink(va);
  652. * kfree(va);
  653. *
  654. * return 0;
  655. * }
  656. *
  657. * int driver_gpuva_unmap(struct drm_gpuva_op *op, void *__ctx)
  658. * {
  659. * drm_gpuva_unlink(op->unmap.va);
  660. * drm_gpuva_unmap(&op->unmap);
  661. * kfree(op->unmap.va);
  662. *
  663. * return 0;
  664. * }
  665. */
  666. /**
  667. * get_next_vm_bo_from_list() - get the next vm_bo element
  668. * @__gpuvm: the &drm_gpuvm
  669. * @__list_name: the name of the list we're iterating on
  670. * @__local_list: a pointer to the local list used to store already iterated items
  671. * @__prev_vm_bo: the previous element we got from get_next_vm_bo_from_list()
  672. *
  673. * This helper is here to provide lockless list iteration. Lockless as in, the
  674. * iterator releases the lock immediately after picking the first element from
  675. * the list, so list insertion deletion can happen concurrently.
  676. *
  677. * Elements popped from the original list are kept in a local list, so removal
  678. * and is_empty checks can still happen while we're iterating the list.
  679. */
  680. #define get_next_vm_bo_from_list(__gpuvm, __list_name, __local_list, __prev_vm_bo) \
  681. ({ \
  682. struct drm_gpuvm_bo *__vm_bo = NULL; \
  683. \
  684. drm_gpuvm_bo_put(__prev_vm_bo); \
  685. \
  686. spin_lock(&(__gpuvm)->__list_name.lock); \
  687. if (!(__gpuvm)->__list_name.local_list) \
  688. (__gpuvm)->__list_name.local_list = __local_list; \
  689. else \
  690. drm_WARN_ON((__gpuvm)->drm, \
  691. (__gpuvm)->__list_name.local_list != __local_list); \
  692. \
  693. while (!list_empty(&(__gpuvm)->__list_name.list)) { \
  694. __vm_bo = list_first_entry(&(__gpuvm)->__list_name.list, \
  695. struct drm_gpuvm_bo, \
  696. list.entry.__list_name); \
  697. if (kref_get_unless_zero(&__vm_bo->kref)) { \
  698. list_move_tail(&(__vm_bo)->list.entry.__list_name, \
  699. __local_list); \
  700. break; \
  701. } else { \
  702. list_del_init(&(__vm_bo)->list.entry.__list_name); \
  703. __vm_bo = NULL; \
  704. } \
  705. } \
  706. spin_unlock(&(__gpuvm)->__list_name.lock); \
  707. \
  708. __vm_bo; \
  709. })
  710. /**
  711. * for_each_vm_bo_in_list() - internal vm_bo list iterator
  712. * @__gpuvm: the &drm_gpuvm
  713. * @__list_name: the name of the list we're iterating on
  714. * @__local_list: a pointer to the local list used to store already iterated items
  715. * @__vm_bo: the struct drm_gpuvm_bo to assign in each iteration step
  716. *
  717. * This helper is here to provide lockless list iteration. Lockless as in, the
  718. * iterator releases the lock immediately after picking the first element from the
  719. * list, hence list insertion and deletion can happen concurrently.
  720. *
  721. * It is not allowed to re-assign the vm_bo pointer from inside this loop.
  722. *
  723. * Typical use:
  724. *
  725. * struct drm_gpuvm_bo *vm_bo;
  726. * LIST_HEAD(my_local_list);
  727. *
  728. * ret = 0;
  729. * for_each_vm_bo_in_list(gpuvm, <list_name>, &my_local_list, vm_bo) {
  730. * ret = do_something_with_vm_bo(..., vm_bo);
  731. * if (ret)
  732. * break;
  733. * }
  734. * // Drop ref in case we break out of the loop.
  735. * drm_gpuvm_bo_put(vm_bo);
  736. * restore_vm_bo_list(gpuvm, <list_name>, &my_local_list);
  737. *
  738. *
  739. * Only used for internal list iterations, not meant to be exposed to the outside
  740. * world.
  741. */
  742. #define for_each_vm_bo_in_list(__gpuvm, __list_name, __local_list, __vm_bo) \
  743. for (__vm_bo = get_next_vm_bo_from_list(__gpuvm, __list_name, \
  744. __local_list, NULL); \
  745. __vm_bo; \
  746. __vm_bo = get_next_vm_bo_from_list(__gpuvm, __list_name, \
  747. __local_list, __vm_bo))
  748. static void
  749. __restore_vm_bo_list(struct drm_gpuvm *gpuvm, spinlock_t *lock,
  750. struct list_head *list, struct list_head **local_list)
  751. {
  752. /* Merge back the two lists, moving local list elements to the
  753. * head to preserve previous ordering, in case it matters.
  754. */
  755. spin_lock(lock);
  756. if (*local_list) {
  757. list_splice(*local_list, list);
  758. *local_list = NULL;
  759. }
  760. spin_unlock(lock);
  761. }
  762. /**
  763. * restore_vm_bo_list() - move vm_bo elements back to their original list
  764. * @__gpuvm: the &drm_gpuvm
  765. * @__list_name: the name of the list we're iterating on
  766. *
  767. * When we're done iterating a vm_bo list, we should call restore_vm_bo_list()
  768. * to restore the original state and let new iterations take place.
  769. */
  770. #define restore_vm_bo_list(__gpuvm, __list_name) \
  771. __restore_vm_bo_list((__gpuvm), &(__gpuvm)->__list_name.lock, \
  772. &(__gpuvm)->__list_name.list, \
  773. &(__gpuvm)->__list_name.local_list)
  774. static void
  775. cond_spin_lock(spinlock_t *lock, bool cond)
  776. {
  777. if (cond)
  778. spin_lock(lock);
  779. }
  780. static void
  781. cond_spin_unlock(spinlock_t *lock, bool cond)
  782. {
  783. if (cond)
  784. spin_unlock(lock);
  785. }
  786. static void
  787. __drm_gpuvm_bo_list_add(struct drm_gpuvm *gpuvm, spinlock_t *lock,
  788. struct list_head *entry, struct list_head *list)
  789. {
  790. cond_spin_lock(lock, !!lock);
  791. if (list_empty(entry))
  792. list_add_tail(entry, list);
  793. cond_spin_unlock(lock, !!lock);
  794. }
  795. /**
  796. * drm_gpuvm_bo_list_add() - insert a vm_bo into the given list
  797. * @__vm_bo: the &drm_gpuvm_bo
  798. * @__list_name: the name of the list to insert into
  799. * @__lock: whether to lock with the internal spinlock
  800. *
  801. * Inserts the given @__vm_bo into the list specified by @__list_name.
  802. */
  803. #define drm_gpuvm_bo_list_add(__vm_bo, __list_name, __lock) \
  804. __drm_gpuvm_bo_list_add((__vm_bo)->vm, \
  805. __lock ? &(__vm_bo)->vm->__list_name.lock : \
  806. NULL, \
  807. &(__vm_bo)->list.entry.__list_name, \
  808. &(__vm_bo)->vm->__list_name.list)
  809. static void
  810. __drm_gpuvm_bo_list_del(struct drm_gpuvm *gpuvm, spinlock_t *lock,
  811. struct list_head *entry, bool init)
  812. {
  813. cond_spin_lock(lock, !!lock);
  814. if (init) {
  815. if (!list_empty(entry))
  816. list_del_init(entry);
  817. } else {
  818. list_del(entry);
  819. }
  820. cond_spin_unlock(lock, !!lock);
  821. }
  822. /**
  823. * drm_gpuvm_bo_list_del_init() - remove a vm_bo from the given list
  824. * @__vm_bo: the &drm_gpuvm_bo
  825. * @__list_name: the name of the list to insert into
  826. * @__lock: whether to lock with the internal spinlock
  827. *
  828. * Removes the given @__vm_bo from the list specified by @__list_name.
  829. */
  830. #define drm_gpuvm_bo_list_del_init(__vm_bo, __list_name, __lock) \
  831. __drm_gpuvm_bo_list_del((__vm_bo)->vm, \
  832. __lock ? &(__vm_bo)->vm->__list_name.lock : \
  833. NULL, \
  834. &(__vm_bo)->list.entry.__list_name, \
  835. true)
  836. /**
  837. * drm_gpuvm_bo_list_del() - remove a vm_bo from the given list
  838. * @__vm_bo: the &drm_gpuvm_bo
  839. * @__list_name: the name of the list to insert into
  840. * @__lock: whether to lock with the internal spinlock
  841. *
  842. * Removes the given @__vm_bo from the list specified by @__list_name.
  843. */
  844. #define drm_gpuvm_bo_list_del(__vm_bo, __list_name, __lock) \
  845. __drm_gpuvm_bo_list_del((__vm_bo)->vm, \
  846. __lock ? &(__vm_bo)->vm->__list_name.lock : \
  847. NULL, \
  848. &(__vm_bo)->list.entry.__list_name, \
  849. false)
  850. #define to_drm_gpuva(__node) container_of((__node), struct drm_gpuva, rb.node)
  851. #define GPUVA_START(node) ((node)->va.addr)
  852. #define GPUVA_LAST(node) ((node)->va.addr + (node)->va.range - 1)
  853. /* We do not actually use drm_gpuva_it_next(), tell the compiler to not complain
  854. * about this.
  855. */
  856. INTERVAL_TREE_DEFINE(struct drm_gpuva, rb.node, u64, rb.__subtree_last,
  857. GPUVA_START, GPUVA_LAST, static __maybe_unused,
  858. drm_gpuva_it)
  859. static int __drm_gpuva_insert(struct drm_gpuvm *gpuvm,
  860. struct drm_gpuva *va);
  861. static void __drm_gpuva_remove(struct drm_gpuva *va);
  862. static bool
  863. drm_gpuvm_check_overflow(u64 addr, u64 range)
  864. {
  865. u64 end;
  866. return check_add_overflow(addr, range, &end);
  867. }
  868. static bool
  869. drm_gpuvm_warn_check_overflow(struct drm_gpuvm *gpuvm, u64 addr, u64 range)
  870. {
  871. return drm_WARN(gpuvm->drm, drm_gpuvm_check_overflow(addr, range),
  872. "GPUVA address limited to %zu bytes.\n", sizeof(addr));
  873. }
  874. static bool
  875. drm_gpuvm_in_mm_range(struct drm_gpuvm *gpuvm, u64 addr, u64 range)
  876. {
  877. u64 end = addr + range;
  878. u64 mm_start = gpuvm->mm_start;
  879. u64 mm_end = mm_start + gpuvm->mm_range;
  880. return addr >= mm_start && end <= mm_end;
  881. }
  882. static bool
  883. drm_gpuvm_in_kernel_node(struct drm_gpuvm *gpuvm, u64 addr, u64 range)
  884. {
  885. u64 end = addr + range;
  886. u64 kstart = gpuvm->kernel_alloc_node.va.addr;
  887. u64 krange = gpuvm->kernel_alloc_node.va.range;
  888. u64 kend = kstart + krange;
  889. return krange && addr < kend && kstart < end;
  890. }
  891. /**
  892. * drm_gpuvm_range_valid() - checks whether the given range is valid for the
  893. * given &drm_gpuvm
  894. * @gpuvm: the GPUVM to check the range for
  895. * @addr: the base address
  896. * @range: the range starting from the base address
  897. *
  898. * Checks whether the range is within the GPUVM's managed boundaries.
  899. *
  900. * Returns: true for a valid range, false otherwise
  901. */
  902. bool
  903. drm_gpuvm_range_valid(struct drm_gpuvm *gpuvm,
  904. u64 addr, u64 range)
  905. {
  906. return !drm_gpuvm_check_overflow(addr, range) &&
  907. drm_gpuvm_in_mm_range(gpuvm, addr, range) &&
  908. !drm_gpuvm_in_kernel_node(gpuvm, addr, range);
  909. }
  910. EXPORT_SYMBOL_GPL(drm_gpuvm_range_valid);
  911. static void
  912. drm_gpuvm_gem_object_free(struct drm_gem_object *obj)
  913. {
  914. drm_gem_object_release(obj);
  915. kfree(obj);
  916. }
  917. static const struct drm_gem_object_funcs drm_gpuvm_object_funcs = {
  918. .free = drm_gpuvm_gem_object_free,
  919. };
  920. /**
  921. * drm_gpuvm_resv_object_alloc() - allocate a dummy &drm_gem_object
  922. * @drm: the drivers &drm_device
  923. *
  924. * Allocates a dummy &drm_gem_object which can be passed to drm_gpuvm_init() in
  925. * order to serve as root GEM object providing the &drm_resv shared across
  926. * &drm_gem_objects local to a single GPUVM.
  927. *
  928. * Returns: the &drm_gem_object on success, NULL on failure
  929. */
  930. struct drm_gem_object *
  931. drm_gpuvm_resv_object_alloc(struct drm_device *drm)
  932. {
  933. struct drm_gem_object *obj;
  934. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  935. if (!obj)
  936. return NULL;
  937. obj->funcs = &drm_gpuvm_object_funcs;
  938. drm_gem_private_object_init(drm, obj, 0);
  939. return obj;
  940. }
  941. EXPORT_SYMBOL_GPL(drm_gpuvm_resv_object_alloc);
  942. /**
  943. * drm_gpuvm_init() - initialize a &drm_gpuvm
  944. * @gpuvm: pointer to the &drm_gpuvm to initialize
  945. * @name: the name of the GPU VA space
  946. * @flags: the &drm_gpuvm_flags for this GPUVM
  947. * @drm: the &drm_device this VM resides in
  948. * @r_obj: the resv &drm_gem_object providing the GPUVM's common &dma_resv
  949. * @start_offset: the start offset of the GPU VA space
  950. * @range: the size of the GPU VA space
  951. * @reserve_offset: the start of the kernel reserved GPU VA area
  952. * @reserve_range: the size of the kernel reserved GPU VA area
  953. * @ops: &drm_gpuvm_ops called on &drm_gpuvm_sm_map / &drm_gpuvm_sm_unmap
  954. *
  955. * The &drm_gpuvm must be initialized with this function before use.
  956. *
  957. * Note that @gpuvm must be cleared to 0 before calling this function. The given
  958. * &name is expected to be managed by the surrounding driver structures.
  959. */
  960. void
  961. drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name,
  962. enum drm_gpuvm_flags flags,
  963. struct drm_device *drm,
  964. struct drm_gem_object *r_obj,
  965. u64 start_offset, u64 range,
  966. u64 reserve_offset, u64 reserve_range,
  967. const struct drm_gpuvm_ops *ops)
  968. {
  969. gpuvm->rb.tree = RB_ROOT_CACHED;
  970. INIT_LIST_HEAD(&gpuvm->rb.list);
  971. INIT_LIST_HEAD(&gpuvm->extobj.list);
  972. spin_lock_init(&gpuvm->extobj.lock);
  973. INIT_LIST_HEAD(&gpuvm->evict.list);
  974. spin_lock_init(&gpuvm->evict.lock);
  975. kref_init(&gpuvm->kref);
  976. gpuvm->name = name ? name : "unknown";
  977. gpuvm->flags = flags;
  978. gpuvm->ops = ops;
  979. gpuvm->drm = drm;
  980. gpuvm->r_obj = r_obj;
  981. drm_gem_object_get(r_obj);
  982. drm_gpuvm_warn_check_overflow(gpuvm, start_offset, range);
  983. gpuvm->mm_start = start_offset;
  984. gpuvm->mm_range = range;
  985. memset(&gpuvm->kernel_alloc_node, 0, sizeof(struct drm_gpuva));
  986. if (reserve_range) {
  987. gpuvm->kernel_alloc_node.va.addr = reserve_offset;
  988. gpuvm->kernel_alloc_node.va.range = reserve_range;
  989. if (likely(!drm_gpuvm_warn_check_overflow(gpuvm, reserve_offset,
  990. reserve_range)))
  991. __drm_gpuva_insert(gpuvm, &gpuvm->kernel_alloc_node);
  992. }
  993. }
  994. EXPORT_SYMBOL_GPL(drm_gpuvm_init);
  995. static void
  996. drm_gpuvm_fini(struct drm_gpuvm *gpuvm)
  997. {
  998. gpuvm->name = NULL;
  999. if (gpuvm->kernel_alloc_node.va.range)
  1000. __drm_gpuva_remove(&gpuvm->kernel_alloc_node);
  1001. drm_WARN(gpuvm->drm, !RB_EMPTY_ROOT(&gpuvm->rb.tree.rb_root),
  1002. "GPUVA tree is not empty, potentially leaking memory.\n");
  1003. drm_WARN(gpuvm->drm, !list_empty(&gpuvm->extobj.list),
  1004. "Extobj list should be empty.\n");
  1005. drm_WARN(gpuvm->drm, !list_empty(&gpuvm->evict.list),
  1006. "Evict list should be empty.\n");
  1007. drm_gem_object_put(gpuvm->r_obj);
  1008. }
  1009. static void
  1010. drm_gpuvm_free(struct kref *kref)
  1011. {
  1012. struct drm_gpuvm *gpuvm = container_of(kref, struct drm_gpuvm, kref);
  1013. drm_gpuvm_fini(gpuvm);
  1014. if (drm_WARN_ON(gpuvm->drm, !gpuvm->ops->vm_free))
  1015. return;
  1016. gpuvm->ops->vm_free(gpuvm);
  1017. }
  1018. /**
  1019. * drm_gpuvm_put() - drop a struct drm_gpuvm reference
  1020. * @gpuvm: the &drm_gpuvm to release the reference of
  1021. *
  1022. * This releases a reference to @gpuvm.
  1023. *
  1024. * This function may be called from atomic context.
  1025. */
  1026. void
  1027. drm_gpuvm_put(struct drm_gpuvm *gpuvm)
  1028. {
  1029. if (gpuvm)
  1030. kref_put(&gpuvm->kref, drm_gpuvm_free);
  1031. }
  1032. EXPORT_SYMBOL_GPL(drm_gpuvm_put);
  1033. static int
  1034. exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
  1035. unsigned int num_fences)
  1036. {
  1037. return num_fences ? drm_exec_prepare_obj(exec, obj, num_fences) :
  1038. drm_exec_lock_obj(exec, obj);
  1039. }
  1040. /**
  1041. * drm_gpuvm_prepare_vm() - prepare the GPUVMs common dma-resv
  1042. * @gpuvm: the &drm_gpuvm
  1043. * @exec: the &drm_exec context
  1044. * @num_fences: the amount of &dma_fences to reserve
  1045. *
  1046. * Calls drm_exec_prepare_obj() for the GPUVMs dummy &drm_gem_object; if
  1047. * @num_fences is zero drm_exec_lock_obj() is called instead.
  1048. *
  1049. * Using this function directly, it is the drivers responsibility to call
  1050. * drm_exec_init() and drm_exec_fini() accordingly.
  1051. *
  1052. * Returns: 0 on success, negative error code on failure.
  1053. */
  1054. int
  1055. drm_gpuvm_prepare_vm(struct drm_gpuvm *gpuvm,
  1056. struct drm_exec *exec,
  1057. unsigned int num_fences)
  1058. {
  1059. return exec_prepare_obj(exec, gpuvm->r_obj, num_fences);
  1060. }
  1061. EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_vm);
  1062. static int
  1063. __drm_gpuvm_prepare_objects(struct drm_gpuvm *gpuvm,
  1064. struct drm_exec *exec,
  1065. unsigned int num_fences)
  1066. {
  1067. struct drm_gpuvm_bo *vm_bo;
  1068. LIST_HEAD(extobjs);
  1069. int ret = 0;
  1070. for_each_vm_bo_in_list(gpuvm, extobj, &extobjs, vm_bo) {
  1071. ret = exec_prepare_obj(exec, vm_bo->obj, num_fences);
  1072. if (ret)
  1073. break;
  1074. }
  1075. /* Drop ref in case we break out of the loop. */
  1076. drm_gpuvm_bo_put(vm_bo);
  1077. restore_vm_bo_list(gpuvm, extobj);
  1078. return ret;
  1079. }
  1080. static int
  1081. drm_gpuvm_prepare_objects_locked(struct drm_gpuvm *gpuvm,
  1082. struct drm_exec *exec,
  1083. unsigned int num_fences)
  1084. {
  1085. struct drm_gpuvm_bo *vm_bo;
  1086. int ret = 0;
  1087. drm_gpuvm_resv_assert_held(gpuvm);
  1088. list_for_each_entry(vm_bo, &gpuvm->extobj.list, list.entry.extobj) {
  1089. ret = exec_prepare_obj(exec, vm_bo->obj, num_fences);
  1090. if (ret)
  1091. break;
  1092. if (vm_bo->evicted)
  1093. drm_gpuvm_bo_list_add(vm_bo, evict, false);
  1094. }
  1095. return ret;
  1096. }
  1097. /**
  1098. * drm_gpuvm_prepare_objects() - prepare all assoiciated BOs
  1099. * @gpuvm: the &drm_gpuvm
  1100. * @exec: the &drm_exec locking context
  1101. * @num_fences: the amount of &dma_fences to reserve
  1102. *
  1103. * Calls drm_exec_prepare_obj() for all &drm_gem_objects the given
  1104. * &drm_gpuvm contains mappings of; if @num_fences is zero drm_exec_lock_obj()
  1105. * is called instead.
  1106. *
  1107. * Using this function directly, it is the drivers responsibility to call
  1108. * drm_exec_init() and drm_exec_fini() accordingly.
  1109. *
  1110. * Note: This function is safe against concurrent insertion and removal of
  1111. * external objects, however it is not safe against concurrent usage itself.
  1112. *
  1113. * Drivers need to make sure to protect this case with either an outer VM lock
  1114. * or by calling drm_gpuvm_prepare_vm() before this function within the
  1115. * drm_exec_until_all_locked() loop, such that the GPUVM's dma-resv lock ensures
  1116. * mutual exclusion.
  1117. *
  1118. * Returns: 0 on success, negative error code on failure.
  1119. */
  1120. int
  1121. drm_gpuvm_prepare_objects(struct drm_gpuvm *gpuvm,
  1122. struct drm_exec *exec,
  1123. unsigned int num_fences)
  1124. {
  1125. if (drm_gpuvm_resv_protected(gpuvm))
  1126. return drm_gpuvm_prepare_objects_locked(gpuvm, exec,
  1127. num_fences);
  1128. else
  1129. return __drm_gpuvm_prepare_objects(gpuvm, exec, num_fences);
  1130. }
  1131. EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_objects);
  1132. /**
  1133. * drm_gpuvm_prepare_range() - prepare all BOs mapped within a given range
  1134. * @gpuvm: the &drm_gpuvm
  1135. * @exec: the &drm_exec locking context
  1136. * @addr: the start address within the VA space
  1137. * @range: the range to iterate within the VA space
  1138. * @num_fences: the amount of &dma_fences to reserve
  1139. *
  1140. * Calls drm_exec_prepare_obj() for all &drm_gem_objects mapped between @addr
  1141. * and @addr + @range; if @num_fences is zero drm_exec_lock_obj() is called
  1142. * instead.
  1143. *
  1144. * Returns: 0 on success, negative error code on failure.
  1145. */
  1146. int
  1147. drm_gpuvm_prepare_range(struct drm_gpuvm *gpuvm, struct drm_exec *exec,
  1148. u64 addr, u64 range, unsigned int num_fences)
  1149. {
  1150. struct drm_gpuva *va;
  1151. u64 end = addr + range;
  1152. int ret;
  1153. drm_gpuvm_for_each_va_range(va, gpuvm, addr, end) {
  1154. struct drm_gem_object *obj = va->gem.obj;
  1155. ret = exec_prepare_obj(exec, obj, num_fences);
  1156. if (ret)
  1157. return ret;
  1158. }
  1159. return 0;
  1160. }
  1161. EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_range);
  1162. /**
  1163. * drm_gpuvm_exec_lock() - lock all dma-resv of all assoiciated BOs
  1164. * @vm_exec: the &drm_gpuvm_exec wrapper
  1165. *
  1166. * Acquires all dma-resv locks of all &drm_gem_objects the given
  1167. * &drm_gpuvm contains mappings of.
  1168. *
  1169. * Addionally, when calling this function with struct drm_gpuvm_exec::extra
  1170. * being set the driver receives the given @fn callback to lock additional
  1171. * dma-resv in the context of the &drm_gpuvm_exec instance. Typically, drivers
  1172. * would call drm_exec_prepare_obj() from within this callback.
  1173. *
  1174. * Returns: 0 on success, negative error code on failure.
  1175. */
  1176. int
  1177. drm_gpuvm_exec_lock(struct drm_gpuvm_exec *vm_exec)
  1178. {
  1179. struct drm_gpuvm *gpuvm = vm_exec->vm;
  1180. struct drm_exec *exec = &vm_exec->exec;
  1181. unsigned int num_fences = vm_exec->num_fences;
  1182. int ret;
  1183. drm_exec_init(exec, vm_exec->flags, 0);
  1184. drm_exec_until_all_locked(exec) {
  1185. ret = drm_gpuvm_prepare_vm(gpuvm, exec, num_fences);
  1186. drm_exec_retry_on_contention(exec);
  1187. if (ret)
  1188. goto err;
  1189. ret = drm_gpuvm_prepare_objects(gpuvm, exec, num_fences);
  1190. drm_exec_retry_on_contention(exec);
  1191. if (ret)
  1192. goto err;
  1193. if (vm_exec->extra.fn) {
  1194. ret = vm_exec->extra.fn(vm_exec);
  1195. drm_exec_retry_on_contention(exec);
  1196. if (ret)
  1197. goto err;
  1198. }
  1199. }
  1200. return 0;
  1201. err:
  1202. drm_exec_fini(exec);
  1203. return ret;
  1204. }
  1205. EXPORT_SYMBOL_GPL(drm_gpuvm_exec_lock);
  1206. static int
  1207. fn_lock_array(struct drm_gpuvm_exec *vm_exec)
  1208. {
  1209. struct {
  1210. struct drm_gem_object **objs;
  1211. unsigned int num_objs;
  1212. } *args = vm_exec->extra.priv;
  1213. return drm_exec_prepare_array(&vm_exec->exec, args->objs,
  1214. args->num_objs, vm_exec->num_fences);
  1215. }
  1216. /**
  1217. * drm_gpuvm_exec_lock_array() - lock all dma-resv of all assoiciated BOs
  1218. * @vm_exec: the &drm_gpuvm_exec wrapper
  1219. * @objs: additional &drm_gem_objects to lock
  1220. * @num_objs: the number of additional &drm_gem_objects to lock
  1221. *
  1222. * Acquires all dma-resv locks of all &drm_gem_objects the given &drm_gpuvm
  1223. * contains mappings of, plus the ones given through @objs.
  1224. *
  1225. * Returns: 0 on success, negative error code on failure.
  1226. */
  1227. int
  1228. drm_gpuvm_exec_lock_array(struct drm_gpuvm_exec *vm_exec,
  1229. struct drm_gem_object **objs,
  1230. unsigned int num_objs)
  1231. {
  1232. struct {
  1233. struct drm_gem_object **objs;
  1234. unsigned int num_objs;
  1235. } args;
  1236. args.objs = objs;
  1237. args.num_objs = num_objs;
  1238. vm_exec->extra.fn = fn_lock_array;
  1239. vm_exec->extra.priv = &args;
  1240. return drm_gpuvm_exec_lock(vm_exec);
  1241. }
  1242. EXPORT_SYMBOL_GPL(drm_gpuvm_exec_lock_array);
  1243. /**
  1244. * drm_gpuvm_exec_lock_range() - prepare all BOs mapped within a given range
  1245. * @vm_exec: the &drm_gpuvm_exec wrapper
  1246. * @addr: the start address within the VA space
  1247. * @range: the range to iterate within the VA space
  1248. *
  1249. * Acquires all dma-resv locks of all &drm_gem_objects mapped between @addr and
  1250. * @addr + @range.
  1251. *
  1252. * Returns: 0 on success, negative error code on failure.
  1253. */
  1254. int
  1255. drm_gpuvm_exec_lock_range(struct drm_gpuvm_exec *vm_exec,
  1256. u64 addr, u64 range)
  1257. {
  1258. struct drm_gpuvm *gpuvm = vm_exec->vm;
  1259. struct drm_exec *exec = &vm_exec->exec;
  1260. int ret;
  1261. drm_exec_init(exec, vm_exec->flags, 0);
  1262. drm_exec_until_all_locked(exec) {
  1263. ret = drm_gpuvm_prepare_range(gpuvm, exec, addr, range,
  1264. vm_exec->num_fences);
  1265. drm_exec_retry_on_contention(exec);
  1266. if (ret)
  1267. goto err;
  1268. }
  1269. return ret;
  1270. err:
  1271. drm_exec_fini(exec);
  1272. return ret;
  1273. }
  1274. EXPORT_SYMBOL_GPL(drm_gpuvm_exec_lock_range);
  1275. static int
  1276. __drm_gpuvm_validate(struct drm_gpuvm *gpuvm, struct drm_exec *exec)
  1277. {
  1278. const struct drm_gpuvm_ops *ops = gpuvm->ops;
  1279. struct drm_gpuvm_bo *vm_bo;
  1280. LIST_HEAD(evict);
  1281. int ret = 0;
  1282. for_each_vm_bo_in_list(gpuvm, evict, &evict, vm_bo) {
  1283. ret = ops->vm_bo_validate(vm_bo, exec);
  1284. if (ret)
  1285. break;
  1286. }
  1287. /* Drop ref in case we break out of the loop. */
  1288. drm_gpuvm_bo_put(vm_bo);
  1289. restore_vm_bo_list(gpuvm, evict);
  1290. return ret;
  1291. }
  1292. static int
  1293. drm_gpuvm_validate_locked(struct drm_gpuvm *gpuvm, struct drm_exec *exec)
  1294. {
  1295. const struct drm_gpuvm_ops *ops = gpuvm->ops;
  1296. struct drm_gpuvm_bo *vm_bo, *next;
  1297. int ret = 0;
  1298. drm_gpuvm_resv_assert_held(gpuvm);
  1299. list_for_each_entry_safe(vm_bo, next, &gpuvm->evict.list,
  1300. list.entry.evict) {
  1301. ret = ops->vm_bo_validate(vm_bo, exec);
  1302. if (ret)
  1303. break;
  1304. dma_resv_assert_held(vm_bo->obj->resv);
  1305. if (!vm_bo->evicted)
  1306. drm_gpuvm_bo_list_del_init(vm_bo, evict, false);
  1307. }
  1308. return ret;
  1309. }
  1310. /**
  1311. * drm_gpuvm_validate() - validate all BOs marked as evicted
  1312. * @gpuvm: the &drm_gpuvm to validate evicted BOs
  1313. * @exec: the &drm_exec instance used for locking the GPUVM
  1314. *
  1315. * Calls the &drm_gpuvm_ops::vm_bo_validate callback for all evicted buffer
  1316. * objects being mapped in the given &drm_gpuvm.
  1317. *
  1318. * Returns: 0 on success, negative error code on failure.
  1319. */
  1320. int
  1321. drm_gpuvm_validate(struct drm_gpuvm *gpuvm, struct drm_exec *exec)
  1322. {
  1323. const struct drm_gpuvm_ops *ops = gpuvm->ops;
  1324. if (unlikely(!ops || !ops->vm_bo_validate))
  1325. return -EOPNOTSUPP;
  1326. if (drm_gpuvm_resv_protected(gpuvm))
  1327. return drm_gpuvm_validate_locked(gpuvm, exec);
  1328. else
  1329. return __drm_gpuvm_validate(gpuvm, exec);
  1330. }
  1331. EXPORT_SYMBOL_GPL(drm_gpuvm_validate);
  1332. /**
  1333. * drm_gpuvm_resv_add_fence - add fence to private and all extobj
  1334. * dma-resv
  1335. * @gpuvm: the &drm_gpuvm to add a fence to
  1336. * @exec: the &drm_exec locking context
  1337. * @fence: fence to add
  1338. * @private_usage: private dma-resv usage
  1339. * @extobj_usage: extobj dma-resv usage
  1340. */
  1341. void
  1342. drm_gpuvm_resv_add_fence(struct drm_gpuvm *gpuvm,
  1343. struct drm_exec *exec,
  1344. struct dma_fence *fence,
  1345. enum dma_resv_usage private_usage,
  1346. enum dma_resv_usage extobj_usage)
  1347. {
  1348. struct drm_gem_object *obj;
  1349. unsigned long index;
  1350. drm_exec_for_each_locked_object(exec, index, obj) {
  1351. dma_resv_assert_held(obj->resv);
  1352. dma_resv_add_fence(obj->resv, fence,
  1353. drm_gpuvm_is_extobj(gpuvm, obj) ?
  1354. extobj_usage : private_usage);
  1355. }
  1356. }
  1357. EXPORT_SYMBOL_GPL(drm_gpuvm_resv_add_fence);
  1358. /**
  1359. * drm_gpuvm_bo_create() - create a new instance of struct drm_gpuvm_bo
  1360. * @gpuvm: The &drm_gpuvm the @obj is mapped in.
  1361. * @obj: The &drm_gem_object being mapped in the @gpuvm.
  1362. *
  1363. * If provided by the driver, this function uses the &drm_gpuvm_ops
  1364. * vm_bo_alloc() callback to allocate.
  1365. *
  1366. * Returns: a pointer to the &drm_gpuvm_bo on success, NULL on failure
  1367. */
  1368. struct drm_gpuvm_bo *
  1369. drm_gpuvm_bo_create(struct drm_gpuvm *gpuvm,
  1370. struct drm_gem_object *obj)
  1371. {
  1372. const struct drm_gpuvm_ops *ops = gpuvm->ops;
  1373. struct drm_gpuvm_bo *vm_bo;
  1374. if (ops && ops->vm_bo_alloc)
  1375. vm_bo = ops->vm_bo_alloc();
  1376. else
  1377. vm_bo = kzalloc(sizeof(*vm_bo), GFP_KERNEL);
  1378. if (unlikely(!vm_bo))
  1379. return NULL;
  1380. vm_bo->vm = drm_gpuvm_get(gpuvm);
  1381. vm_bo->obj = obj;
  1382. drm_gem_object_get(obj);
  1383. kref_init(&vm_bo->kref);
  1384. INIT_LIST_HEAD(&vm_bo->list.gpuva);
  1385. INIT_LIST_HEAD(&vm_bo->list.entry.gem);
  1386. INIT_LIST_HEAD(&vm_bo->list.entry.extobj);
  1387. INIT_LIST_HEAD(&vm_bo->list.entry.evict);
  1388. return vm_bo;
  1389. }
  1390. EXPORT_SYMBOL_GPL(drm_gpuvm_bo_create);
  1391. static void
  1392. drm_gpuvm_bo_destroy(struct kref *kref)
  1393. {
  1394. struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo,
  1395. kref);
  1396. struct drm_gpuvm *gpuvm = vm_bo->vm;
  1397. const struct drm_gpuvm_ops *ops = gpuvm->ops;
  1398. struct drm_gem_object *obj = vm_bo->obj;
  1399. bool lock = !drm_gpuvm_resv_protected(gpuvm);
  1400. if (!lock)
  1401. drm_gpuvm_resv_assert_held(gpuvm);
  1402. drm_gpuvm_bo_list_del(vm_bo, extobj, lock);
  1403. drm_gpuvm_bo_list_del(vm_bo, evict, lock);
  1404. drm_gem_gpuva_assert_lock_held(obj);
  1405. list_del(&vm_bo->list.entry.gem);
  1406. if (ops && ops->vm_bo_free)
  1407. ops->vm_bo_free(vm_bo);
  1408. else
  1409. kfree(vm_bo);
  1410. drm_gpuvm_put(gpuvm);
  1411. drm_gem_object_put(obj);
  1412. }
  1413. /**
  1414. * drm_gpuvm_bo_put() - drop a struct drm_gpuvm_bo reference
  1415. * @vm_bo: the &drm_gpuvm_bo to release the reference of
  1416. *
  1417. * This releases a reference to @vm_bo.
  1418. *
  1419. * If the reference count drops to zero, the &gpuvm_bo is destroyed, which
  1420. * includes removing it from the GEMs gpuva list. Hence, if a call to this
  1421. * function can potentially let the reference count drop to zero the caller must
  1422. * hold the dma-resv or driver specific GEM gpuva lock.
  1423. *
  1424. * This function may only be called from non-atomic context.
  1425. *
  1426. * Returns: true if vm_bo was destroyed, false otherwise.
  1427. */
  1428. bool
  1429. drm_gpuvm_bo_put(struct drm_gpuvm_bo *vm_bo)
  1430. {
  1431. might_sleep();
  1432. if (vm_bo)
  1433. return !!kref_put(&vm_bo->kref, drm_gpuvm_bo_destroy);
  1434. return false;
  1435. }
  1436. EXPORT_SYMBOL_GPL(drm_gpuvm_bo_put);
  1437. static struct drm_gpuvm_bo *
  1438. __drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm,
  1439. struct drm_gem_object *obj)
  1440. {
  1441. struct drm_gpuvm_bo *vm_bo;
  1442. drm_gem_gpuva_assert_lock_held(obj);
  1443. drm_gem_for_each_gpuvm_bo(vm_bo, obj)
  1444. if (vm_bo->vm == gpuvm)
  1445. return vm_bo;
  1446. return NULL;
  1447. }
  1448. /**
  1449. * drm_gpuvm_bo_find() - find the &drm_gpuvm_bo for the given
  1450. * &drm_gpuvm and &drm_gem_object
  1451. * @gpuvm: The &drm_gpuvm the @obj is mapped in.
  1452. * @obj: The &drm_gem_object being mapped in the @gpuvm.
  1453. *
  1454. * Find the &drm_gpuvm_bo representing the combination of the given
  1455. * &drm_gpuvm and &drm_gem_object. If found, increases the reference
  1456. * count of the &drm_gpuvm_bo accordingly.
  1457. *
  1458. * Returns: a pointer to the &drm_gpuvm_bo on success, NULL on failure
  1459. */
  1460. struct drm_gpuvm_bo *
  1461. drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm,
  1462. struct drm_gem_object *obj)
  1463. {
  1464. struct drm_gpuvm_bo *vm_bo = __drm_gpuvm_bo_find(gpuvm, obj);
  1465. return vm_bo ? drm_gpuvm_bo_get(vm_bo) : NULL;
  1466. }
  1467. EXPORT_SYMBOL_GPL(drm_gpuvm_bo_find);
  1468. /**
  1469. * drm_gpuvm_bo_obtain() - obtains and instance of the &drm_gpuvm_bo for the
  1470. * given &drm_gpuvm and &drm_gem_object
  1471. * @gpuvm: The &drm_gpuvm the @obj is mapped in.
  1472. * @obj: The &drm_gem_object being mapped in the @gpuvm.
  1473. *
  1474. * Find the &drm_gpuvm_bo representing the combination of the given
  1475. * &drm_gpuvm and &drm_gem_object. If found, increases the reference
  1476. * count of the &drm_gpuvm_bo accordingly. If not found, allocates a new
  1477. * &drm_gpuvm_bo.
  1478. *
  1479. * A new &drm_gpuvm_bo is added to the GEMs gpuva list.
  1480. *
  1481. * Returns: a pointer to the &drm_gpuvm_bo on success, an ERR_PTR on failure
  1482. */
  1483. struct drm_gpuvm_bo *
  1484. drm_gpuvm_bo_obtain(struct drm_gpuvm *gpuvm,
  1485. struct drm_gem_object *obj)
  1486. {
  1487. struct drm_gpuvm_bo *vm_bo;
  1488. vm_bo = drm_gpuvm_bo_find(gpuvm, obj);
  1489. if (vm_bo)
  1490. return vm_bo;
  1491. vm_bo = drm_gpuvm_bo_create(gpuvm, obj);
  1492. if (!vm_bo)
  1493. return ERR_PTR(-ENOMEM);
  1494. drm_gem_gpuva_assert_lock_held(obj);
  1495. list_add_tail(&vm_bo->list.entry.gem, &obj->gpuva.list);
  1496. return vm_bo;
  1497. }
  1498. EXPORT_SYMBOL_GPL(drm_gpuvm_bo_obtain);
  1499. /**
  1500. * drm_gpuvm_bo_obtain_prealloc() - obtains and instance of the &drm_gpuvm_bo
  1501. * for the given &drm_gpuvm and &drm_gem_object
  1502. * @__vm_bo: A pre-allocated struct drm_gpuvm_bo.
  1503. *
  1504. * Find the &drm_gpuvm_bo representing the combination of the given
  1505. * &drm_gpuvm and &drm_gem_object. If found, increases the reference
  1506. * count of the found &drm_gpuvm_bo accordingly, while the @__vm_bo reference
  1507. * count is decreased. If not found @__vm_bo is returned without further
  1508. * increase of the reference count.
  1509. *
  1510. * A new &drm_gpuvm_bo is added to the GEMs gpuva list.
  1511. *
  1512. * Returns: a pointer to the found &drm_gpuvm_bo or @__vm_bo if no existing
  1513. * &drm_gpuvm_bo was found
  1514. */
  1515. struct drm_gpuvm_bo *
  1516. drm_gpuvm_bo_obtain_prealloc(struct drm_gpuvm_bo *__vm_bo)
  1517. {
  1518. struct drm_gpuvm *gpuvm = __vm_bo->vm;
  1519. struct drm_gem_object *obj = __vm_bo->obj;
  1520. struct drm_gpuvm_bo *vm_bo;
  1521. vm_bo = drm_gpuvm_bo_find(gpuvm, obj);
  1522. if (vm_bo) {
  1523. drm_gpuvm_bo_put(__vm_bo);
  1524. return vm_bo;
  1525. }
  1526. drm_gem_gpuva_assert_lock_held(obj);
  1527. list_add_tail(&__vm_bo->list.entry.gem, &obj->gpuva.list);
  1528. return __vm_bo;
  1529. }
  1530. EXPORT_SYMBOL_GPL(drm_gpuvm_bo_obtain_prealloc);
  1531. /**
  1532. * drm_gpuvm_bo_extobj_add() - adds the &drm_gpuvm_bo to its &drm_gpuvm's
  1533. * extobj list
  1534. * @vm_bo: The &drm_gpuvm_bo to add to its &drm_gpuvm's the extobj list.
  1535. *
  1536. * Adds the given @vm_bo to its &drm_gpuvm's extobj list if not on the list
  1537. * already and if the corresponding &drm_gem_object is an external object,
  1538. * actually.
  1539. */
  1540. void
  1541. drm_gpuvm_bo_extobj_add(struct drm_gpuvm_bo *vm_bo)
  1542. {
  1543. struct drm_gpuvm *gpuvm = vm_bo->vm;
  1544. bool lock = !drm_gpuvm_resv_protected(gpuvm);
  1545. if (!lock)
  1546. drm_gpuvm_resv_assert_held(gpuvm);
  1547. if (drm_gpuvm_is_extobj(gpuvm, vm_bo->obj))
  1548. drm_gpuvm_bo_list_add(vm_bo, extobj, lock);
  1549. }
  1550. EXPORT_SYMBOL_GPL(drm_gpuvm_bo_extobj_add);
  1551. /**
  1552. * drm_gpuvm_bo_evict() - add / remove a &drm_gpuvm_bo to / from the &drm_gpuvms
  1553. * evicted list
  1554. * @vm_bo: the &drm_gpuvm_bo to add or remove
  1555. * @evict: indicates whether the object is evicted
  1556. *
  1557. * Adds a &drm_gpuvm_bo to or removes it from the &drm_gpuvms evicted list.
  1558. */
  1559. void
  1560. drm_gpuvm_bo_evict(struct drm_gpuvm_bo *vm_bo, bool evict)
  1561. {
  1562. struct drm_gpuvm *gpuvm = vm_bo->vm;
  1563. struct drm_gem_object *obj = vm_bo->obj;
  1564. bool lock = !drm_gpuvm_resv_protected(gpuvm);
  1565. dma_resv_assert_held(obj->resv);
  1566. vm_bo->evicted = evict;
  1567. /* Can't add external objects to the evicted list directly if not using
  1568. * internal spinlocks, since in this case the evicted list is protected
  1569. * with the VM's common dma-resv lock.
  1570. */
  1571. if (drm_gpuvm_is_extobj(gpuvm, obj) && !lock)
  1572. return;
  1573. if (evict)
  1574. drm_gpuvm_bo_list_add(vm_bo, evict, lock);
  1575. else
  1576. drm_gpuvm_bo_list_del_init(vm_bo, evict, lock);
  1577. }
  1578. EXPORT_SYMBOL_GPL(drm_gpuvm_bo_evict);
  1579. static int
  1580. __drm_gpuva_insert(struct drm_gpuvm *gpuvm,
  1581. struct drm_gpuva *va)
  1582. {
  1583. struct rb_node *node;
  1584. struct list_head *head;
  1585. if (drm_gpuva_it_iter_first(&gpuvm->rb.tree,
  1586. GPUVA_START(va),
  1587. GPUVA_LAST(va)))
  1588. return -EEXIST;
  1589. va->vm = gpuvm;
  1590. drm_gpuva_it_insert(va, &gpuvm->rb.tree);
  1591. node = rb_prev(&va->rb.node);
  1592. if (node)
  1593. head = &(to_drm_gpuva(node))->rb.entry;
  1594. else
  1595. head = &gpuvm->rb.list;
  1596. list_add(&va->rb.entry, head);
  1597. return 0;
  1598. }
  1599. /**
  1600. * drm_gpuva_insert() - insert a &drm_gpuva
  1601. * @gpuvm: the &drm_gpuvm to insert the &drm_gpuva in
  1602. * @va: the &drm_gpuva to insert
  1603. *
  1604. * Insert a &drm_gpuva with a given address and range into a
  1605. * &drm_gpuvm.
  1606. *
  1607. * It is safe to use this function using the safe versions of iterating the GPU
  1608. * VA space, such as drm_gpuvm_for_each_va_safe() and
  1609. * drm_gpuvm_for_each_va_range_safe().
  1610. *
  1611. * Returns: 0 on success, negative error code on failure.
  1612. */
  1613. int
  1614. drm_gpuva_insert(struct drm_gpuvm *gpuvm,
  1615. struct drm_gpuva *va)
  1616. {
  1617. u64 addr = va->va.addr;
  1618. u64 range = va->va.range;
  1619. int ret;
  1620. if (unlikely(!drm_gpuvm_range_valid(gpuvm, addr, range)))
  1621. return -EINVAL;
  1622. ret = __drm_gpuva_insert(gpuvm, va);
  1623. if (likely(!ret))
  1624. /* Take a reference of the GPUVM for the successfully inserted
  1625. * drm_gpuva. We can't take the reference in
  1626. * __drm_gpuva_insert() itself, since we don't want to increse
  1627. * the reference count for the GPUVM's kernel_alloc_node.
  1628. */
  1629. drm_gpuvm_get(gpuvm);
  1630. return ret;
  1631. }
  1632. EXPORT_SYMBOL_GPL(drm_gpuva_insert);
  1633. static void
  1634. __drm_gpuva_remove(struct drm_gpuva *va)
  1635. {
  1636. drm_gpuva_it_remove(va, &va->vm->rb.tree);
  1637. list_del_init(&va->rb.entry);
  1638. }
  1639. /**
  1640. * drm_gpuva_remove() - remove a &drm_gpuva
  1641. * @va: the &drm_gpuva to remove
  1642. *
  1643. * This removes the given &va from the underlaying tree.
  1644. *
  1645. * It is safe to use this function using the safe versions of iterating the GPU
  1646. * VA space, such as drm_gpuvm_for_each_va_safe() and
  1647. * drm_gpuvm_for_each_va_range_safe().
  1648. */
  1649. void
  1650. drm_gpuva_remove(struct drm_gpuva *va)
  1651. {
  1652. struct drm_gpuvm *gpuvm = va->vm;
  1653. if (unlikely(va == &gpuvm->kernel_alloc_node)) {
  1654. drm_WARN(gpuvm->drm, 1,
  1655. "Can't destroy kernel reserved node.\n");
  1656. return;
  1657. }
  1658. __drm_gpuva_remove(va);
  1659. drm_gpuvm_put(va->vm);
  1660. }
  1661. EXPORT_SYMBOL_GPL(drm_gpuva_remove);
  1662. /**
  1663. * drm_gpuva_link() - link a &drm_gpuva
  1664. * @va: the &drm_gpuva to link
  1665. * @vm_bo: the &drm_gpuvm_bo to add the &drm_gpuva to
  1666. *
  1667. * This adds the given &va to the GPU VA list of the &drm_gpuvm_bo and the
  1668. * &drm_gpuvm_bo to the &drm_gem_object it is associated with.
  1669. *
  1670. * For every &drm_gpuva entry added to the &drm_gpuvm_bo an additional
  1671. * reference of the latter is taken.
  1672. *
  1673. * This function expects the caller to protect the GEM's GPUVA list against
  1674. * concurrent access using either the GEMs dma_resv lock or a driver specific
  1675. * lock set through drm_gem_gpuva_set_lock().
  1676. */
  1677. void
  1678. drm_gpuva_link(struct drm_gpuva *va, struct drm_gpuvm_bo *vm_bo)
  1679. {
  1680. struct drm_gem_object *obj = va->gem.obj;
  1681. struct drm_gpuvm *gpuvm = va->vm;
  1682. if (unlikely(!obj))
  1683. return;
  1684. drm_WARN_ON(gpuvm->drm, obj != vm_bo->obj);
  1685. va->vm_bo = drm_gpuvm_bo_get(vm_bo);
  1686. drm_gem_gpuva_assert_lock_held(obj);
  1687. list_add_tail(&va->gem.entry, &vm_bo->list.gpuva);
  1688. }
  1689. EXPORT_SYMBOL_GPL(drm_gpuva_link);
  1690. /**
  1691. * drm_gpuva_unlink() - unlink a &drm_gpuva
  1692. * @va: the &drm_gpuva to unlink
  1693. *
  1694. * This removes the given &va from the GPU VA list of the &drm_gem_object it is
  1695. * associated with.
  1696. *
  1697. * This removes the given &va from the GPU VA list of the &drm_gpuvm_bo and
  1698. * the &drm_gpuvm_bo from the &drm_gem_object it is associated with in case
  1699. * this call unlinks the last &drm_gpuva from the &drm_gpuvm_bo.
  1700. *
  1701. * For every &drm_gpuva entry removed from the &drm_gpuvm_bo a reference of
  1702. * the latter is dropped.
  1703. *
  1704. * This function expects the caller to protect the GEM's GPUVA list against
  1705. * concurrent access using either the GEMs dma_resv lock or a driver specific
  1706. * lock set through drm_gem_gpuva_set_lock().
  1707. */
  1708. void
  1709. drm_gpuva_unlink(struct drm_gpuva *va)
  1710. {
  1711. struct drm_gem_object *obj = va->gem.obj;
  1712. struct drm_gpuvm_bo *vm_bo = va->vm_bo;
  1713. if (unlikely(!obj))
  1714. return;
  1715. drm_gem_gpuva_assert_lock_held(obj);
  1716. list_del_init(&va->gem.entry);
  1717. va->vm_bo = NULL;
  1718. drm_gpuvm_bo_put(vm_bo);
  1719. }
  1720. EXPORT_SYMBOL_GPL(drm_gpuva_unlink);
  1721. /**
  1722. * drm_gpuva_find_first() - find the first &drm_gpuva in the given range
  1723. * @gpuvm: the &drm_gpuvm to search in
  1724. * @addr: the &drm_gpuvas address
  1725. * @range: the &drm_gpuvas range
  1726. *
  1727. * Returns: the first &drm_gpuva within the given range
  1728. */
  1729. struct drm_gpuva *
  1730. drm_gpuva_find_first(struct drm_gpuvm *gpuvm,
  1731. u64 addr, u64 range)
  1732. {
  1733. u64 last = addr + range - 1;
  1734. return drm_gpuva_it_iter_first(&gpuvm->rb.tree, addr, last);
  1735. }
  1736. EXPORT_SYMBOL_GPL(drm_gpuva_find_first);
  1737. /**
  1738. * drm_gpuva_find() - find a &drm_gpuva
  1739. * @gpuvm: the &drm_gpuvm to search in
  1740. * @addr: the &drm_gpuvas address
  1741. * @range: the &drm_gpuvas range
  1742. *
  1743. * Returns: the &drm_gpuva at a given &addr and with a given &range
  1744. */
  1745. struct drm_gpuva *
  1746. drm_gpuva_find(struct drm_gpuvm *gpuvm,
  1747. u64 addr, u64 range)
  1748. {
  1749. struct drm_gpuva *va;
  1750. va = drm_gpuva_find_first(gpuvm, addr, range);
  1751. if (!va)
  1752. goto out;
  1753. if (va->va.addr != addr ||
  1754. va->va.range != range)
  1755. goto out;
  1756. return va;
  1757. out:
  1758. return NULL;
  1759. }
  1760. EXPORT_SYMBOL_GPL(drm_gpuva_find);
  1761. /**
  1762. * drm_gpuva_find_prev() - find the &drm_gpuva before the given address
  1763. * @gpuvm: the &drm_gpuvm to search in
  1764. * @start: the given GPU VA's start address
  1765. *
  1766. * Find the adjacent &drm_gpuva before the GPU VA with given &start address.
  1767. *
  1768. * Note that if there is any free space between the GPU VA mappings no mapping
  1769. * is returned.
  1770. *
  1771. * Returns: a pointer to the found &drm_gpuva or NULL if none was found
  1772. */
  1773. struct drm_gpuva *
  1774. drm_gpuva_find_prev(struct drm_gpuvm *gpuvm, u64 start)
  1775. {
  1776. if (!drm_gpuvm_range_valid(gpuvm, start - 1, 1))
  1777. return NULL;
  1778. return drm_gpuva_it_iter_first(&gpuvm->rb.tree, start - 1, start);
  1779. }
  1780. EXPORT_SYMBOL_GPL(drm_gpuva_find_prev);
  1781. /**
  1782. * drm_gpuva_find_next() - find the &drm_gpuva after the given address
  1783. * @gpuvm: the &drm_gpuvm to search in
  1784. * @end: the given GPU VA's end address
  1785. *
  1786. * Find the adjacent &drm_gpuva after the GPU VA with given &end address.
  1787. *
  1788. * Note that if there is any free space between the GPU VA mappings no mapping
  1789. * is returned.
  1790. *
  1791. * Returns: a pointer to the found &drm_gpuva or NULL if none was found
  1792. */
  1793. struct drm_gpuva *
  1794. drm_gpuva_find_next(struct drm_gpuvm *gpuvm, u64 end)
  1795. {
  1796. if (!drm_gpuvm_range_valid(gpuvm, end, 1))
  1797. return NULL;
  1798. return drm_gpuva_it_iter_first(&gpuvm->rb.tree, end, end + 1);
  1799. }
  1800. EXPORT_SYMBOL_GPL(drm_gpuva_find_next);
  1801. /**
  1802. * drm_gpuvm_interval_empty() - indicate whether a given interval of the VA space
  1803. * is empty
  1804. * @gpuvm: the &drm_gpuvm to check the range for
  1805. * @addr: the start address of the range
  1806. * @range: the range of the interval
  1807. *
  1808. * Returns: true if the interval is empty, false otherwise
  1809. */
  1810. bool
  1811. drm_gpuvm_interval_empty(struct drm_gpuvm *gpuvm, u64 addr, u64 range)
  1812. {
  1813. return !drm_gpuva_find_first(gpuvm, addr, range);
  1814. }
  1815. EXPORT_SYMBOL_GPL(drm_gpuvm_interval_empty);
  1816. /**
  1817. * drm_gpuva_map() - helper to insert a &drm_gpuva according to a
  1818. * &drm_gpuva_op_map
  1819. * @gpuvm: the &drm_gpuvm
  1820. * @va: the &drm_gpuva to insert
  1821. * @op: the &drm_gpuva_op_map to initialize @va with
  1822. *
  1823. * Initializes the @va from the @op and inserts it into the given @gpuvm.
  1824. */
  1825. void
  1826. drm_gpuva_map(struct drm_gpuvm *gpuvm,
  1827. struct drm_gpuva *va,
  1828. struct drm_gpuva_op_map *op)
  1829. {
  1830. drm_gpuva_init_from_op(va, op);
  1831. drm_gpuva_insert(gpuvm, va);
  1832. }
  1833. EXPORT_SYMBOL_GPL(drm_gpuva_map);
  1834. /**
  1835. * drm_gpuva_remap() - helper to remap a &drm_gpuva according to a
  1836. * &drm_gpuva_op_remap
  1837. * @prev: the &drm_gpuva to remap when keeping the start of a mapping
  1838. * @next: the &drm_gpuva to remap when keeping the end of a mapping
  1839. * @op: the &drm_gpuva_op_remap to initialize @prev and @next with
  1840. *
  1841. * Removes the currently mapped &drm_gpuva and remaps it using @prev and/or
  1842. * @next.
  1843. */
  1844. void
  1845. drm_gpuva_remap(struct drm_gpuva *prev,
  1846. struct drm_gpuva *next,
  1847. struct drm_gpuva_op_remap *op)
  1848. {
  1849. struct drm_gpuva *va = op->unmap->va;
  1850. struct drm_gpuvm *gpuvm = va->vm;
  1851. drm_gpuva_remove(va);
  1852. if (op->prev) {
  1853. drm_gpuva_init_from_op(prev, op->prev);
  1854. drm_gpuva_insert(gpuvm, prev);
  1855. }
  1856. if (op->next) {
  1857. drm_gpuva_init_from_op(next, op->next);
  1858. drm_gpuva_insert(gpuvm, next);
  1859. }
  1860. }
  1861. EXPORT_SYMBOL_GPL(drm_gpuva_remap);
  1862. /**
  1863. * drm_gpuva_unmap() - helper to remove a &drm_gpuva according to a
  1864. * &drm_gpuva_op_unmap
  1865. * @op: the &drm_gpuva_op_unmap specifying the &drm_gpuva to remove
  1866. *
  1867. * Removes the &drm_gpuva associated with the &drm_gpuva_op_unmap.
  1868. */
  1869. void
  1870. drm_gpuva_unmap(struct drm_gpuva_op_unmap *op)
  1871. {
  1872. drm_gpuva_remove(op->va);
  1873. }
  1874. EXPORT_SYMBOL_GPL(drm_gpuva_unmap);
  1875. static int
  1876. op_map_cb(const struct drm_gpuvm_ops *fn, void *priv,
  1877. u64 addr, u64 range,
  1878. struct drm_gem_object *obj, u64 offset)
  1879. {
  1880. struct drm_gpuva_op op = {};
  1881. op.op = DRM_GPUVA_OP_MAP;
  1882. op.map.va.addr = addr;
  1883. op.map.va.range = range;
  1884. op.map.gem.obj = obj;
  1885. op.map.gem.offset = offset;
  1886. return fn->sm_step_map(&op, priv);
  1887. }
  1888. static int
  1889. op_remap_cb(const struct drm_gpuvm_ops *fn, void *priv,
  1890. struct drm_gpuva_op_map *prev,
  1891. struct drm_gpuva_op_map *next,
  1892. struct drm_gpuva_op_unmap *unmap)
  1893. {
  1894. struct drm_gpuva_op op = {};
  1895. struct drm_gpuva_op_remap *r;
  1896. op.op = DRM_GPUVA_OP_REMAP;
  1897. r = &op.remap;
  1898. r->prev = prev;
  1899. r->next = next;
  1900. r->unmap = unmap;
  1901. return fn->sm_step_remap(&op, priv);
  1902. }
  1903. static int
  1904. op_unmap_cb(const struct drm_gpuvm_ops *fn, void *priv,
  1905. struct drm_gpuva *va, bool merge)
  1906. {
  1907. struct drm_gpuva_op op = {};
  1908. op.op = DRM_GPUVA_OP_UNMAP;
  1909. op.unmap.va = va;
  1910. op.unmap.keep = merge;
  1911. return fn->sm_step_unmap(&op, priv);
  1912. }
  1913. static int
  1914. __drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm,
  1915. const struct drm_gpuvm_ops *ops, void *priv,
  1916. u64 req_addr, u64 req_range,
  1917. struct drm_gem_object *req_obj, u64 req_offset)
  1918. {
  1919. struct drm_gpuva *va, *next;
  1920. u64 req_end = req_addr + req_range;
  1921. int ret;
  1922. if (unlikely(!drm_gpuvm_range_valid(gpuvm, req_addr, req_range)))
  1923. return -EINVAL;
  1924. drm_gpuvm_for_each_va_range_safe(va, next, gpuvm, req_addr, req_end) {
  1925. struct drm_gem_object *obj = va->gem.obj;
  1926. u64 offset = va->gem.offset;
  1927. u64 addr = va->va.addr;
  1928. u64 range = va->va.range;
  1929. u64 end = addr + range;
  1930. bool merge = !!va->gem.obj;
  1931. if (addr == req_addr) {
  1932. merge &= obj == req_obj &&
  1933. offset == req_offset;
  1934. if (end == req_end) {
  1935. ret = op_unmap_cb(ops, priv, va, merge);
  1936. if (ret)
  1937. return ret;
  1938. break;
  1939. }
  1940. if (end < req_end) {
  1941. ret = op_unmap_cb(ops, priv, va, merge);
  1942. if (ret)
  1943. return ret;
  1944. continue;
  1945. }
  1946. if (end > req_end) {
  1947. struct drm_gpuva_op_map n = {
  1948. .va.addr = req_end,
  1949. .va.range = range - req_range,
  1950. .gem.obj = obj,
  1951. .gem.offset = offset + req_range,
  1952. };
  1953. struct drm_gpuva_op_unmap u = {
  1954. .va = va,
  1955. .keep = merge,
  1956. };
  1957. ret = op_remap_cb(ops, priv, NULL, &n, &u);
  1958. if (ret)
  1959. return ret;
  1960. break;
  1961. }
  1962. } else if (addr < req_addr) {
  1963. u64 ls_range = req_addr - addr;
  1964. struct drm_gpuva_op_map p = {
  1965. .va.addr = addr,
  1966. .va.range = ls_range,
  1967. .gem.obj = obj,
  1968. .gem.offset = offset,
  1969. };
  1970. struct drm_gpuva_op_unmap u = { .va = va };
  1971. merge &= obj == req_obj &&
  1972. offset + ls_range == req_offset;
  1973. u.keep = merge;
  1974. if (end == req_end) {
  1975. ret = op_remap_cb(ops, priv, &p, NULL, &u);
  1976. if (ret)
  1977. return ret;
  1978. break;
  1979. }
  1980. if (end < req_end) {
  1981. ret = op_remap_cb(ops, priv, &p, NULL, &u);
  1982. if (ret)
  1983. return ret;
  1984. continue;
  1985. }
  1986. if (end > req_end) {
  1987. struct drm_gpuva_op_map n = {
  1988. .va.addr = req_end,
  1989. .va.range = end - req_end,
  1990. .gem.obj = obj,
  1991. .gem.offset = offset + ls_range +
  1992. req_range,
  1993. };
  1994. ret = op_remap_cb(ops, priv, &p, &n, &u);
  1995. if (ret)
  1996. return ret;
  1997. break;
  1998. }
  1999. } else if (addr > req_addr) {
  2000. merge &= obj == req_obj &&
  2001. offset == req_offset +
  2002. (addr - req_addr);
  2003. if (end == req_end) {
  2004. ret = op_unmap_cb(ops, priv, va, merge);
  2005. if (ret)
  2006. return ret;
  2007. break;
  2008. }
  2009. if (end < req_end) {
  2010. ret = op_unmap_cb(ops, priv, va, merge);
  2011. if (ret)
  2012. return ret;
  2013. continue;
  2014. }
  2015. if (end > req_end) {
  2016. struct drm_gpuva_op_map n = {
  2017. .va.addr = req_end,
  2018. .va.range = end - req_end,
  2019. .gem.obj = obj,
  2020. .gem.offset = offset + req_end - addr,
  2021. };
  2022. struct drm_gpuva_op_unmap u = {
  2023. .va = va,
  2024. .keep = merge,
  2025. };
  2026. ret = op_remap_cb(ops, priv, NULL, &n, &u);
  2027. if (ret)
  2028. return ret;
  2029. break;
  2030. }
  2031. }
  2032. }
  2033. return op_map_cb(ops, priv,
  2034. req_addr, req_range,
  2035. req_obj, req_offset);
  2036. }
  2037. static int
  2038. __drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm,
  2039. const struct drm_gpuvm_ops *ops, void *priv,
  2040. u64 req_addr, u64 req_range)
  2041. {
  2042. struct drm_gpuva *va, *next;
  2043. u64 req_end = req_addr + req_range;
  2044. int ret;
  2045. if (unlikely(!drm_gpuvm_range_valid(gpuvm, req_addr, req_range)))
  2046. return -EINVAL;
  2047. drm_gpuvm_for_each_va_range_safe(va, next, gpuvm, req_addr, req_end) {
  2048. struct drm_gpuva_op_map prev = {}, next = {};
  2049. bool prev_split = false, next_split = false;
  2050. struct drm_gem_object *obj = va->gem.obj;
  2051. u64 offset = va->gem.offset;
  2052. u64 addr = va->va.addr;
  2053. u64 range = va->va.range;
  2054. u64 end = addr + range;
  2055. if (addr < req_addr) {
  2056. prev.va.addr = addr;
  2057. prev.va.range = req_addr - addr;
  2058. prev.gem.obj = obj;
  2059. prev.gem.offset = offset;
  2060. prev_split = true;
  2061. }
  2062. if (end > req_end) {
  2063. next.va.addr = req_end;
  2064. next.va.range = end - req_end;
  2065. next.gem.obj = obj;
  2066. next.gem.offset = offset + (req_end - addr);
  2067. next_split = true;
  2068. }
  2069. if (prev_split || next_split) {
  2070. struct drm_gpuva_op_unmap unmap = { .va = va };
  2071. ret = op_remap_cb(ops, priv,
  2072. prev_split ? &prev : NULL,
  2073. next_split ? &next : NULL,
  2074. &unmap);
  2075. if (ret)
  2076. return ret;
  2077. } else {
  2078. ret = op_unmap_cb(ops, priv, va, false);
  2079. if (ret)
  2080. return ret;
  2081. }
  2082. }
  2083. return 0;
  2084. }
  2085. /**
  2086. * drm_gpuvm_sm_map() - creates the &drm_gpuva_op split/merge steps
  2087. * @gpuvm: the &drm_gpuvm representing the GPU VA space
  2088. * @req_addr: the start address of the new mapping
  2089. * @req_range: the range of the new mapping
  2090. * @req_obj: the &drm_gem_object to map
  2091. * @req_offset: the offset within the &drm_gem_object
  2092. * @priv: pointer to a driver private data structure
  2093. *
  2094. * This function iterates the given range of the GPU VA space. It utilizes the
  2095. * &drm_gpuvm_ops to call back into the driver providing the split and merge
  2096. * steps.
  2097. *
  2098. * Drivers may use these callbacks to update the GPU VA space right away within
  2099. * the callback. In case the driver decides to copy and store the operations for
  2100. * later processing neither this function nor &drm_gpuvm_sm_unmap is allowed to
  2101. * be called before the &drm_gpuvm's view of the GPU VA space was
  2102. * updated with the previous set of operations. To update the
  2103. * &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(),
  2104. * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
  2105. * used.
  2106. *
  2107. * A sequence of callbacks can contain map, unmap and remap operations, but
  2108. * the sequence of callbacks might also be empty if no operation is required,
  2109. * e.g. if the requested mapping already exists in the exact same way.
  2110. *
  2111. * There can be an arbitrary amount of unmap operations, a maximum of two remap
  2112. * operations and a single map operation. The latter one represents the original
  2113. * map operation requested by the caller.
  2114. *
  2115. * Returns: 0 on success or a negative error code
  2116. */
  2117. int
  2118. drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm, void *priv,
  2119. u64 req_addr, u64 req_range,
  2120. struct drm_gem_object *req_obj, u64 req_offset)
  2121. {
  2122. const struct drm_gpuvm_ops *ops = gpuvm->ops;
  2123. if (unlikely(!(ops && ops->sm_step_map &&
  2124. ops->sm_step_remap &&
  2125. ops->sm_step_unmap)))
  2126. return -EINVAL;
  2127. return __drm_gpuvm_sm_map(gpuvm, ops, priv,
  2128. req_addr, req_range,
  2129. req_obj, req_offset);
  2130. }
  2131. EXPORT_SYMBOL_GPL(drm_gpuvm_sm_map);
  2132. /**
  2133. * drm_gpuvm_sm_unmap() - creates the &drm_gpuva_ops to split on unmap
  2134. * @gpuvm: the &drm_gpuvm representing the GPU VA space
  2135. * @priv: pointer to a driver private data structure
  2136. * @req_addr: the start address of the range to unmap
  2137. * @req_range: the range of the mappings to unmap
  2138. *
  2139. * This function iterates the given range of the GPU VA space. It utilizes the
  2140. * &drm_gpuvm_ops to call back into the driver providing the operations to
  2141. * unmap and, if required, split existent mappings.
  2142. *
  2143. * Drivers may use these callbacks to update the GPU VA space right away within
  2144. * the callback. In case the driver decides to copy and store the operations for
  2145. * later processing neither this function nor &drm_gpuvm_sm_map is allowed to be
  2146. * called before the &drm_gpuvm's view of the GPU VA space was updated
  2147. * with the previous set of operations. To update the &drm_gpuvm's view
  2148. * of the GPU VA space drm_gpuva_insert(), drm_gpuva_destroy_locked() and/or
  2149. * drm_gpuva_destroy_unlocked() should be used.
  2150. *
  2151. * A sequence of callbacks can contain unmap and remap operations, depending on
  2152. * whether there are actual overlapping mappings to split.
  2153. *
  2154. * There can be an arbitrary amount of unmap operations and a maximum of two
  2155. * remap operations.
  2156. *
  2157. * Returns: 0 on success or a negative error code
  2158. */
  2159. int
  2160. drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv,
  2161. u64 req_addr, u64 req_range)
  2162. {
  2163. const struct drm_gpuvm_ops *ops = gpuvm->ops;
  2164. if (unlikely(!(ops && ops->sm_step_remap &&
  2165. ops->sm_step_unmap)))
  2166. return -EINVAL;
  2167. return __drm_gpuvm_sm_unmap(gpuvm, ops, priv,
  2168. req_addr, req_range);
  2169. }
  2170. EXPORT_SYMBOL_GPL(drm_gpuvm_sm_unmap);
  2171. static struct drm_gpuva_op *
  2172. gpuva_op_alloc(struct drm_gpuvm *gpuvm)
  2173. {
  2174. const struct drm_gpuvm_ops *fn = gpuvm->ops;
  2175. struct drm_gpuva_op *op;
  2176. if (fn && fn->op_alloc)
  2177. op = fn->op_alloc();
  2178. else
  2179. op = kzalloc(sizeof(*op), GFP_KERNEL);
  2180. if (unlikely(!op))
  2181. return NULL;
  2182. return op;
  2183. }
  2184. static void
  2185. gpuva_op_free(struct drm_gpuvm *gpuvm,
  2186. struct drm_gpuva_op *op)
  2187. {
  2188. const struct drm_gpuvm_ops *fn = gpuvm->ops;
  2189. if (fn && fn->op_free)
  2190. fn->op_free(op);
  2191. else
  2192. kfree(op);
  2193. }
  2194. static int
  2195. drm_gpuva_sm_step(struct drm_gpuva_op *__op,
  2196. void *priv)
  2197. {
  2198. struct {
  2199. struct drm_gpuvm *vm;
  2200. struct drm_gpuva_ops *ops;
  2201. } *args = priv;
  2202. struct drm_gpuvm *gpuvm = args->vm;
  2203. struct drm_gpuva_ops *ops = args->ops;
  2204. struct drm_gpuva_op *op;
  2205. op = gpuva_op_alloc(gpuvm);
  2206. if (unlikely(!op))
  2207. goto err;
  2208. memcpy(op, __op, sizeof(*op));
  2209. if (op->op == DRM_GPUVA_OP_REMAP) {
  2210. struct drm_gpuva_op_remap *__r = &__op->remap;
  2211. struct drm_gpuva_op_remap *r = &op->remap;
  2212. r->unmap = kmemdup(__r->unmap, sizeof(*r->unmap),
  2213. GFP_KERNEL);
  2214. if (unlikely(!r->unmap))
  2215. goto err_free_op;
  2216. if (__r->prev) {
  2217. r->prev = kmemdup(__r->prev, sizeof(*r->prev),
  2218. GFP_KERNEL);
  2219. if (unlikely(!r->prev))
  2220. goto err_free_unmap;
  2221. }
  2222. if (__r->next) {
  2223. r->next = kmemdup(__r->next, sizeof(*r->next),
  2224. GFP_KERNEL);
  2225. if (unlikely(!r->next))
  2226. goto err_free_prev;
  2227. }
  2228. }
  2229. list_add_tail(&op->entry, &ops->list);
  2230. return 0;
  2231. err_free_unmap:
  2232. kfree(op->remap.unmap);
  2233. err_free_prev:
  2234. kfree(op->remap.prev);
  2235. err_free_op:
  2236. gpuva_op_free(gpuvm, op);
  2237. err:
  2238. return -ENOMEM;
  2239. }
  2240. static const struct drm_gpuvm_ops gpuvm_list_ops = {
  2241. .sm_step_map = drm_gpuva_sm_step,
  2242. .sm_step_remap = drm_gpuva_sm_step,
  2243. .sm_step_unmap = drm_gpuva_sm_step,
  2244. };
  2245. /**
  2246. * drm_gpuvm_sm_map_ops_create() - creates the &drm_gpuva_ops to split and merge
  2247. * @gpuvm: the &drm_gpuvm representing the GPU VA space
  2248. * @req_addr: the start address of the new mapping
  2249. * @req_range: the range of the new mapping
  2250. * @req_obj: the &drm_gem_object to map
  2251. * @req_offset: the offset within the &drm_gem_object
  2252. *
  2253. * This function creates a list of operations to perform splitting and merging
  2254. * of existent mapping(s) with the newly requested one.
  2255. *
  2256. * The list can be iterated with &drm_gpuva_for_each_op and must be processed
  2257. * in the given order. It can contain map, unmap and remap operations, but it
  2258. * also can be empty if no operation is required, e.g. if the requested mapping
  2259. * already exists is the exact same way.
  2260. *
  2261. * There can be an arbitrary amount of unmap operations, a maximum of two remap
  2262. * operations and a single map operation. The latter one represents the original
  2263. * map operation requested by the caller.
  2264. *
  2265. * Note that before calling this function again with another mapping request it
  2266. * is necessary to update the &drm_gpuvm's view of the GPU VA space. The
  2267. * previously obtained operations must be either processed or abandoned. To
  2268. * update the &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(),
  2269. * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
  2270. * used.
  2271. *
  2272. * After the caller finished processing the returned &drm_gpuva_ops, they must
  2273. * be freed with &drm_gpuva_ops_free.
  2274. *
  2275. * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
  2276. */
  2277. struct drm_gpuva_ops *
  2278. drm_gpuvm_sm_map_ops_create(struct drm_gpuvm *gpuvm,
  2279. u64 req_addr, u64 req_range,
  2280. struct drm_gem_object *req_obj, u64 req_offset)
  2281. {
  2282. struct drm_gpuva_ops *ops;
  2283. struct {
  2284. struct drm_gpuvm *vm;
  2285. struct drm_gpuva_ops *ops;
  2286. } args;
  2287. int ret;
  2288. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  2289. if (unlikely(!ops))
  2290. return ERR_PTR(-ENOMEM);
  2291. INIT_LIST_HEAD(&ops->list);
  2292. args.vm = gpuvm;
  2293. args.ops = ops;
  2294. ret = __drm_gpuvm_sm_map(gpuvm, &gpuvm_list_ops, &args,
  2295. req_addr, req_range,
  2296. req_obj, req_offset);
  2297. if (ret)
  2298. goto err_free_ops;
  2299. return ops;
  2300. err_free_ops:
  2301. drm_gpuva_ops_free(gpuvm, ops);
  2302. return ERR_PTR(ret);
  2303. }
  2304. EXPORT_SYMBOL_GPL(drm_gpuvm_sm_map_ops_create);
  2305. /**
  2306. * drm_gpuvm_sm_unmap_ops_create() - creates the &drm_gpuva_ops to split on
  2307. * unmap
  2308. * @gpuvm: the &drm_gpuvm representing the GPU VA space
  2309. * @req_addr: the start address of the range to unmap
  2310. * @req_range: the range of the mappings to unmap
  2311. *
  2312. * This function creates a list of operations to perform unmapping and, if
  2313. * required, splitting of the mappings overlapping the unmap range.
  2314. *
  2315. * The list can be iterated with &drm_gpuva_for_each_op and must be processed
  2316. * in the given order. It can contain unmap and remap operations, depending on
  2317. * whether there are actual overlapping mappings to split.
  2318. *
  2319. * There can be an arbitrary amount of unmap operations and a maximum of two
  2320. * remap operations.
  2321. *
  2322. * Note that before calling this function again with another range to unmap it
  2323. * is necessary to update the &drm_gpuvm's view of the GPU VA space. The
  2324. * previously obtained operations must be processed or abandoned. To update the
  2325. * &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(),
  2326. * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
  2327. * used.
  2328. *
  2329. * After the caller finished processing the returned &drm_gpuva_ops, they must
  2330. * be freed with &drm_gpuva_ops_free.
  2331. *
  2332. * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
  2333. */
  2334. struct drm_gpuva_ops *
  2335. drm_gpuvm_sm_unmap_ops_create(struct drm_gpuvm *gpuvm,
  2336. u64 req_addr, u64 req_range)
  2337. {
  2338. struct drm_gpuva_ops *ops;
  2339. struct {
  2340. struct drm_gpuvm *vm;
  2341. struct drm_gpuva_ops *ops;
  2342. } args;
  2343. int ret;
  2344. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  2345. if (unlikely(!ops))
  2346. return ERR_PTR(-ENOMEM);
  2347. INIT_LIST_HEAD(&ops->list);
  2348. args.vm = gpuvm;
  2349. args.ops = ops;
  2350. ret = __drm_gpuvm_sm_unmap(gpuvm, &gpuvm_list_ops, &args,
  2351. req_addr, req_range);
  2352. if (ret)
  2353. goto err_free_ops;
  2354. return ops;
  2355. err_free_ops:
  2356. drm_gpuva_ops_free(gpuvm, ops);
  2357. return ERR_PTR(ret);
  2358. }
  2359. EXPORT_SYMBOL_GPL(drm_gpuvm_sm_unmap_ops_create);
  2360. /**
  2361. * drm_gpuvm_prefetch_ops_create() - creates the &drm_gpuva_ops to prefetch
  2362. * @gpuvm: the &drm_gpuvm representing the GPU VA space
  2363. * @addr: the start address of the range to prefetch
  2364. * @range: the range of the mappings to prefetch
  2365. *
  2366. * This function creates a list of operations to perform prefetching.
  2367. *
  2368. * The list can be iterated with &drm_gpuva_for_each_op and must be processed
  2369. * in the given order. It can contain prefetch operations.
  2370. *
  2371. * There can be an arbitrary amount of prefetch operations.
  2372. *
  2373. * After the caller finished processing the returned &drm_gpuva_ops, they must
  2374. * be freed with &drm_gpuva_ops_free.
  2375. *
  2376. * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
  2377. */
  2378. struct drm_gpuva_ops *
  2379. drm_gpuvm_prefetch_ops_create(struct drm_gpuvm *gpuvm,
  2380. u64 addr, u64 range)
  2381. {
  2382. struct drm_gpuva_ops *ops;
  2383. struct drm_gpuva_op *op;
  2384. struct drm_gpuva *va;
  2385. u64 end = addr + range;
  2386. int ret;
  2387. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  2388. if (!ops)
  2389. return ERR_PTR(-ENOMEM);
  2390. INIT_LIST_HEAD(&ops->list);
  2391. drm_gpuvm_for_each_va_range(va, gpuvm, addr, end) {
  2392. op = gpuva_op_alloc(gpuvm);
  2393. if (!op) {
  2394. ret = -ENOMEM;
  2395. goto err_free_ops;
  2396. }
  2397. op->op = DRM_GPUVA_OP_PREFETCH;
  2398. op->prefetch.va = va;
  2399. list_add_tail(&op->entry, &ops->list);
  2400. }
  2401. return ops;
  2402. err_free_ops:
  2403. drm_gpuva_ops_free(gpuvm, ops);
  2404. return ERR_PTR(ret);
  2405. }
  2406. EXPORT_SYMBOL_GPL(drm_gpuvm_prefetch_ops_create);
  2407. /**
  2408. * drm_gpuvm_bo_unmap_ops_create() - creates the &drm_gpuva_ops to unmap a GEM
  2409. * @vm_bo: the &drm_gpuvm_bo abstraction
  2410. *
  2411. * This function creates a list of operations to perform unmapping for every
  2412. * GPUVA attached to a GEM.
  2413. *
  2414. * The list can be iterated with &drm_gpuva_for_each_op and consists out of an
  2415. * arbitrary amount of unmap operations.
  2416. *
  2417. * After the caller finished processing the returned &drm_gpuva_ops, they must
  2418. * be freed with &drm_gpuva_ops_free.
  2419. *
  2420. * It is the callers responsibility to protect the GEMs GPUVA list against
  2421. * concurrent access using the GEMs dma_resv lock.
  2422. *
  2423. * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
  2424. */
  2425. struct drm_gpuva_ops *
  2426. drm_gpuvm_bo_unmap_ops_create(struct drm_gpuvm_bo *vm_bo)
  2427. {
  2428. struct drm_gpuva_ops *ops;
  2429. struct drm_gpuva_op *op;
  2430. struct drm_gpuva *va;
  2431. int ret;
  2432. drm_gem_gpuva_assert_lock_held(vm_bo->obj);
  2433. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  2434. if (!ops)
  2435. return ERR_PTR(-ENOMEM);
  2436. INIT_LIST_HEAD(&ops->list);
  2437. drm_gpuvm_bo_for_each_va(va, vm_bo) {
  2438. op = gpuva_op_alloc(vm_bo->vm);
  2439. if (!op) {
  2440. ret = -ENOMEM;
  2441. goto err_free_ops;
  2442. }
  2443. op->op = DRM_GPUVA_OP_UNMAP;
  2444. op->unmap.va = va;
  2445. list_add_tail(&op->entry, &ops->list);
  2446. }
  2447. return ops;
  2448. err_free_ops:
  2449. drm_gpuva_ops_free(vm_bo->vm, ops);
  2450. return ERR_PTR(ret);
  2451. }
  2452. EXPORT_SYMBOL_GPL(drm_gpuvm_bo_unmap_ops_create);
  2453. /**
  2454. * drm_gpuva_ops_free() - free the given &drm_gpuva_ops
  2455. * @gpuvm: the &drm_gpuvm the ops were created for
  2456. * @ops: the &drm_gpuva_ops to free
  2457. *
  2458. * Frees the given &drm_gpuva_ops structure including all the ops associated
  2459. * with it.
  2460. */
  2461. void
  2462. drm_gpuva_ops_free(struct drm_gpuvm *gpuvm,
  2463. struct drm_gpuva_ops *ops)
  2464. {
  2465. struct drm_gpuva_op *op, *next;
  2466. drm_gpuva_for_each_op_safe(op, next, ops) {
  2467. list_del(&op->entry);
  2468. if (op->op == DRM_GPUVA_OP_REMAP) {
  2469. kfree(op->remap.prev);
  2470. kfree(op->remap.next);
  2471. kfree(op->remap.unmap);
  2472. }
  2473. gpuva_op_free(gpuvm, op);
  2474. }
  2475. kfree(ops);
  2476. }
  2477. EXPORT_SYMBOL_GPL(drm_gpuva_ops_free);
  2478. MODULE_DESCRIPTION("DRM GPUVM");
  2479. MODULE_LICENSE("GPL");