i915_vma.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197
  1. /*
  2. * Copyright © 2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * 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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include <linux/sched/mm.h>
  25. #include <linux/dma-fence-array.h>
  26. #include <drm/drm_gem.h>
  27. #include "display/intel_frontbuffer.h"
  28. #include "gem/i915_gem_lmem.h"
  29. #include "gem/i915_gem_tiling.h"
  30. #include "gt/intel_engine.h"
  31. #include "gt/intel_engine_heartbeat.h"
  32. #include "gt/intel_gt.h"
  33. #include "gt/intel_gt_requests.h"
  34. #include "i915_drv.h"
  35. #include "i915_gem_evict.h"
  36. #include "i915_sw_fence_work.h"
  37. #include "i915_trace.h"
  38. #include "i915_vma.h"
  39. #include "i915_vma_resource.h"
  40. static inline void assert_vma_held_evict(const struct i915_vma *vma)
  41. {
  42. /*
  43. * We may be forced to unbind when the vm is dead, to clean it up.
  44. * This is the only exception to the requirement of the object lock
  45. * being held.
  46. */
  47. if (kref_read(&vma->vm->ref))
  48. assert_object_held_shared(vma->obj);
  49. }
  50. static struct kmem_cache *slab_vmas;
  51. static struct i915_vma *i915_vma_alloc(void)
  52. {
  53. return kmem_cache_zalloc(slab_vmas, GFP_KERNEL);
  54. }
  55. static void i915_vma_free(struct i915_vma *vma)
  56. {
  57. return kmem_cache_free(slab_vmas, vma);
  58. }
  59. #if IS_ENABLED(CONFIG_DRM_I915_ERRLOG_GEM) && IS_ENABLED(CONFIG_DRM_DEBUG_MM)
  60. #include <linux/stackdepot.h>
  61. static void vma_print_allocator(struct i915_vma *vma, const char *reason)
  62. {
  63. char buf[512];
  64. if (!vma->node.stack) {
  65. DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: unknown owner\n",
  66. vma->node.start, vma->node.size, reason);
  67. return;
  68. }
  69. stack_depot_snprint(vma->node.stack, buf, sizeof(buf), 0);
  70. DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: inserted at %s\n",
  71. vma->node.start, vma->node.size, reason, buf);
  72. }
  73. #else
  74. static void vma_print_allocator(struct i915_vma *vma, const char *reason)
  75. {
  76. }
  77. #endif
  78. static inline struct i915_vma *active_to_vma(struct i915_active *ref)
  79. {
  80. return container_of(ref, typeof(struct i915_vma), active);
  81. }
  82. static int __i915_vma_active(struct i915_active *ref)
  83. {
  84. return i915_vma_tryget(active_to_vma(ref)) ? 0 : -ENOENT;
  85. }
  86. static void __i915_vma_retire(struct i915_active *ref)
  87. {
  88. i915_vma_put(active_to_vma(ref));
  89. }
  90. static struct i915_vma *
  91. vma_create(struct drm_i915_gem_object *obj,
  92. struct i915_address_space *vm,
  93. const struct i915_gtt_view *view)
  94. {
  95. struct i915_vma *pos = ERR_PTR(-E2BIG);
  96. struct i915_vma *vma;
  97. struct rb_node *rb, **p;
  98. int err;
  99. /* The aliasing_ppgtt should never be used directly! */
  100. GEM_BUG_ON(vm == &vm->gt->ggtt->alias->vm);
  101. vma = i915_vma_alloc();
  102. if (vma == NULL)
  103. return ERR_PTR(-ENOMEM);
  104. vma->ops = &vm->vma_ops;
  105. vma->obj = obj;
  106. vma->size = obj->base.size;
  107. vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
  108. i915_active_init(&vma->active, __i915_vma_active, __i915_vma_retire, 0);
  109. /* Declare ourselves safe for use inside shrinkers */
  110. if (IS_ENABLED(CONFIG_LOCKDEP)) {
  111. fs_reclaim_acquire(GFP_KERNEL);
  112. might_lock(&vma->active.mutex);
  113. fs_reclaim_release(GFP_KERNEL);
  114. }
  115. INIT_LIST_HEAD(&vma->closed_link);
  116. INIT_LIST_HEAD(&vma->obj_link);
  117. RB_CLEAR_NODE(&vma->obj_node);
  118. if (view && view->type != I915_GTT_VIEW_NORMAL) {
  119. vma->gtt_view = *view;
  120. if (view->type == I915_GTT_VIEW_PARTIAL) {
  121. GEM_BUG_ON(range_overflows_t(u64,
  122. view->partial.offset,
  123. view->partial.size,
  124. obj->base.size >> PAGE_SHIFT));
  125. vma->size = view->partial.size;
  126. vma->size <<= PAGE_SHIFT;
  127. GEM_BUG_ON(vma->size > obj->base.size);
  128. } else if (view->type == I915_GTT_VIEW_ROTATED) {
  129. vma->size = intel_rotation_info_size(&view->rotated);
  130. vma->size <<= PAGE_SHIFT;
  131. } else if (view->type == I915_GTT_VIEW_REMAPPED) {
  132. vma->size = intel_remapped_info_size(&view->remapped);
  133. vma->size <<= PAGE_SHIFT;
  134. }
  135. }
  136. if (unlikely(vma->size > vm->total))
  137. goto err_vma;
  138. GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
  139. err = mutex_lock_interruptible(&vm->mutex);
  140. if (err) {
  141. pos = ERR_PTR(err);
  142. goto err_vma;
  143. }
  144. vma->vm = vm;
  145. list_add_tail(&vma->vm_link, &vm->unbound_list);
  146. spin_lock(&obj->vma.lock);
  147. if (i915_is_ggtt(vm)) {
  148. if (unlikely(overflows_type(vma->size, u32)))
  149. goto err_unlock;
  150. vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
  151. i915_gem_object_get_tiling(obj),
  152. i915_gem_object_get_stride(obj));
  153. if (unlikely(vma->fence_size < vma->size || /* overflow */
  154. vma->fence_size > vm->total))
  155. goto err_unlock;
  156. GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
  157. vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
  158. i915_gem_object_get_tiling(obj),
  159. i915_gem_object_get_stride(obj));
  160. GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
  161. __set_bit(I915_VMA_GGTT_BIT, __i915_vma_flags(vma));
  162. }
  163. rb = NULL;
  164. p = &obj->vma.tree.rb_node;
  165. while (*p) {
  166. long cmp;
  167. rb = *p;
  168. pos = rb_entry(rb, struct i915_vma, obj_node);
  169. /*
  170. * If the view already exists in the tree, another thread
  171. * already created a matching vma, so return the older instance
  172. * and dispose of ours.
  173. */
  174. cmp = i915_vma_compare(pos, vm, view);
  175. if (cmp < 0)
  176. p = &rb->rb_right;
  177. else if (cmp > 0)
  178. p = &rb->rb_left;
  179. else
  180. goto err_unlock;
  181. }
  182. rb_link_node(&vma->obj_node, rb, p);
  183. rb_insert_color(&vma->obj_node, &obj->vma.tree);
  184. if (i915_vma_is_ggtt(vma))
  185. /*
  186. * We put the GGTT vma at the start of the vma-list, followed
  187. * by the ppGGTT vma. This allows us to break early when
  188. * iterating over only the GGTT vma for an object, see
  189. * for_each_ggtt_vma()
  190. */
  191. list_add(&vma->obj_link, &obj->vma.list);
  192. else
  193. list_add_tail(&vma->obj_link, &obj->vma.list);
  194. spin_unlock(&obj->vma.lock);
  195. mutex_unlock(&vm->mutex);
  196. return vma;
  197. err_unlock:
  198. spin_unlock(&obj->vma.lock);
  199. list_del_init(&vma->vm_link);
  200. mutex_unlock(&vm->mutex);
  201. err_vma:
  202. i915_vma_free(vma);
  203. return pos;
  204. }
  205. static struct i915_vma *
  206. i915_vma_lookup(struct drm_i915_gem_object *obj,
  207. struct i915_address_space *vm,
  208. const struct i915_gtt_view *view)
  209. {
  210. struct rb_node *rb;
  211. rb = obj->vma.tree.rb_node;
  212. while (rb) {
  213. struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
  214. long cmp;
  215. cmp = i915_vma_compare(vma, vm, view);
  216. if (cmp == 0)
  217. return vma;
  218. if (cmp < 0)
  219. rb = rb->rb_right;
  220. else
  221. rb = rb->rb_left;
  222. }
  223. return NULL;
  224. }
  225. /**
  226. * i915_vma_instance - return the singleton instance of the VMA
  227. * @obj: parent &struct drm_i915_gem_object to be mapped
  228. * @vm: address space in which the mapping is located
  229. * @view: additional mapping requirements
  230. *
  231. * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
  232. * the same @view characteristics. If a match is not found, one is created.
  233. * Once created, the VMA is kept until either the object is freed, or the
  234. * address space is closed.
  235. *
  236. * Returns the vma, or an error pointer.
  237. */
  238. struct i915_vma *
  239. i915_vma_instance(struct drm_i915_gem_object *obj,
  240. struct i915_address_space *vm,
  241. const struct i915_gtt_view *view)
  242. {
  243. struct i915_vma *vma;
  244. GEM_BUG_ON(view && !i915_is_ggtt_or_dpt(vm));
  245. GEM_BUG_ON(!kref_read(&vm->ref));
  246. spin_lock(&obj->vma.lock);
  247. vma = i915_vma_lookup(obj, vm, view);
  248. spin_unlock(&obj->vma.lock);
  249. /* vma_create() will resolve the race if another creates the vma */
  250. if (unlikely(!vma))
  251. vma = vma_create(obj, vm, view);
  252. GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
  253. return vma;
  254. }
  255. struct i915_vma_work {
  256. struct dma_fence_work base;
  257. struct i915_address_space *vm;
  258. struct i915_vm_pt_stash stash;
  259. struct i915_vma_resource *vma_res;
  260. struct drm_i915_gem_object *obj;
  261. struct i915_sw_dma_fence_cb cb;
  262. enum i915_cache_level cache_level;
  263. unsigned int flags;
  264. };
  265. static void __vma_bind(struct dma_fence_work *work)
  266. {
  267. struct i915_vma_work *vw = container_of(work, typeof(*vw), base);
  268. struct i915_vma_resource *vma_res = vw->vma_res;
  269. /*
  270. * We are about the bind the object, which must mean we have already
  271. * signaled the work to potentially clear/move the pages underneath. If
  272. * something went wrong at that stage then the object should have
  273. * unknown_state set, in which case we need to skip the bind.
  274. */
  275. if (i915_gem_object_has_unknown_state(vw->obj))
  276. return;
  277. vma_res->ops->bind_vma(vma_res->vm, &vw->stash,
  278. vma_res, vw->cache_level, vw->flags);
  279. }
  280. static void __vma_release(struct dma_fence_work *work)
  281. {
  282. struct i915_vma_work *vw = container_of(work, typeof(*vw), base);
  283. if (vw->obj)
  284. i915_gem_object_put(vw->obj);
  285. i915_vm_free_pt_stash(vw->vm, &vw->stash);
  286. if (vw->vma_res)
  287. i915_vma_resource_put(vw->vma_res);
  288. }
  289. static const struct dma_fence_work_ops bind_ops = {
  290. .name = "bind",
  291. .work = __vma_bind,
  292. .release = __vma_release,
  293. };
  294. struct i915_vma_work *i915_vma_work(void)
  295. {
  296. struct i915_vma_work *vw;
  297. vw = kzalloc(sizeof(*vw), GFP_KERNEL);
  298. if (!vw)
  299. return NULL;
  300. dma_fence_work_init(&vw->base, &bind_ops);
  301. vw->base.dma.error = -EAGAIN; /* disable the worker by default */
  302. return vw;
  303. }
  304. int i915_vma_wait_for_bind(struct i915_vma *vma)
  305. {
  306. int err = 0;
  307. if (rcu_access_pointer(vma->active.excl.fence)) {
  308. struct dma_fence *fence;
  309. rcu_read_lock();
  310. fence = dma_fence_get_rcu_safe(&vma->active.excl.fence);
  311. rcu_read_unlock();
  312. if (fence) {
  313. err = dma_fence_wait(fence, true);
  314. dma_fence_put(fence);
  315. }
  316. }
  317. return err;
  318. }
  319. #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
  320. static int i915_vma_verify_bind_complete(struct i915_vma *vma)
  321. {
  322. struct dma_fence *fence = i915_active_fence_get(&vma->active.excl);
  323. int err;
  324. if (!fence)
  325. return 0;
  326. if (dma_fence_is_signaled(fence))
  327. err = fence->error;
  328. else
  329. err = -EBUSY;
  330. dma_fence_put(fence);
  331. return err;
  332. }
  333. #else
  334. #define i915_vma_verify_bind_complete(_vma) 0
  335. #endif
  336. I915_SELFTEST_EXPORT void
  337. i915_vma_resource_init_from_vma(struct i915_vma_resource *vma_res,
  338. struct i915_vma *vma)
  339. {
  340. struct drm_i915_gem_object *obj = vma->obj;
  341. i915_vma_resource_init(vma_res, vma->vm, vma->pages, &vma->page_sizes,
  342. obj->mm.rsgt, i915_gem_object_is_readonly(obj),
  343. i915_gem_object_is_lmem(obj), obj->mm.region,
  344. vma->ops, vma->private, vma->node.start,
  345. vma->node.size, vma->size);
  346. }
  347. /**
  348. * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
  349. * @vma: VMA to map
  350. * @cache_level: mapping cache level
  351. * @flags: flags like global or local mapping
  352. * @work: preallocated worker for allocating and binding the PTE
  353. * @vma_res: pointer to a preallocated vma resource. The resource is either
  354. * consumed or freed.
  355. *
  356. * DMA addresses are taken from the scatter-gather table of this object (or of
  357. * this VMA in case of non-default GGTT views) and PTE entries set up.
  358. * Note that DMA addresses are also the only part of the SG table we care about.
  359. */
  360. int i915_vma_bind(struct i915_vma *vma,
  361. enum i915_cache_level cache_level,
  362. u32 flags,
  363. struct i915_vma_work *work,
  364. struct i915_vma_resource *vma_res)
  365. {
  366. u32 bind_flags;
  367. u32 vma_flags;
  368. int ret;
  369. lockdep_assert_held(&vma->vm->mutex);
  370. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  371. GEM_BUG_ON(vma->size > vma->node.size);
  372. if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start,
  373. vma->node.size,
  374. vma->vm->total))) {
  375. i915_vma_resource_free(vma_res);
  376. return -ENODEV;
  377. }
  378. if (GEM_DEBUG_WARN_ON(!flags)) {
  379. i915_vma_resource_free(vma_res);
  380. return -EINVAL;
  381. }
  382. bind_flags = flags;
  383. bind_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
  384. vma_flags = atomic_read(&vma->flags);
  385. vma_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
  386. bind_flags &= ~vma_flags;
  387. if (bind_flags == 0) {
  388. i915_vma_resource_free(vma_res);
  389. return 0;
  390. }
  391. GEM_BUG_ON(!atomic_read(&vma->pages_count));
  392. /* Wait for or await async unbinds touching our range */
  393. if (work && bind_flags & vma->vm->bind_async_flags)
  394. ret = i915_vma_resource_bind_dep_await(vma->vm,
  395. &work->base.chain,
  396. vma->node.start,
  397. vma->node.size,
  398. true,
  399. GFP_NOWAIT |
  400. __GFP_RETRY_MAYFAIL |
  401. __GFP_NOWARN);
  402. else
  403. ret = i915_vma_resource_bind_dep_sync(vma->vm, vma->node.start,
  404. vma->node.size, true);
  405. if (ret) {
  406. i915_vma_resource_free(vma_res);
  407. return ret;
  408. }
  409. if (vma->resource || !vma_res) {
  410. /* Rebinding with an additional I915_VMA_*_BIND */
  411. GEM_WARN_ON(!vma_flags);
  412. i915_vma_resource_free(vma_res);
  413. } else {
  414. i915_vma_resource_init_from_vma(vma_res, vma);
  415. vma->resource = vma_res;
  416. }
  417. trace_i915_vma_bind(vma, bind_flags);
  418. if (work && bind_flags & vma->vm->bind_async_flags) {
  419. struct dma_fence *prev;
  420. work->vma_res = i915_vma_resource_get(vma->resource);
  421. work->cache_level = cache_level;
  422. work->flags = bind_flags;
  423. /*
  424. * Note we only want to chain up to the migration fence on
  425. * the pages (not the object itself). As we don't track that,
  426. * yet, we have to use the exclusive fence instead.
  427. *
  428. * Also note that we do not want to track the async vma as
  429. * part of the obj->resv->excl_fence as it only affects
  430. * execution and not content or object's backing store lifetime.
  431. */
  432. prev = i915_active_set_exclusive(&vma->active, &work->base.dma);
  433. if (prev) {
  434. __i915_sw_fence_await_dma_fence(&work->base.chain,
  435. prev,
  436. &work->cb);
  437. dma_fence_put(prev);
  438. }
  439. work->base.dma.error = 0; /* enable the queue_work() */
  440. work->obj = i915_gem_object_get(vma->obj);
  441. } else {
  442. ret = i915_gem_object_wait_moving_fence(vma->obj, true);
  443. if (ret) {
  444. i915_vma_resource_free(vma->resource);
  445. vma->resource = NULL;
  446. return ret;
  447. }
  448. vma->ops->bind_vma(vma->vm, NULL, vma->resource, cache_level,
  449. bind_flags);
  450. }
  451. atomic_or(bind_flags, &vma->flags);
  452. return 0;
  453. }
  454. void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
  455. {
  456. void __iomem *ptr;
  457. int err;
  458. if (WARN_ON_ONCE(vma->obj->flags & I915_BO_ALLOC_GPU_ONLY))
  459. return IOMEM_ERR_PTR(-EINVAL);
  460. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  461. GEM_BUG_ON(!i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND));
  462. GEM_BUG_ON(i915_vma_verify_bind_complete(vma));
  463. ptr = READ_ONCE(vma->iomap);
  464. if (ptr == NULL) {
  465. /*
  466. * TODO: consider just using i915_gem_object_pin_map() for lmem
  467. * instead, which already supports mapping non-contiguous chunks
  468. * of pages, that way we can also drop the
  469. * I915_BO_ALLOC_CONTIGUOUS when allocating the object.
  470. */
  471. if (i915_gem_object_is_lmem(vma->obj)) {
  472. ptr = i915_gem_object_lmem_io_map(vma->obj, 0,
  473. vma->obj->base.size);
  474. } else if (i915_vma_is_map_and_fenceable(vma)) {
  475. ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
  476. vma->node.start,
  477. vma->node.size);
  478. } else {
  479. ptr = (void __iomem *)
  480. i915_gem_object_pin_map(vma->obj, I915_MAP_WC);
  481. if (IS_ERR(ptr)) {
  482. err = PTR_ERR(ptr);
  483. goto err;
  484. }
  485. ptr = page_pack_bits(ptr, 1);
  486. }
  487. if (ptr == NULL) {
  488. err = -ENOMEM;
  489. goto err;
  490. }
  491. if (unlikely(cmpxchg(&vma->iomap, NULL, ptr))) {
  492. if (page_unmask_bits(ptr))
  493. __i915_gem_object_release_map(vma->obj);
  494. else
  495. io_mapping_unmap(ptr);
  496. ptr = vma->iomap;
  497. }
  498. }
  499. __i915_vma_pin(vma);
  500. err = i915_vma_pin_fence(vma);
  501. if (err)
  502. goto err_unpin;
  503. i915_vma_set_ggtt_write(vma);
  504. /* NB Access through the GTT requires the device to be awake. */
  505. return page_mask_bits(ptr);
  506. err_unpin:
  507. __i915_vma_unpin(vma);
  508. err:
  509. return IOMEM_ERR_PTR(err);
  510. }
  511. void i915_vma_flush_writes(struct i915_vma *vma)
  512. {
  513. if (i915_vma_unset_ggtt_write(vma))
  514. intel_gt_flush_ggtt_writes(vma->vm->gt);
  515. }
  516. void i915_vma_unpin_iomap(struct i915_vma *vma)
  517. {
  518. GEM_BUG_ON(vma->iomap == NULL);
  519. /* XXX We keep the mapping until __i915_vma_unbind()/evict() */
  520. i915_vma_flush_writes(vma);
  521. i915_vma_unpin_fence(vma);
  522. i915_vma_unpin(vma);
  523. }
  524. void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags)
  525. {
  526. struct i915_vma *vma;
  527. struct drm_i915_gem_object *obj;
  528. vma = fetch_and_zero(p_vma);
  529. if (!vma)
  530. return;
  531. obj = vma->obj;
  532. GEM_BUG_ON(!obj);
  533. i915_vma_unpin(vma);
  534. if (flags & I915_VMA_RELEASE_MAP)
  535. i915_gem_object_unpin_map(obj);
  536. i915_gem_object_put(obj);
  537. }
  538. bool i915_vma_misplaced(const struct i915_vma *vma,
  539. u64 size, u64 alignment, u64 flags)
  540. {
  541. if (!drm_mm_node_allocated(&vma->node))
  542. return false;
  543. if (test_bit(I915_VMA_ERROR_BIT, __i915_vma_flags(vma)))
  544. return true;
  545. if (vma->node.size < size)
  546. return true;
  547. GEM_BUG_ON(alignment && !is_power_of_2(alignment));
  548. if (alignment && !IS_ALIGNED(vma->node.start, alignment))
  549. return true;
  550. if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
  551. return true;
  552. if (flags & PIN_OFFSET_BIAS &&
  553. vma->node.start < (flags & PIN_OFFSET_MASK))
  554. return true;
  555. if (flags & PIN_OFFSET_FIXED &&
  556. vma->node.start != (flags & PIN_OFFSET_MASK))
  557. return true;
  558. return false;
  559. }
  560. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
  561. {
  562. bool mappable, fenceable;
  563. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  564. GEM_BUG_ON(!vma->fence_size);
  565. fenceable = (vma->node.size >= vma->fence_size &&
  566. IS_ALIGNED(vma->node.start, vma->fence_alignment));
  567. mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
  568. if (mappable && fenceable)
  569. set_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
  570. else
  571. clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
  572. }
  573. bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long color)
  574. {
  575. struct drm_mm_node *node = &vma->node;
  576. struct drm_mm_node *other;
  577. /*
  578. * On some machines we have to be careful when putting differing types
  579. * of snoopable memory together to avoid the prefetcher crossing memory
  580. * domains and dying. During vm initialisation, we decide whether or not
  581. * these constraints apply and set the drm_mm.color_adjust
  582. * appropriately.
  583. */
  584. if (!i915_vm_has_cache_coloring(vma->vm))
  585. return true;
  586. /* Only valid to be called on an already inserted vma */
  587. GEM_BUG_ON(!drm_mm_node_allocated(node));
  588. GEM_BUG_ON(list_empty(&node->node_list));
  589. other = list_prev_entry(node, node_list);
  590. if (i915_node_color_differs(other, color) &&
  591. !drm_mm_hole_follows(other))
  592. return false;
  593. other = list_next_entry(node, node_list);
  594. if (i915_node_color_differs(other, color) &&
  595. !drm_mm_hole_follows(node))
  596. return false;
  597. return true;
  598. }
  599. /**
  600. * i915_vma_insert - finds a slot for the vma in its address space
  601. * @vma: the vma
  602. * @size: requested size in bytes (can be larger than the VMA)
  603. * @alignment: required alignment
  604. * @flags: mask of PIN_* flags to use
  605. *
  606. * First we try to allocate some free space that meets the requirements for
  607. * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
  608. * preferrably the oldest idle entry to make room for the new VMA.
  609. *
  610. * Returns:
  611. * 0 on success, negative error code otherwise.
  612. */
  613. static int
  614. i915_vma_insert(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
  615. u64 size, u64 alignment, u64 flags)
  616. {
  617. unsigned long color;
  618. u64 start, end;
  619. int ret;
  620. GEM_BUG_ON(i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
  621. GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
  622. size = max(size, vma->size);
  623. alignment = max(alignment, vma->display_alignment);
  624. if (flags & PIN_MAPPABLE) {
  625. size = max_t(typeof(size), size, vma->fence_size);
  626. alignment = max_t(typeof(alignment),
  627. alignment, vma->fence_alignment);
  628. }
  629. GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
  630. GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
  631. GEM_BUG_ON(!is_power_of_2(alignment));
  632. start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  633. GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
  634. end = vma->vm->total;
  635. if (flags & PIN_MAPPABLE)
  636. end = min_t(u64, end, i915_vm_to_ggtt(vma->vm)->mappable_end);
  637. if (flags & PIN_ZONE_4G)
  638. end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
  639. GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
  640. alignment = max(alignment, i915_vm_obj_min_alignment(vma->vm, vma->obj));
  641. /*
  642. * for compact-pt we round up the reservation to prevent
  643. * any smaller pages being used within the same PDE
  644. */
  645. if (NEEDS_COMPACT_PT(vma->vm->i915))
  646. size = round_up(size, alignment);
  647. /* If binding the object/GGTT view requires more space than the entire
  648. * aperture has, reject it early before evicting everything in a vain
  649. * attempt to find space.
  650. */
  651. if (size > end) {
  652. DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n",
  653. size, flags & PIN_MAPPABLE ? "mappable" : "total",
  654. end);
  655. return -ENOSPC;
  656. }
  657. color = 0;
  658. if (i915_vm_has_cache_coloring(vma->vm))
  659. color = vma->obj->cache_level;
  660. if (flags & PIN_OFFSET_FIXED) {
  661. u64 offset = flags & PIN_OFFSET_MASK;
  662. if (!IS_ALIGNED(offset, alignment) ||
  663. range_overflows(offset, size, end))
  664. return -EINVAL;
  665. ret = i915_gem_gtt_reserve(vma->vm, ww, &vma->node,
  666. size, offset, color,
  667. flags);
  668. if (ret)
  669. return ret;
  670. } else {
  671. /*
  672. * We only support huge gtt pages through the 48b PPGTT,
  673. * however we also don't want to force any alignment for
  674. * objects which need to be tightly packed into the low 32bits.
  675. *
  676. * Note that we assume that GGTT are limited to 4GiB for the
  677. * forseeable future. See also i915_ggtt_offset().
  678. */
  679. if (upper_32_bits(end - 1) &&
  680. vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
  681. /*
  682. * We can't mix 64K and 4K PTEs in the same page-table
  683. * (2M block), and so to avoid the ugliness and
  684. * complexity of coloring we opt for just aligning 64K
  685. * objects to 2M.
  686. */
  687. u64 page_alignment =
  688. rounddown_pow_of_two(vma->page_sizes.sg |
  689. I915_GTT_PAGE_SIZE_2M);
  690. /*
  691. * Check we don't expand for the limited Global GTT
  692. * (mappable aperture is even more precious!). This
  693. * also checks that we exclude the aliasing-ppgtt.
  694. */
  695. GEM_BUG_ON(i915_vma_is_ggtt(vma));
  696. alignment = max(alignment, page_alignment);
  697. if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
  698. size = round_up(size, I915_GTT_PAGE_SIZE_2M);
  699. }
  700. ret = i915_gem_gtt_insert(vma->vm, ww, &vma->node,
  701. size, alignment, color,
  702. start, end, flags);
  703. if (ret)
  704. return ret;
  705. GEM_BUG_ON(vma->node.start < start);
  706. GEM_BUG_ON(vma->node.start + vma->node.size > end);
  707. }
  708. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  709. GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, color));
  710. list_move_tail(&vma->vm_link, &vma->vm->bound_list);
  711. return 0;
  712. }
  713. static void
  714. i915_vma_detach(struct i915_vma *vma)
  715. {
  716. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  717. GEM_BUG_ON(i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
  718. /*
  719. * And finally now the object is completely decoupled from this
  720. * vma, we can drop its hold on the backing storage and allow
  721. * it to be reaped by the shrinker.
  722. */
  723. list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
  724. }
  725. static bool try_qad_pin(struct i915_vma *vma, unsigned int flags)
  726. {
  727. unsigned int bound;
  728. bound = atomic_read(&vma->flags);
  729. if (flags & PIN_VALIDATE) {
  730. flags &= I915_VMA_BIND_MASK;
  731. return (flags & bound) == flags;
  732. }
  733. /* with the lock mandatory for unbind, we don't race here */
  734. flags &= I915_VMA_BIND_MASK;
  735. do {
  736. if (unlikely(flags & ~bound))
  737. return false;
  738. if (unlikely(bound & (I915_VMA_OVERFLOW | I915_VMA_ERROR)))
  739. return false;
  740. GEM_BUG_ON(((bound + 1) & I915_VMA_PIN_MASK) == 0);
  741. } while (!atomic_try_cmpxchg(&vma->flags, &bound, bound + 1));
  742. return true;
  743. }
  744. static struct scatterlist *
  745. rotate_pages(struct drm_i915_gem_object *obj, unsigned int offset,
  746. unsigned int width, unsigned int height,
  747. unsigned int src_stride, unsigned int dst_stride,
  748. struct sg_table *st, struct scatterlist *sg)
  749. {
  750. unsigned int column, row;
  751. unsigned int src_idx;
  752. for (column = 0; column < width; column++) {
  753. unsigned int left;
  754. src_idx = src_stride * (height - 1) + column + offset;
  755. for (row = 0; row < height; row++) {
  756. st->nents++;
  757. /*
  758. * We don't need the pages, but need to initialize
  759. * the entries so the sg list can be happily traversed.
  760. * The only thing we need are DMA addresses.
  761. */
  762. sg_set_page(sg, NULL, I915_GTT_PAGE_SIZE, 0);
  763. sg_dma_address(sg) =
  764. i915_gem_object_get_dma_address(obj, src_idx);
  765. sg_dma_len(sg) = I915_GTT_PAGE_SIZE;
  766. sg = sg_next(sg);
  767. src_idx -= src_stride;
  768. }
  769. left = (dst_stride - height) * I915_GTT_PAGE_SIZE;
  770. if (!left)
  771. continue;
  772. st->nents++;
  773. /*
  774. * The DE ignores the PTEs for the padding tiles, the sg entry
  775. * here is just a conenience to indicate how many padding PTEs
  776. * to insert at this spot.
  777. */
  778. sg_set_page(sg, NULL, left, 0);
  779. sg_dma_address(sg) = 0;
  780. sg_dma_len(sg) = left;
  781. sg = sg_next(sg);
  782. }
  783. return sg;
  784. }
  785. static noinline struct sg_table *
  786. intel_rotate_pages(struct intel_rotation_info *rot_info,
  787. struct drm_i915_gem_object *obj)
  788. {
  789. unsigned int size = intel_rotation_info_size(rot_info);
  790. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  791. struct sg_table *st;
  792. struct scatterlist *sg;
  793. int ret = -ENOMEM;
  794. int i;
  795. /* Allocate target SG list. */
  796. st = kmalloc(sizeof(*st), GFP_KERNEL);
  797. if (!st)
  798. goto err_st_alloc;
  799. ret = sg_alloc_table(st, size, GFP_KERNEL);
  800. if (ret)
  801. goto err_sg_alloc;
  802. st->nents = 0;
  803. sg = st->sgl;
  804. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  805. sg = rotate_pages(obj, rot_info->plane[i].offset,
  806. rot_info->plane[i].width, rot_info->plane[i].height,
  807. rot_info->plane[i].src_stride,
  808. rot_info->plane[i].dst_stride,
  809. st, sg);
  810. return st;
  811. err_sg_alloc:
  812. kfree(st);
  813. err_st_alloc:
  814. drm_dbg(&i915->drm, "Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
  815. obj->base.size, rot_info->plane[0].width,
  816. rot_info->plane[0].height, size);
  817. return ERR_PTR(ret);
  818. }
  819. static struct scatterlist *
  820. add_padding_pages(unsigned int count,
  821. struct sg_table *st, struct scatterlist *sg)
  822. {
  823. st->nents++;
  824. /*
  825. * The DE ignores the PTEs for the padding tiles, the sg entry
  826. * here is just a convenience to indicate how many padding PTEs
  827. * to insert at this spot.
  828. */
  829. sg_set_page(sg, NULL, count * I915_GTT_PAGE_SIZE, 0);
  830. sg_dma_address(sg) = 0;
  831. sg_dma_len(sg) = count * I915_GTT_PAGE_SIZE;
  832. sg = sg_next(sg);
  833. return sg;
  834. }
  835. static struct scatterlist *
  836. remap_tiled_color_plane_pages(struct drm_i915_gem_object *obj,
  837. unsigned int offset, unsigned int alignment_pad,
  838. unsigned int width, unsigned int height,
  839. unsigned int src_stride, unsigned int dst_stride,
  840. struct sg_table *st, struct scatterlist *sg,
  841. unsigned int *gtt_offset)
  842. {
  843. unsigned int row;
  844. if (!width || !height)
  845. return sg;
  846. if (alignment_pad)
  847. sg = add_padding_pages(alignment_pad, st, sg);
  848. for (row = 0; row < height; row++) {
  849. unsigned int left = width * I915_GTT_PAGE_SIZE;
  850. while (left) {
  851. dma_addr_t addr;
  852. unsigned int length;
  853. /*
  854. * We don't need the pages, but need to initialize
  855. * the entries so the sg list can be happily traversed.
  856. * The only thing we need are DMA addresses.
  857. */
  858. addr = i915_gem_object_get_dma_address_len(obj, offset, &length);
  859. length = min(left, length);
  860. st->nents++;
  861. sg_set_page(sg, NULL, length, 0);
  862. sg_dma_address(sg) = addr;
  863. sg_dma_len(sg) = length;
  864. sg = sg_next(sg);
  865. offset += length / I915_GTT_PAGE_SIZE;
  866. left -= length;
  867. }
  868. offset += src_stride - width;
  869. left = (dst_stride - width) * I915_GTT_PAGE_SIZE;
  870. if (!left)
  871. continue;
  872. sg = add_padding_pages(left >> PAGE_SHIFT, st, sg);
  873. }
  874. *gtt_offset += alignment_pad + dst_stride * height;
  875. return sg;
  876. }
  877. static struct scatterlist *
  878. remap_contiguous_pages(struct drm_i915_gem_object *obj,
  879. unsigned int obj_offset,
  880. unsigned int count,
  881. struct sg_table *st, struct scatterlist *sg)
  882. {
  883. struct scatterlist *iter;
  884. unsigned int offset;
  885. iter = i915_gem_object_get_sg_dma(obj, obj_offset, &offset);
  886. GEM_BUG_ON(!iter);
  887. do {
  888. unsigned int len;
  889. len = min(sg_dma_len(iter) - (offset << PAGE_SHIFT),
  890. count << PAGE_SHIFT);
  891. sg_set_page(sg, NULL, len, 0);
  892. sg_dma_address(sg) =
  893. sg_dma_address(iter) + (offset << PAGE_SHIFT);
  894. sg_dma_len(sg) = len;
  895. st->nents++;
  896. count -= len >> PAGE_SHIFT;
  897. if (count == 0)
  898. return sg;
  899. sg = __sg_next(sg);
  900. iter = __sg_next(iter);
  901. offset = 0;
  902. } while (1);
  903. }
  904. static struct scatterlist *
  905. remap_linear_color_plane_pages(struct drm_i915_gem_object *obj,
  906. unsigned int obj_offset, unsigned int alignment_pad,
  907. unsigned int size,
  908. struct sg_table *st, struct scatterlist *sg,
  909. unsigned int *gtt_offset)
  910. {
  911. if (!size)
  912. return sg;
  913. if (alignment_pad)
  914. sg = add_padding_pages(alignment_pad, st, sg);
  915. sg = remap_contiguous_pages(obj, obj_offset, size, st, sg);
  916. sg = sg_next(sg);
  917. *gtt_offset += alignment_pad + size;
  918. return sg;
  919. }
  920. static struct scatterlist *
  921. remap_color_plane_pages(const struct intel_remapped_info *rem_info,
  922. struct drm_i915_gem_object *obj,
  923. int color_plane,
  924. struct sg_table *st, struct scatterlist *sg,
  925. unsigned int *gtt_offset)
  926. {
  927. unsigned int alignment_pad = 0;
  928. if (rem_info->plane_alignment)
  929. alignment_pad = ALIGN(*gtt_offset, rem_info->plane_alignment) - *gtt_offset;
  930. if (rem_info->plane[color_plane].linear)
  931. sg = remap_linear_color_plane_pages(obj,
  932. rem_info->plane[color_plane].offset,
  933. alignment_pad,
  934. rem_info->plane[color_plane].size,
  935. st, sg,
  936. gtt_offset);
  937. else
  938. sg = remap_tiled_color_plane_pages(obj,
  939. rem_info->plane[color_plane].offset,
  940. alignment_pad,
  941. rem_info->plane[color_plane].width,
  942. rem_info->plane[color_plane].height,
  943. rem_info->plane[color_plane].src_stride,
  944. rem_info->plane[color_plane].dst_stride,
  945. st, sg,
  946. gtt_offset);
  947. return sg;
  948. }
  949. static noinline struct sg_table *
  950. intel_remap_pages(struct intel_remapped_info *rem_info,
  951. struct drm_i915_gem_object *obj)
  952. {
  953. unsigned int size = intel_remapped_info_size(rem_info);
  954. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  955. struct sg_table *st;
  956. struct scatterlist *sg;
  957. unsigned int gtt_offset = 0;
  958. int ret = -ENOMEM;
  959. int i;
  960. /* Allocate target SG list. */
  961. st = kmalloc(sizeof(*st), GFP_KERNEL);
  962. if (!st)
  963. goto err_st_alloc;
  964. ret = sg_alloc_table(st, size, GFP_KERNEL);
  965. if (ret)
  966. goto err_sg_alloc;
  967. st->nents = 0;
  968. sg = st->sgl;
  969. for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
  970. sg = remap_color_plane_pages(rem_info, obj, i, st, sg, &gtt_offset);
  971. i915_sg_trim(st);
  972. return st;
  973. err_sg_alloc:
  974. kfree(st);
  975. err_st_alloc:
  976. drm_dbg(&i915->drm, "Failed to create remapped mapping for object size %zu! (%ux%u tiles, %u pages)\n",
  977. obj->base.size, rem_info->plane[0].width,
  978. rem_info->plane[0].height, size);
  979. return ERR_PTR(ret);
  980. }
  981. static noinline struct sg_table *
  982. intel_partial_pages(const struct i915_gtt_view *view,
  983. struct drm_i915_gem_object *obj)
  984. {
  985. struct sg_table *st;
  986. struct scatterlist *sg;
  987. unsigned int count = view->partial.size;
  988. int ret = -ENOMEM;
  989. st = kmalloc(sizeof(*st), GFP_KERNEL);
  990. if (!st)
  991. goto err_st_alloc;
  992. ret = sg_alloc_table(st, count, GFP_KERNEL);
  993. if (ret)
  994. goto err_sg_alloc;
  995. st->nents = 0;
  996. sg = remap_contiguous_pages(obj, view->partial.offset, count, st, st->sgl);
  997. sg_mark_end(sg);
  998. i915_sg_trim(st); /* Drop any unused tail entries. */
  999. return st;
  1000. err_sg_alloc:
  1001. kfree(st);
  1002. err_st_alloc:
  1003. return ERR_PTR(ret);
  1004. }
  1005. static int
  1006. __i915_vma_get_pages(struct i915_vma *vma)
  1007. {
  1008. struct sg_table *pages;
  1009. /*
  1010. * The vma->pages are only valid within the lifespan of the borrowed
  1011. * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
  1012. * must be the vma->pages. A simple rule is that vma->pages must only
  1013. * be accessed when the obj->mm.pages are pinned.
  1014. */
  1015. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
  1016. switch (vma->gtt_view.type) {
  1017. default:
  1018. GEM_BUG_ON(vma->gtt_view.type);
  1019. fallthrough;
  1020. case I915_GTT_VIEW_NORMAL:
  1021. pages = vma->obj->mm.pages;
  1022. break;
  1023. case I915_GTT_VIEW_ROTATED:
  1024. pages =
  1025. intel_rotate_pages(&vma->gtt_view.rotated, vma->obj);
  1026. break;
  1027. case I915_GTT_VIEW_REMAPPED:
  1028. pages =
  1029. intel_remap_pages(&vma->gtt_view.remapped, vma->obj);
  1030. break;
  1031. case I915_GTT_VIEW_PARTIAL:
  1032. pages = intel_partial_pages(&vma->gtt_view, vma->obj);
  1033. break;
  1034. }
  1035. if (IS_ERR(pages)) {
  1036. drm_err(&vma->vm->i915->drm,
  1037. "Failed to get pages for VMA view type %u (%ld)!\n",
  1038. vma->gtt_view.type, PTR_ERR(pages));
  1039. return PTR_ERR(pages);
  1040. }
  1041. vma->pages = pages;
  1042. return 0;
  1043. }
  1044. I915_SELFTEST_EXPORT int i915_vma_get_pages(struct i915_vma *vma)
  1045. {
  1046. int err;
  1047. if (atomic_add_unless(&vma->pages_count, 1, 0))
  1048. return 0;
  1049. err = i915_gem_object_pin_pages(vma->obj);
  1050. if (err)
  1051. return err;
  1052. err = __i915_vma_get_pages(vma);
  1053. if (err)
  1054. goto err_unpin;
  1055. vma->page_sizes = vma->obj->mm.page_sizes;
  1056. atomic_inc(&vma->pages_count);
  1057. return 0;
  1058. err_unpin:
  1059. __i915_gem_object_unpin_pages(vma->obj);
  1060. return err;
  1061. }
  1062. void vma_invalidate_tlb(struct i915_address_space *vm, u32 *tlb)
  1063. {
  1064. /*
  1065. * Before we release the pages that were bound by this vma, we
  1066. * must invalidate all the TLBs that may still have a reference
  1067. * back to our physical address. It only needs to be done once,
  1068. * so after updating the PTE to point away from the pages, record
  1069. * the most recent TLB invalidation seqno, and if we have not yet
  1070. * flushed the TLBs upon release, perform a full invalidation.
  1071. */
  1072. WRITE_ONCE(*tlb, intel_gt_next_invalidate_tlb_full(vm->gt));
  1073. }
  1074. static void __vma_put_pages(struct i915_vma *vma, unsigned int count)
  1075. {
  1076. /* We allocate under vma_get_pages, so beware the shrinker */
  1077. GEM_BUG_ON(atomic_read(&vma->pages_count) < count);
  1078. if (atomic_sub_return(count, &vma->pages_count) == 0) {
  1079. if (vma->pages != vma->obj->mm.pages) {
  1080. sg_free_table(vma->pages);
  1081. kfree(vma->pages);
  1082. }
  1083. vma->pages = NULL;
  1084. i915_gem_object_unpin_pages(vma->obj);
  1085. }
  1086. }
  1087. I915_SELFTEST_EXPORT void i915_vma_put_pages(struct i915_vma *vma)
  1088. {
  1089. if (atomic_add_unless(&vma->pages_count, -1, 1))
  1090. return;
  1091. __vma_put_pages(vma, 1);
  1092. }
  1093. static void vma_unbind_pages(struct i915_vma *vma)
  1094. {
  1095. unsigned int count;
  1096. lockdep_assert_held(&vma->vm->mutex);
  1097. /* The upper portion of pages_count is the number of bindings */
  1098. count = atomic_read(&vma->pages_count);
  1099. count >>= I915_VMA_PAGES_BIAS;
  1100. GEM_BUG_ON(!count);
  1101. __vma_put_pages(vma, count | count << I915_VMA_PAGES_BIAS);
  1102. }
  1103. int i915_vma_pin_ww(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
  1104. u64 size, u64 alignment, u64 flags)
  1105. {
  1106. struct i915_vma_work *work = NULL;
  1107. struct dma_fence *moving = NULL;
  1108. struct i915_vma_resource *vma_res = NULL;
  1109. intel_wakeref_t wakeref = 0;
  1110. unsigned int bound;
  1111. int err;
  1112. assert_vma_held(vma);
  1113. GEM_BUG_ON(!ww);
  1114. BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
  1115. BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
  1116. GEM_BUG_ON(!(flags & (PIN_USER | PIN_GLOBAL)));
  1117. /* First try and grab the pin without rebinding the vma */
  1118. if (try_qad_pin(vma, flags))
  1119. return 0;
  1120. err = i915_vma_get_pages(vma);
  1121. if (err)
  1122. return err;
  1123. if (flags & PIN_GLOBAL)
  1124. wakeref = intel_runtime_pm_get(&vma->vm->i915->runtime_pm);
  1125. if (flags & vma->vm->bind_async_flags) {
  1126. /* lock VM */
  1127. err = i915_vm_lock_objects(vma->vm, ww);
  1128. if (err)
  1129. goto err_rpm;
  1130. work = i915_vma_work();
  1131. if (!work) {
  1132. err = -ENOMEM;
  1133. goto err_rpm;
  1134. }
  1135. work->vm = vma->vm;
  1136. err = i915_gem_object_get_moving_fence(vma->obj, &moving);
  1137. if (err)
  1138. goto err_rpm;
  1139. dma_fence_work_chain(&work->base, moving);
  1140. /* Allocate enough page directories to used PTE */
  1141. if (vma->vm->allocate_va_range) {
  1142. err = i915_vm_alloc_pt_stash(vma->vm,
  1143. &work->stash,
  1144. vma->size);
  1145. if (err)
  1146. goto err_fence;
  1147. err = i915_vm_map_pt_stash(vma->vm, &work->stash);
  1148. if (err)
  1149. goto err_fence;
  1150. }
  1151. }
  1152. vma_res = i915_vma_resource_alloc();
  1153. if (IS_ERR(vma_res)) {
  1154. err = PTR_ERR(vma_res);
  1155. goto err_fence;
  1156. }
  1157. /*
  1158. * Differentiate between user/kernel vma inside the aliasing-ppgtt.
  1159. *
  1160. * We conflate the Global GTT with the user's vma when using the
  1161. * aliasing-ppgtt, but it is still vitally important to try and
  1162. * keep the use cases distinct. For example, userptr objects are
  1163. * not allowed inside the Global GTT as that will cause lock
  1164. * inversions when we have to evict them the mmu_notifier callbacks -
  1165. * but they are allowed to be part of the user ppGTT which can never
  1166. * be mapped. As such we try to give the distinct users of the same
  1167. * mutex, distinct lockclasses [equivalent to how we keep i915_ggtt
  1168. * and i915_ppgtt separate].
  1169. *
  1170. * NB this may cause us to mask real lock inversions -- while the
  1171. * code is safe today, lockdep may not be able to spot future
  1172. * transgressions.
  1173. */
  1174. err = mutex_lock_interruptible_nested(&vma->vm->mutex,
  1175. !(flags & PIN_GLOBAL));
  1176. if (err)
  1177. goto err_vma_res;
  1178. /* No more allocations allowed now we hold vm->mutex */
  1179. if (unlikely(i915_vma_is_closed(vma))) {
  1180. err = -ENOENT;
  1181. goto err_unlock;
  1182. }
  1183. bound = atomic_read(&vma->flags);
  1184. if (unlikely(bound & I915_VMA_ERROR)) {
  1185. err = -ENOMEM;
  1186. goto err_unlock;
  1187. }
  1188. if (unlikely(!((bound + 1) & I915_VMA_PIN_MASK))) {
  1189. err = -EAGAIN; /* pins are meant to be fairly temporary */
  1190. goto err_unlock;
  1191. }
  1192. if (unlikely(!(flags & ~bound & I915_VMA_BIND_MASK))) {
  1193. if (!(flags & PIN_VALIDATE))
  1194. __i915_vma_pin(vma);
  1195. goto err_unlock;
  1196. }
  1197. err = i915_active_acquire(&vma->active);
  1198. if (err)
  1199. goto err_unlock;
  1200. if (!(bound & I915_VMA_BIND_MASK)) {
  1201. err = i915_vma_insert(vma, ww, size, alignment, flags);
  1202. if (err)
  1203. goto err_active;
  1204. if (i915_is_ggtt(vma->vm))
  1205. __i915_vma_set_map_and_fenceable(vma);
  1206. }
  1207. GEM_BUG_ON(!vma->pages);
  1208. err = i915_vma_bind(vma,
  1209. vma->obj->cache_level,
  1210. flags, work, vma_res);
  1211. vma_res = NULL;
  1212. if (err)
  1213. goto err_remove;
  1214. /* There should only be at most 2 active bindings (user, global) */
  1215. GEM_BUG_ON(bound + I915_VMA_PAGES_ACTIVE < bound);
  1216. atomic_add(I915_VMA_PAGES_ACTIVE, &vma->pages_count);
  1217. list_move_tail(&vma->vm_link, &vma->vm->bound_list);
  1218. if (!(flags & PIN_VALIDATE)) {
  1219. __i915_vma_pin(vma);
  1220. GEM_BUG_ON(!i915_vma_is_pinned(vma));
  1221. }
  1222. GEM_BUG_ON(!i915_vma_is_bound(vma, flags));
  1223. GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
  1224. err_remove:
  1225. if (!i915_vma_is_bound(vma, I915_VMA_BIND_MASK)) {
  1226. i915_vma_detach(vma);
  1227. drm_mm_remove_node(&vma->node);
  1228. }
  1229. err_active:
  1230. i915_active_release(&vma->active);
  1231. err_unlock:
  1232. mutex_unlock(&vma->vm->mutex);
  1233. err_vma_res:
  1234. i915_vma_resource_free(vma_res);
  1235. err_fence:
  1236. if (work)
  1237. dma_fence_work_commit_imm(&work->base);
  1238. err_rpm:
  1239. if (wakeref)
  1240. intel_runtime_pm_put(&vma->vm->i915->runtime_pm, wakeref);
  1241. if (moving)
  1242. dma_fence_put(moving);
  1243. i915_vma_put_pages(vma);
  1244. return err;
  1245. }
  1246. static void flush_idle_contexts(struct intel_gt *gt)
  1247. {
  1248. struct intel_engine_cs *engine;
  1249. enum intel_engine_id id;
  1250. for_each_engine(engine, gt, id)
  1251. intel_engine_flush_barriers(engine);
  1252. intel_gt_wait_for_idle(gt, MAX_SCHEDULE_TIMEOUT);
  1253. }
  1254. static int __i915_ggtt_pin(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
  1255. u32 align, unsigned int flags)
  1256. {
  1257. struct i915_address_space *vm = vma->vm;
  1258. int err;
  1259. do {
  1260. err = i915_vma_pin_ww(vma, ww, 0, align, flags | PIN_GLOBAL);
  1261. if (err != -ENOSPC) {
  1262. if (!err) {
  1263. err = i915_vma_wait_for_bind(vma);
  1264. if (err)
  1265. i915_vma_unpin(vma);
  1266. }
  1267. return err;
  1268. }
  1269. /* Unlike i915_vma_pin, we don't take no for an answer! */
  1270. flush_idle_contexts(vm->gt);
  1271. if (mutex_lock_interruptible(&vm->mutex) == 0) {
  1272. /*
  1273. * We pass NULL ww here, as we don't want to unbind
  1274. * locked objects when called from execbuf when pinning
  1275. * is removed. This would probably regress badly.
  1276. */
  1277. i915_gem_evict_vm(vm, NULL, NULL);
  1278. mutex_unlock(&vm->mutex);
  1279. }
  1280. } while (1);
  1281. }
  1282. int i915_ggtt_pin(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
  1283. u32 align, unsigned int flags)
  1284. {
  1285. struct i915_gem_ww_ctx _ww;
  1286. int err;
  1287. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  1288. if (ww)
  1289. return __i915_ggtt_pin(vma, ww, align, flags);
  1290. lockdep_assert_not_held(&vma->obj->base.resv->lock.base);
  1291. for_i915_gem_ww(&_ww, err, true) {
  1292. err = i915_gem_object_lock(vma->obj, &_ww);
  1293. if (!err)
  1294. err = __i915_ggtt_pin(vma, &_ww, align, flags);
  1295. }
  1296. return err;
  1297. }
  1298. static void __vma_close(struct i915_vma *vma, struct intel_gt *gt)
  1299. {
  1300. /*
  1301. * We defer actually closing, unbinding and destroying the VMA until
  1302. * the next idle point, or if the object is freed in the meantime. By
  1303. * postponing the unbind, we allow for it to be resurrected by the
  1304. * client, avoiding the work required to rebind the VMA. This is
  1305. * advantageous for DRI, where the client/server pass objects
  1306. * between themselves, temporarily opening a local VMA to the
  1307. * object, and then closing it again. The same object is then reused
  1308. * on the next frame (or two, depending on the depth of the swap queue)
  1309. * causing us to rebind the VMA once more. This ends up being a lot
  1310. * of wasted work for the steady state.
  1311. */
  1312. GEM_BUG_ON(i915_vma_is_closed(vma));
  1313. list_add(&vma->closed_link, &gt->closed_vma);
  1314. }
  1315. void i915_vma_close(struct i915_vma *vma)
  1316. {
  1317. struct intel_gt *gt = vma->vm->gt;
  1318. unsigned long flags;
  1319. if (i915_vma_is_ggtt(vma))
  1320. return;
  1321. GEM_BUG_ON(!atomic_read(&vma->open_count));
  1322. if (atomic_dec_and_lock_irqsave(&vma->open_count,
  1323. &gt->closed_lock,
  1324. flags)) {
  1325. __vma_close(vma, gt);
  1326. spin_unlock_irqrestore(&gt->closed_lock, flags);
  1327. }
  1328. }
  1329. static void __i915_vma_remove_closed(struct i915_vma *vma)
  1330. {
  1331. list_del_init(&vma->closed_link);
  1332. }
  1333. void i915_vma_reopen(struct i915_vma *vma)
  1334. {
  1335. struct intel_gt *gt = vma->vm->gt;
  1336. spin_lock_irq(&gt->closed_lock);
  1337. if (i915_vma_is_closed(vma))
  1338. __i915_vma_remove_closed(vma);
  1339. spin_unlock_irq(&gt->closed_lock);
  1340. }
  1341. static void force_unbind(struct i915_vma *vma)
  1342. {
  1343. if (!drm_mm_node_allocated(&vma->node))
  1344. return;
  1345. atomic_and(~I915_VMA_PIN_MASK, &vma->flags);
  1346. WARN_ON(__i915_vma_unbind(vma));
  1347. GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
  1348. }
  1349. static void release_references(struct i915_vma *vma, struct intel_gt *gt,
  1350. bool vm_ddestroy)
  1351. {
  1352. struct drm_i915_gem_object *obj = vma->obj;
  1353. GEM_BUG_ON(i915_vma_is_active(vma));
  1354. spin_lock(&obj->vma.lock);
  1355. list_del(&vma->obj_link);
  1356. if (!RB_EMPTY_NODE(&vma->obj_node))
  1357. rb_erase(&vma->obj_node, &obj->vma.tree);
  1358. spin_unlock(&obj->vma.lock);
  1359. spin_lock_irq(&gt->closed_lock);
  1360. __i915_vma_remove_closed(vma);
  1361. spin_unlock_irq(&gt->closed_lock);
  1362. if (vm_ddestroy)
  1363. i915_vm_resv_put(vma->vm);
  1364. i915_active_fini(&vma->active);
  1365. GEM_WARN_ON(vma->resource);
  1366. i915_vma_free(vma);
  1367. }
  1368. /**
  1369. * i915_vma_destroy_locked - Remove all weak reference to the vma and put
  1370. * the initial reference.
  1371. *
  1372. * This function should be called when it's decided the vma isn't needed
  1373. * anymore. The caller must assure that it doesn't race with another lookup
  1374. * plus destroy, typically by taking an appropriate reference.
  1375. *
  1376. * Current callsites are
  1377. * - __i915_gem_object_pages_fini()
  1378. * - __i915_vm_close() - Blocks the above function by taking a reference on
  1379. * the object.
  1380. * - __i915_vma_parked() - Blocks the above functions by taking a reference
  1381. * on the vm and a reference on the object. Also takes the object lock so
  1382. * destruction from __i915_vma_parked() can be blocked by holding the
  1383. * object lock. Since the object lock is only allowed from within i915 with
  1384. * an object refcount, holding the object lock also implicitly blocks the
  1385. * vma freeing from __i915_gem_object_pages_fini().
  1386. *
  1387. * Because of locks taken during destruction, a vma is also guaranteed to
  1388. * stay alive while the following locks are held if it was looked up while
  1389. * holding one of the locks:
  1390. * - vm->mutex
  1391. * - obj->vma.lock
  1392. * - gt->closed_lock
  1393. */
  1394. void i915_vma_destroy_locked(struct i915_vma *vma)
  1395. {
  1396. lockdep_assert_held(&vma->vm->mutex);
  1397. force_unbind(vma);
  1398. list_del_init(&vma->vm_link);
  1399. release_references(vma, vma->vm->gt, false);
  1400. }
  1401. void i915_vma_destroy(struct i915_vma *vma)
  1402. {
  1403. struct intel_gt *gt;
  1404. bool vm_ddestroy;
  1405. mutex_lock(&vma->vm->mutex);
  1406. force_unbind(vma);
  1407. list_del_init(&vma->vm_link);
  1408. vm_ddestroy = vma->vm_ddestroy;
  1409. vma->vm_ddestroy = false;
  1410. /* vma->vm may be freed when releasing vma->vm->mutex. */
  1411. gt = vma->vm->gt;
  1412. mutex_unlock(&vma->vm->mutex);
  1413. release_references(vma, gt, vm_ddestroy);
  1414. }
  1415. void i915_vma_parked(struct intel_gt *gt)
  1416. {
  1417. struct i915_vma *vma, *next;
  1418. LIST_HEAD(closed);
  1419. spin_lock_irq(&gt->closed_lock);
  1420. list_for_each_entry_safe(vma, next, &gt->closed_vma, closed_link) {
  1421. struct drm_i915_gem_object *obj = vma->obj;
  1422. struct i915_address_space *vm = vma->vm;
  1423. /* XXX All to avoid keeping a reference on i915_vma itself */
  1424. if (!kref_get_unless_zero(&obj->base.refcount))
  1425. continue;
  1426. if (!i915_vm_tryget(vm)) {
  1427. i915_gem_object_put(obj);
  1428. continue;
  1429. }
  1430. list_move(&vma->closed_link, &closed);
  1431. }
  1432. spin_unlock_irq(&gt->closed_lock);
  1433. /* As the GT is held idle, no vma can be reopened as we destroy them */
  1434. list_for_each_entry_safe(vma, next, &closed, closed_link) {
  1435. struct drm_i915_gem_object *obj = vma->obj;
  1436. struct i915_address_space *vm = vma->vm;
  1437. if (i915_gem_object_trylock(obj, NULL)) {
  1438. INIT_LIST_HEAD(&vma->closed_link);
  1439. i915_vma_destroy(vma);
  1440. i915_gem_object_unlock(obj);
  1441. } else {
  1442. /* back you go.. */
  1443. spin_lock_irq(&gt->closed_lock);
  1444. list_add(&vma->closed_link, &gt->closed_vma);
  1445. spin_unlock_irq(&gt->closed_lock);
  1446. }
  1447. i915_gem_object_put(obj);
  1448. i915_vm_put(vm);
  1449. }
  1450. }
  1451. static void __i915_vma_iounmap(struct i915_vma *vma)
  1452. {
  1453. GEM_BUG_ON(i915_vma_is_pinned(vma));
  1454. if (vma->iomap == NULL)
  1455. return;
  1456. if (page_unmask_bits(vma->iomap))
  1457. __i915_gem_object_release_map(vma->obj);
  1458. else
  1459. io_mapping_unmap(vma->iomap);
  1460. vma->iomap = NULL;
  1461. }
  1462. void i915_vma_revoke_mmap(struct i915_vma *vma)
  1463. {
  1464. struct drm_vma_offset_node *node;
  1465. u64 vma_offset;
  1466. if (!i915_vma_has_userfault(vma))
  1467. return;
  1468. GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
  1469. GEM_BUG_ON(!vma->obj->userfault_count);
  1470. node = &vma->mmo->vma_node;
  1471. vma_offset = vma->gtt_view.partial.offset << PAGE_SHIFT;
  1472. unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
  1473. drm_vma_node_offset_addr(node) + vma_offset,
  1474. vma->size,
  1475. 1);
  1476. i915_vma_unset_userfault(vma);
  1477. if (!--vma->obj->userfault_count)
  1478. list_del(&vma->obj->userfault_link);
  1479. }
  1480. static int
  1481. __i915_request_await_bind(struct i915_request *rq, struct i915_vma *vma)
  1482. {
  1483. return __i915_request_await_exclusive(rq, &vma->active);
  1484. }
  1485. static int __i915_vma_move_to_active(struct i915_vma *vma, struct i915_request *rq)
  1486. {
  1487. int err;
  1488. /* Wait for the vma to be bound before we start! */
  1489. err = __i915_request_await_bind(rq, vma);
  1490. if (err)
  1491. return err;
  1492. return i915_active_add_request(&vma->active, rq);
  1493. }
  1494. int _i915_vma_move_to_active(struct i915_vma *vma,
  1495. struct i915_request *rq,
  1496. struct dma_fence *fence,
  1497. unsigned int flags)
  1498. {
  1499. struct drm_i915_gem_object *obj = vma->obj;
  1500. int err;
  1501. assert_object_held(obj);
  1502. GEM_BUG_ON(!vma->pages);
  1503. err = __i915_vma_move_to_active(vma, rq);
  1504. if (unlikely(err))
  1505. return err;
  1506. /*
  1507. * Reserve fences slot early to prevent an allocation after preparing
  1508. * the workload and associating fences with dma_resv.
  1509. */
  1510. if (fence && !(flags & __EXEC_OBJECT_NO_RESERVE)) {
  1511. struct dma_fence *curr;
  1512. int idx;
  1513. dma_fence_array_for_each(curr, idx, fence)
  1514. ;
  1515. err = dma_resv_reserve_fences(vma->obj->base.resv, idx);
  1516. if (unlikely(err))
  1517. return err;
  1518. }
  1519. if (flags & EXEC_OBJECT_WRITE) {
  1520. struct intel_frontbuffer *front;
  1521. front = __intel_frontbuffer_get(obj);
  1522. if (unlikely(front)) {
  1523. if (intel_frontbuffer_invalidate(front, ORIGIN_CS))
  1524. i915_active_add_request(&front->write, rq);
  1525. intel_frontbuffer_put(front);
  1526. }
  1527. }
  1528. if (fence) {
  1529. struct dma_fence *curr;
  1530. enum dma_resv_usage usage;
  1531. int idx;
  1532. if (flags & EXEC_OBJECT_WRITE) {
  1533. usage = DMA_RESV_USAGE_WRITE;
  1534. obj->write_domain = I915_GEM_DOMAIN_RENDER;
  1535. obj->read_domains = 0;
  1536. } else {
  1537. usage = DMA_RESV_USAGE_READ;
  1538. obj->write_domain = 0;
  1539. }
  1540. dma_fence_array_for_each(curr, idx, fence)
  1541. dma_resv_add_fence(vma->obj->base.resv, curr, usage);
  1542. }
  1543. if (flags & EXEC_OBJECT_NEEDS_FENCE && vma->fence)
  1544. i915_active_add_request(&vma->fence->active, rq);
  1545. obj->read_domains |= I915_GEM_GPU_DOMAINS;
  1546. obj->mm.dirty = true;
  1547. GEM_BUG_ON(!i915_vma_is_active(vma));
  1548. return 0;
  1549. }
  1550. struct dma_fence *__i915_vma_evict(struct i915_vma *vma, bool async)
  1551. {
  1552. struct i915_vma_resource *vma_res = vma->resource;
  1553. struct dma_fence *unbind_fence;
  1554. GEM_BUG_ON(i915_vma_is_pinned(vma));
  1555. assert_vma_held_evict(vma);
  1556. if (i915_vma_is_map_and_fenceable(vma)) {
  1557. /* Force a pagefault for domain tracking on next user access */
  1558. i915_vma_revoke_mmap(vma);
  1559. /*
  1560. * Check that we have flushed all writes through the GGTT
  1561. * before the unbind, other due to non-strict nature of those
  1562. * indirect writes they may end up referencing the GGTT PTE
  1563. * after the unbind.
  1564. *
  1565. * Note that we may be concurrently poking at the GGTT_WRITE
  1566. * bit from set-domain, as we mark all GGTT vma associated
  1567. * with an object. We know this is for another vma, as we
  1568. * are currently unbinding this one -- so if this vma will be
  1569. * reused, it will be refaulted and have its dirty bit set
  1570. * before the next write.
  1571. */
  1572. i915_vma_flush_writes(vma);
  1573. /* release the fence reg _after_ flushing */
  1574. i915_vma_revoke_fence(vma);
  1575. clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
  1576. }
  1577. __i915_vma_iounmap(vma);
  1578. GEM_BUG_ON(vma->fence);
  1579. GEM_BUG_ON(i915_vma_has_userfault(vma));
  1580. /* Object backend must be async capable. */
  1581. GEM_WARN_ON(async && !vma->resource->bi.pages_rsgt);
  1582. /* If vm is not open, unbind is a nop. */
  1583. vma_res->needs_wakeref = i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND) &&
  1584. kref_read(&vma->vm->ref);
  1585. vma_res->skip_pte_rewrite = !kref_read(&vma->vm->ref) ||
  1586. vma->vm->skip_pte_rewrite;
  1587. trace_i915_vma_unbind(vma);
  1588. if (async)
  1589. unbind_fence = i915_vma_resource_unbind(vma_res,
  1590. &vma->obj->mm.tlb);
  1591. else
  1592. unbind_fence = i915_vma_resource_unbind(vma_res, NULL);
  1593. vma->resource = NULL;
  1594. atomic_and(~(I915_VMA_BIND_MASK | I915_VMA_ERROR | I915_VMA_GGTT_WRITE),
  1595. &vma->flags);
  1596. i915_vma_detach(vma);
  1597. if (!async) {
  1598. if (unbind_fence) {
  1599. dma_fence_wait(unbind_fence, false);
  1600. dma_fence_put(unbind_fence);
  1601. unbind_fence = NULL;
  1602. }
  1603. vma_invalidate_tlb(vma->vm, &vma->obj->mm.tlb);
  1604. }
  1605. /*
  1606. * Binding itself may not have completed until the unbind fence signals,
  1607. * so don't drop the pages until that happens, unless the resource is
  1608. * async_capable.
  1609. */
  1610. vma_unbind_pages(vma);
  1611. return unbind_fence;
  1612. }
  1613. int __i915_vma_unbind(struct i915_vma *vma)
  1614. {
  1615. int ret;
  1616. lockdep_assert_held(&vma->vm->mutex);
  1617. assert_vma_held_evict(vma);
  1618. if (!drm_mm_node_allocated(&vma->node))
  1619. return 0;
  1620. if (i915_vma_is_pinned(vma)) {
  1621. vma_print_allocator(vma, "is pinned");
  1622. return -EAGAIN;
  1623. }
  1624. /*
  1625. * After confirming that no one else is pinning this vma, wait for
  1626. * any laggards who may have crept in during the wait (through
  1627. * a residual pin skipping the vm->mutex) to complete.
  1628. */
  1629. ret = i915_vma_sync(vma);
  1630. if (ret)
  1631. return ret;
  1632. GEM_BUG_ON(i915_vma_is_active(vma));
  1633. __i915_vma_evict(vma, false);
  1634. drm_mm_remove_node(&vma->node); /* pairs with i915_vma_release() */
  1635. return 0;
  1636. }
  1637. static struct dma_fence *__i915_vma_unbind_async(struct i915_vma *vma)
  1638. {
  1639. struct dma_fence *fence;
  1640. lockdep_assert_held(&vma->vm->mutex);
  1641. if (!drm_mm_node_allocated(&vma->node))
  1642. return NULL;
  1643. if (i915_vma_is_pinned(vma) ||
  1644. &vma->obj->mm.rsgt->table != vma->resource->bi.pages)
  1645. return ERR_PTR(-EAGAIN);
  1646. /*
  1647. * We probably need to replace this with awaiting the fences of the
  1648. * object's dma_resv when the vma active goes away. When doing that
  1649. * we need to be careful to not add the vma_resource unbind fence
  1650. * immediately to the object's dma_resv, because then unbinding
  1651. * the next vma from the object, in case there are many, will
  1652. * actually await the unbinding of the previous vmas, which is
  1653. * undesirable.
  1654. */
  1655. if (i915_sw_fence_await_active(&vma->resource->chain, &vma->active,
  1656. I915_ACTIVE_AWAIT_EXCL |
  1657. I915_ACTIVE_AWAIT_ACTIVE) < 0) {
  1658. return ERR_PTR(-EBUSY);
  1659. }
  1660. fence = __i915_vma_evict(vma, true);
  1661. drm_mm_remove_node(&vma->node); /* pairs with i915_vma_release() */
  1662. return fence;
  1663. }
  1664. int i915_vma_unbind(struct i915_vma *vma)
  1665. {
  1666. struct i915_address_space *vm = vma->vm;
  1667. intel_wakeref_t wakeref = 0;
  1668. int err;
  1669. assert_object_held_shared(vma->obj);
  1670. /* Optimistic wait before taking the mutex */
  1671. err = i915_vma_sync(vma);
  1672. if (err)
  1673. return err;
  1674. if (!drm_mm_node_allocated(&vma->node))
  1675. return 0;
  1676. if (i915_vma_is_pinned(vma)) {
  1677. vma_print_allocator(vma, "is pinned");
  1678. return -EAGAIN;
  1679. }
  1680. if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
  1681. /* XXX not always required: nop_clear_range */
  1682. wakeref = intel_runtime_pm_get(&vm->i915->runtime_pm);
  1683. err = mutex_lock_interruptible_nested(&vma->vm->mutex, !wakeref);
  1684. if (err)
  1685. goto out_rpm;
  1686. err = __i915_vma_unbind(vma);
  1687. mutex_unlock(&vm->mutex);
  1688. out_rpm:
  1689. if (wakeref)
  1690. intel_runtime_pm_put(&vm->i915->runtime_pm, wakeref);
  1691. return err;
  1692. }
  1693. int i915_vma_unbind_async(struct i915_vma *vma, bool trylock_vm)
  1694. {
  1695. struct drm_i915_gem_object *obj = vma->obj;
  1696. struct i915_address_space *vm = vma->vm;
  1697. intel_wakeref_t wakeref = 0;
  1698. struct dma_fence *fence;
  1699. int err;
  1700. /*
  1701. * We need the dma-resv lock since we add the
  1702. * unbind fence to the dma-resv object.
  1703. */
  1704. assert_object_held(obj);
  1705. if (!drm_mm_node_allocated(&vma->node))
  1706. return 0;
  1707. if (i915_vma_is_pinned(vma)) {
  1708. vma_print_allocator(vma, "is pinned");
  1709. return -EAGAIN;
  1710. }
  1711. if (!obj->mm.rsgt)
  1712. return -EBUSY;
  1713. err = dma_resv_reserve_fences(obj->base.resv, 2);
  1714. if (err)
  1715. return -EBUSY;
  1716. /*
  1717. * It would be great if we could grab this wakeref from the
  1718. * async unbind work if needed, but we can't because it uses
  1719. * kmalloc and it's in the dma-fence signalling critical path.
  1720. */
  1721. if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
  1722. wakeref = intel_runtime_pm_get(&vm->i915->runtime_pm);
  1723. if (trylock_vm && !mutex_trylock(&vm->mutex)) {
  1724. err = -EBUSY;
  1725. goto out_rpm;
  1726. } else if (!trylock_vm) {
  1727. err = mutex_lock_interruptible_nested(&vm->mutex, !wakeref);
  1728. if (err)
  1729. goto out_rpm;
  1730. }
  1731. fence = __i915_vma_unbind_async(vma);
  1732. mutex_unlock(&vm->mutex);
  1733. if (IS_ERR_OR_NULL(fence)) {
  1734. err = PTR_ERR_OR_ZERO(fence);
  1735. goto out_rpm;
  1736. }
  1737. dma_resv_add_fence(obj->base.resv, fence, DMA_RESV_USAGE_READ);
  1738. dma_fence_put(fence);
  1739. out_rpm:
  1740. if (wakeref)
  1741. intel_runtime_pm_put(&vm->i915->runtime_pm, wakeref);
  1742. return err;
  1743. }
  1744. int i915_vma_unbind_unlocked(struct i915_vma *vma)
  1745. {
  1746. int err;
  1747. i915_gem_object_lock(vma->obj, NULL);
  1748. err = i915_vma_unbind(vma);
  1749. i915_gem_object_unlock(vma->obj);
  1750. return err;
  1751. }
  1752. struct i915_vma *i915_vma_make_unshrinkable(struct i915_vma *vma)
  1753. {
  1754. i915_gem_object_make_unshrinkable(vma->obj);
  1755. return vma;
  1756. }
  1757. void i915_vma_make_shrinkable(struct i915_vma *vma)
  1758. {
  1759. i915_gem_object_make_shrinkable(vma->obj);
  1760. }
  1761. void i915_vma_make_purgeable(struct i915_vma *vma)
  1762. {
  1763. i915_gem_object_make_purgeable(vma->obj);
  1764. }
  1765. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1766. #include "selftests/i915_vma.c"
  1767. #endif
  1768. void i915_vma_module_exit(void)
  1769. {
  1770. kmem_cache_destroy(slab_vmas);
  1771. }
  1772. int __init i915_vma_module_init(void)
  1773. {
  1774. slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
  1775. if (!slab_vmas)
  1776. return -ENOMEM;
  1777. return 0;
  1778. }