drm_gpuvm.c 94 KB

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