i915_gem_execbuffer.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /*
  2. * Copyright © 2008,2010 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. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Chris Wilson <chris@chris-wilson.co.uk>
  26. *
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/dma_remapping.h>
  34. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  35. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  36. #define __EXEC_OBJECT_NEEDS_MAP (1<<29)
  37. #define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
  38. #define BATCH_OFFSET_BIAS (256*1024)
  39. struct eb_vmas {
  40. struct list_head vmas;
  41. int and;
  42. union {
  43. struct i915_vma *lut[0];
  44. struct hlist_head buckets[0];
  45. };
  46. };
  47. static struct eb_vmas *
  48. eb_create(struct drm_i915_gem_execbuffer2 *args)
  49. {
  50. struct eb_vmas *eb = NULL;
  51. if (args->flags & I915_EXEC_HANDLE_LUT) {
  52. unsigned size = args->buffer_count;
  53. size *= sizeof(struct i915_vma *);
  54. size += sizeof(struct eb_vmas);
  55. eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  56. }
  57. if (eb == NULL) {
  58. unsigned size = args->buffer_count;
  59. unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  60. BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
  61. while (count > 2*size)
  62. count >>= 1;
  63. eb = kzalloc(count*sizeof(struct hlist_head) +
  64. sizeof(struct eb_vmas),
  65. GFP_TEMPORARY);
  66. if (eb == NULL)
  67. return eb;
  68. eb->and = count - 1;
  69. } else
  70. eb->and = -args->buffer_count;
  71. INIT_LIST_HEAD(&eb->vmas);
  72. return eb;
  73. }
  74. static void
  75. eb_reset(struct eb_vmas *eb)
  76. {
  77. if (eb->and >= 0)
  78. memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  79. }
  80. static int
  81. eb_lookup_vmas(struct eb_vmas *eb,
  82. struct drm_i915_gem_exec_object2 *exec,
  83. const struct drm_i915_gem_execbuffer2 *args,
  84. struct i915_address_space *vm,
  85. struct drm_file *file)
  86. {
  87. struct drm_i915_gem_object *obj;
  88. struct list_head objects;
  89. int i, ret;
  90. INIT_LIST_HEAD(&objects);
  91. spin_lock(&file->table_lock);
  92. /* Grab a reference to the object and release the lock so we can lookup
  93. * or create the VMA without using GFP_ATOMIC */
  94. for (i = 0; i < args->buffer_count; i++) {
  95. obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
  96. if (obj == NULL) {
  97. spin_unlock(&file->table_lock);
  98. DRM_DEBUG("Invalid object handle %d at index %d\n",
  99. exec[i].handle, i);
  100. ret = -ENOENT;
  101. goto err;
  102. }
  103. if (!list_empty(&obj->obj_exec_link)) {
  104. spin_unlock(&file->table_lock);
  105. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  106. obj, exec[i].handle, i);
  107. ret = -EINVAL;
  108. goto err;
  109. }
  110. drm_gem_object_reference(&obj->base);
  111. list_add_tail(&obj->obj_exec_link, &objects);
  112. }
  113. spin_unlock(&file->table_lock);
  114. i = 0;
  115. while (!list_empty(&objects)) {
  116. struct i915_vma *vma;
  117. obj = list_first_entry(&objects,
  118. struct drm_i915_gem_object,
  119. obj_exec_link);
  120. /*
  121. * NOTE: We can leak any vmas created here when something fails
  122. * later on. But that's no issue since vma_unbind can deal with
  123. * vmas which are not actually bound. And since only
  124. * lookup_or_create exists as an interface to get at the vma
  125. * from the (obj, vm) we don't run the risk of creating
  126. * duplicated vmas for the same vm.
  127. */
  128. vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
  129. if (IS_ERR(vma)) {
  130. DRM_DEBUG("Failed to lookup VMA\n");
  131. ret = PTR_ERR(vma);
  132. goto err;
  133. }
  134. /* Transfer ownership from the objects list to the vmas list. */
  135. list_add_tail(&vma->exec_list, &eb->vmas);
  136. list_del_init(&obj->obj_exec_link);
  137. vma->exec_entry = &exec[i];
  138. if (eb->and < 0) {
  139. eb->lut[i] = vma;
  140. } else {
  141. uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
  142. vma->exec_handle = handle;
  143. hlist_add_head(&vma->exec_node,
  144. &eb->buckets[handle & eb->and]);
  145. }
  146. ++i;
  147. }
  148. return 0;
  149. err:
  150. while (!list_empty(&objects)) {
  151. obj = list_first_entry(&objects,
  152. struct drm_i915_gem_object,
  153. obj_exec_link);
  154. list_del_init(&obj->obj_exec_link);
  155. drm_gem_object_unreference(&obj->base);
  156. }
  157. /*
  158. * Objects already transfered to the vmas list will be unreferenced by
  159. * eb_destroy.
  160. */
  161. return ret;
  162. }
  163. static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
  164. {
  165. if (eb->and < 0) {
  166. if (handle >= -eb->and)
  167. return NULL;
  168. return eb->lut[handle];
  169. } else {
  170. struct hlist_head *head;
  171. struct hlist_node *node;
  172. head = &eb->buckets[handle & eb->and];
  173. hlist_for_each(node, head) {
  174. struct i915_vma *vma;
  175. vma = hlist_entry(node, struct i915_vma, exec_node);
  176. if (vma->exec_handle == handle)
  177. return vma;
  178. }
  179. return NULL;
  180. }
  181. }
  182. static void
  183. i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
  184. {
  185. struct drm_i915_gem_exec_object2 *entry;
  186. struct drm_i915_gem_object *obj = vma->obj;
  187. if (!drm_mm_node_allocated(&vma->node))
  188. return;
  189. entry = vma->exec_entry;
  190. if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
  191. i915_gem_object_unpin_fence(obj);
  192. if (entry->flags & __EXEC_OBJECT_HAS_PIN)
  193. vma->pin_count--;
  194. entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  195. }
  196. static void eb_destroy(struct eb_vmas *eb)
  197. {
  198. while (!list_empty(&eb->vmas)) {
  199. struct i915_vma *vma;
  200. vma = list_first_entry(&eb->vmas,
  201. struct i915_vma,
  202. exec_list);
  203. list_del_init(&vma->exec_list);
  204. i915_gem_execbuffer_unreserve_vma(vma);
  205. drm_gem_object_unreference(&vma->obj->base);
  206. }
  207. kfree(eb);
  208. }
  209. static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
  210. {
  211. return (HAS_LLC(obj->base.dev) ||
  212. obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
  213. !obj->map_and_fenceable ||
  214. obj->cache_level != I915_CACHE_NONE);
  215. }
  216. static int
  217. relocate_entry_cpu(struct drm_i915_gem_object *obj,
  218. struct drm_i915_gem_relocation_entry *reloc,
  219. uint64_t target_offset)
  220. {
  221. struct drm_device *dev = obj->base.dev;
  222. uint32_t page_offset = offset_in_page(reloc->offset);
  223. uint64_t delta = reloc->delta + target_offset;
  224. char *vaddr;
  225. int ret;
  226. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  227. if (ret)
  228. return ret;
  229. vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  230. reloc->offset >> PAGE_SHIFT));
  231. *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
  232. if (INTEL_INFO(dev)->gen >= 8) {
  233. page_offset = offset_in_page(page_offset + sizeof(uint32_t));
  234. if (page_offset == 0) {
  235. kunmap_atomic(vaddr);
  236. vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  237. (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
  238. }
  239. *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
  240. }
  241. kunmap_atomic(vaddr);
  242. return 0;
  243. }
  244. static int
  245. relocate_entry_gtt(struct drm_i915_gem_object *obj,
  246. struct drm_i915_gem_relocation_entry *reloc,
  247. uint64_t target_offset)
  248. {
  249. struct drm_device *dev = obj->base.dev;
  250. struct drm_i915_private *dev_priv = dev->dev_private;
  251. uint64_t delta = reloc->delta + target_offset;
  252. uint32_t __iomem *reloc_entry;
  253. void __iomem *reloc_page;
  254. int ret;
  255. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  256. if (ret)
  257. return ret;
  258. ret = i915_gem_object_put_fence(obj);
  259. if (ret)
  260. return ret;
  261. /* Map the page containing the relocation we're going to perform. */
  262. reloc->offset += i915_gem_obj_ggtt_offset(obj);
  263. reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
  264. reloc->offset & PAGE_MASK);
  265. reloc_entry = (uint32_t __iomem *)
  266. (reloc_page + offset_in_page(reloc->offset));
  267. iowrite32(lower_32_bits(delta), reloc_entry);
  268. if (INTEL_INFO(dev)->gen >= 8) {
  269. reloc_entry += 1;
  270. if (offset_in_page(reloc->offset + sizeof(uint32_t)) == 0) {
  271. io_mapping_unmap_atomic(reloc_page);
  272. reloc_page = io_mapping_map_atomic_wc(
  273. dev_priv->gtt.mappable,
  274. reloc->offset + sizeof(uint32_t));
  275. reloc_entry = reloc_page;
  276. }
  277. iowrite32(upper_32_bits(delta), reloc_entry);
  278. }
  279. io_mapping_unmap_atomic(reloc_page);
  280. return 0;
  281. }
  282. static int
  283. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  284. struct eb_vmas *eb,
  285. struct drm_i915_gem_relocation_entry *reloc)
  286. {
  287. struct drm_device *dev = obj->base.dev;
  288. struct drm_gem_object *target_obj;
  289. struct drm_i915_gem_object *target_i915_obj;
  290. struct i915_vma *target_vma;
  291. uint64_t target_offset;
  292. int ret;
  293. /* we've already hold a reference to all valid objects */
  294. target_vma = eb_get_vma(eb, reloc->target_handle);
  295. if (unlikely(target_vma == NULL))
  296. return -ENOENT;
  297. target_i915_obj = target_vma->obj;
  298. target_obj = &target_vma->obj->base;
  299. target_offset = target_vma->node.start;
  300. /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
  301. * pipe_control writes because the gpu doesn't properly redirect them
  302. * through the ppgtt for non_secure batchbuffers. */
  303. if (unlikely(IS_GEN6(dev) &&
  304. reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
  305. !target_i915_obj->has_global_gtt_mapping)) {
  306. struct i915_vma *vma =
  307. list_first_entry(&target_i915_obj->vma_list,
  308. typeof(*vma), vma_link);
  309. vma->bind_vma(vma, target_i915_obj->cache_level, GLOBAL_BIND);
  310. }
  311. /* Validate that the target is in a valid r/w GPU domain */
  312. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  313. DRM_DEBUG("reloc with multiple write domains: "
  314. "obj %p target %d offset %d "
  315. "read %08x write %08x",
  316. obj, reloc->target_handle,
  317. (int) reloc->offset,
  318. reloc->read_domains,
  319. reloc->write_domain);
  320. return -EINVAL;
  321. }
  322. if (unlikely((reloc->write_domain | reloc->read_domains)
  323. & ~I915_GEM_GPU_DOMAINS)) {
  324. DRM_DEBUG("reloc with read/write non-GPU domains: "
  325. "obj %p target %d offset %d "
  326. "read %08x write %08x",
  327. obj, reloc->target_handle,
  328. (int) reloc->offset,
  329. reloc->read_domains,
  330. reloc->write_domain);
  331. return -EINVAL;
  332. }
  333. target_obj->pending_read_domains |= reloc->read_domains;
  334. target_obj->pending_write_domain |= reloc->write_domain;
  335. /* If the relocation already has the right value in it, no
  336. * more work needs to be done.
  337. */
  338. if (target_offset == reloc->presumed_offset)
  339. return 0;
  340. /* Check that the relocation address is valid... */
  341. if (unlikely(reloc->offset >
  342. obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
  343. DRM_DEBUG("Relocation beyond object bounds: "
  344. "obj %p target %d offset %d size %d.\n",
  345. obj, reloc->target_handle,
  346. (int) reloc->offset,
  347. (int) obj->base.size);
  348. return -EINVAL;
  349. }
  350. if (unlikely(reloc->offset & 3)) {
  351. DRM_DEBUG("Relocation not 4-byte aligned: "
  352. "obj %p target %d offset %d.\n",
  353. obj, reloc->target_handle,
  354. (int) reloc->offset);
  355. return -EINVAL;
  356. }
  357. /* We can't wait for rendering with pagefaults disabled */
  358. if (obj->active && in_atomic())
  359. return -EFAULT;
  360. if (use_cpu_reloc(obj))
  361. ret = relocate_entry_cpu(obj, reloc, target_offset);
  362. else
  363. ret = relocate_entry_gtt(obj, reloc, target_offset);
  364. if (ret)
  365. return ret;
  366. /* and update the user's relocation entry */
  367. reloc->presumed_offset = target_offset;
  368. return 0;
  369. }
  370. static int
  371. i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
  372. struct eb_vmas *eb)
  373. {
  374. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  375. struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
  376. struct drm_i915_gem_relocation_entry __user *user_relocs;
  377. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  378. int remain, ret;
  379. user_relocs = to_user_ptr(entry->relocs_ptr);
  380. remain = entry->relocation_count;
  381. while (remain) {
  382. struct drm_i915_gem_relocation_entry *r = stack_reloc;
  383. int count = remain;
  384. if (count > ARRAY_SIZE(stack_reloc))
  385. count = ARRAY_SIZE(stack_reloc);
  386. remain -= count;
  387. if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
  388. return -EFAULT;
  389. do {
  390. u64 offset = r->presumed_offset;
  391. ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
  392. if (ret)
  393. return ret;
  394. if (r->presumed_offset != offset &&
  395. __copy_to_user_inatomic(&user_relocs->presumed_offset,
  396. &r->presumed_offset,
  397. sizeof(r->presumed_offset))) {
  398. return -EFAULT;
  399. }
  400. user_relocs++;
  401. r++;
  402. } while (--count);
  403. }
  404. return 0;
  405. #undef N_RELOC
  406. }
  407. static int
  408. i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
  409. struct eb_vmas *eb,
  410. struct drm_i915_gem_relocation_entry *relocs)
  411. {
  412. const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  413. int i, ret;
  414. for (i = 0; i < entry->relocation_count; i++) {
  415. ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
  416. if (ret)
  417. return ret;
  418. }
  419. return 0;
  420. }
  421. static int
  422. i915_gem_execbuffer_relocate(struct eb_vmas *eb)
  423. {
  424. struct i915_vma *vma;
  425. int ret = 0;
  426. /* This is the fast path and we cannot handle a pagefault whilst
  427. * holding the struct mutex lest the user pass in the relocations
  428. * contained within a mmaped bo. For in such a case we, the page
  429. * fault handler would call i915_gem_fault() and we would try to
  430. * acquire the struct mutex again. Obviously this is bad and so
  431. * lockdep complains vehemently.
  432. */
  433. pagefault_disable();
  434. list_for_each_entry(vma, &eb->vmas, exec_list) {
  435. ret = i915_gem_execbuffer_relocate_vma(vma, eb);
  436. if (ret)
  437. break;
  438. }
  439. pagefault_enable();
  440. return ret;
  441. }
  442. static int
  443. i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
  444. struct intel_engine_cs *ring,
  445. bool *need_reloc)
  446. {
  447. struct drm_i915_gem_object *obj = vma->obj;
  448. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  449. uint64_t flags;
  450. int ret;
  451. flags = 0;
  452. if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
  453. flags |= PIN_MAPPABLE;
  454. if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
  455. flags |= PIN_GLOBAL;
  456. if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
  457. flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
  458. ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
  459. if (ret)
  460. return ret;
  461. entry->flags |= __EXEC_OBJECT_HAS_PIN;
  462. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  463. ret = i915_gem_object_get_fence(obj);
  464. if (ret)
  465. return ret;
  466. if (i915_gem_object_pin_fence(obj))
  467. entry->flags |= __EXEC_OBJECT_HAS_FENCE;
  468. }
  469. if (entry->offset != vma->node.start) {
  470. entry->offset = vma->node.start;
  471. *need_reloc = true;
  472. }
  473. if (entry->flags & EXEC_OBJECT_WRITE) {
  474. obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
  475. obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
  476. }
  477. return 0;
  478. }
  479. static bool
  480. need_reloc_mappable(struct i915_vma *vma)
  481. {
  482. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  483. if (entry->relocation_count == 0)
  484. return false;
  485. if (!i915_is_ggtt(vma->vm))
  486. return false;
  487. /* See also use_cpu_reloc() */
  488. if (HAS_LLC(vma->obj->base.dev))
  489. return false;
  490. if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  491. return false;
  492. return true;
  493. }
  494. static bool
  495. eb_vma_misplaced(struct i915_vma *vma)
  496. {
  497. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  498. struct drm_i915_gem_object *obj = vma->obj;
  499. WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
  500. !i915_is_ggtt(vma->vm));
  501. if (entry->alignment &&
  502. vma->node.start & (entry->alignment - 1))
  503. return true;
  504. if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
  505. return true;
  506. if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
  507. vma->node.start < BATCH_OFFSET_BIAS)
  508. return true;
  509. return false;
  510. }
  511. static int
  512. i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
  513. struct list_head *vmas,
  514. bool *need_relocs)
  515. {
  516. struct drm_i915_gem_object *obj;
  517. struct i915_vma *vma;
  518. struct i915_address_space *vm;
  519. struct list_head ordered_vmas;
  520. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  521. int retry;
  522. if (list_empty(vmas))
  523. return 0;
  524. i915_gem_retire_requests_ring(ring);
  525. vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
  526. INIT_LIST_HEAD(&ordered_vmas);
  527. while (!list_empty(vmas)) {
  528. struct drm_i915_gem_exec_object2 *entry;
  529. bool need_fence, need_mappable;
  530. vma = list_first_entry(vmas, struct i915_vma, exec_list);
  531. obj = vma->obj;
  532. entry = vma->exec_entry;
  533. if (!has_fenced_gpu_access)
  534. entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
  535. need_fence =
  536. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  537. obj->tiling_mode != I915_TILING_NONE;
  538. need_mappable = need_fence || need_reloc_mappable(vma);
  539. if (need_mappable) {
  540. entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
  541. list_move(&vma->exec_list, &ordered_vmas);
  542. } else
  543. list_move_tail(&vma->exec_list, &ordered_vmas);
  544. obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  545. obj->base.pending_write_domain = 0;
  546. }
  547. list_splice(&ordered_vmas, vmas);
  548. /* Attempt to pin all of the buffers into the GTT.
  549. * This is done in 3 phases:
  550. *
  551. * 1a. Unbind all objects that do not match the GTT constraints for
  552. * the execbuffer (fenceable, mappable, alignment etc).
  553. * 1b. Increment pin count for already bound objects.
  554. * 2. Bind new objects.
  555. * 3. Decrement pin count.
  556. *
  557. * This avoid unnecessary unbinding of later objects in order to make
  558. * room for the earlier objects *unless* we need to defragment.
  559. */
  560. retry = 0;
  561. do {
  562. int ret = 0;
  563. /* Unbind any ill-fitting objects or pin. */
  564. list_for_each_entry(vma, vmas, exec_list) {
  565. if (!drm_mm_node_allocated(&vma->node))
  566. continue;
  567. if (eb_vma_misplaced(vma))
  568. ret = i915_vma_unbind(vma);
  569. else
  570. ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  571. if (ret)
  572. goto err;
  573. }
  574. /* Bind fresh objects */
  575. list_for_each_entry(vma, vmas, exec_list) {
  576. if (drm_mm_node_allocated(&vma->node))
  577. continue;
  578. ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  579. if (ret)
  580. goto err;
  581. }
  582. err:
  583. if (ret != -ENOSPC || retry++)
  584. return ret;
  585. /* Decrement pin count for bound objects */
  586. list_for_each_entry(vma, vmas, exec_list)
  587. i915_gem_execbuffer_unreserve_vma(vma);
  588. ret = i915_gem_evict_vm(vm, true);
  589. if (ret)
  590. return ret;
  591. } while (1);
  592. }
  593. static int
  594. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  595. struct drm_i915_gem_execbuffer2 *args,
  596. struct drm_file *file,
  597. struct intel_engine_cs *ring,
  598. struct eb_vmas *eb,
  599. struct drm_i915_gem_exec_object2 *exec)
  600. {
  601. struct drm_i915_gem_relocation_entry *reloc;
  602. struct i915_address_space *vm;
  603. struct i915_vma *vma;
  604. bool need_relocs;
  605. int *reloc_offset;
  606. int i, total, ret;
  607. unsigned count = args->buffer_count;
  608. if (WARN_ON(list_empty(&eb->vmas)))
  609. return 0;
  610. vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
  611. /* We may process another execbuffer during the unlock... */
  612. while (!list_empty(&eb->vmas)) {
  613. vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
  614. list_del_init(&vma->exec_list);
  615. i915_gem_execbuffer_unreserve_vma(vma);
  616. drm_gem_object_unreference(&vma->obj->base);
  617. }
  618. mutex_unlock(&dev->struct_mutex);
  619. total = 0;
  620. for (i = 0; i < count; i++)
  621. total += exec[i].relocation_count;
  622. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  623. reloc = drm_malloc_ab(total, sizeof(*reloc));
  624. if (reloc == NULL || reloc_offset == NULL) {
  625. drm_free_large(reloc);
  626. drm_free_large(reloc_offset);
  627. mutex_lock(&dev->struct_mutex);
  628. return -ENOMEM;
  629. }
  630. total = 0;
  631. for (i = 0; i < count; i++) {
  632. struct drm_i915_gem_relocation_entry __user *user_relocs;
  633. u64 invalid_offset = (u64)-1;
  634. int j;
  635. user_relocs = to_user_ptr(exec[i].relocs_ptr);
  636. if (copy_from_user(reloc+total, user_relocs,
  637. exec[i].relocation_count * sizeof(*reloc))) {
  638. ret = -EFAULT;
  639. mutex_lock(&dev->struct_mutex);
  640. goto err;
  641. }
  642. /* As we do not update the known relocation offsets after
  643. * relocating (due to the complexities in lock handling),
  644. * we need to mark them as invalid now so that we force the
  645. * relocation processing next time. Just in case the target
  646. * object is evicted and then rebound into its old
  647. * presumed_offset before the next execbuffer - if that
  648. * happened we would make the mistake of assuming that the
  649. * relocations were valid.
  650. */
  651. for (j = 0; j < exec[i].relocation_count; j++) {
  652. if (__copy_to_user(&user_relocs[j].presumed_offset,
  653. &invalid_offset,
  654. sizeof(invalid_offset))) {
  655. ret = -EFAULT;
  656. mutex_lock(&dev->struct_mutex);
  657. goto err;
  658. }
  659. }
  660. reloc_offset[i] = total;
  661. total += exec[i].relocation_count;
  662. }
  663. ret = i915_mutex_lock_interruptible(dev);
  664. if (ret) {
  665. mutex_lock(&dev->struct_mutex);
  666. goto err;
  667. }
  668. /* reacquire the objects */
  669. eb_reset(eb);
  670. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  671. if (ret)
  672. goto err;
  673. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  674. ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
  675. if (ret)
  676. goto err;
  677. list_for_each_entry(vma, &eb->vmas, exec_list) {
  678. int offset = vma->exec_entry - exec;
  679. ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
  680. reloc + reloc_offset[offset]);
  681. if (ret)
  682. goto err;
  683. }
  684. /* Leave the user relocations as are, this is the painfully slow path,
  685. * and we want to avoid the complication of dropping the lock whilst
  686. * having buffers reserved in the aperture and so causing spurious
  687. * ENOSPC for random operations.
  688. */
  689. err:
  690. drm_free_large(reloc);
  691. drm_free_large(reloc_offset);
  692. return ret;
  693. }
  694. static int
  695. i915_gem_execbuffer_move_to_gpu(struct intel_engine_cs *ring,
  696. struct list_head *vmas)
  697. {
  698. struct i915_vma *vma;
  699. uint32_t flush_domains = 0;
  700. bool flush_chipset = false;
  701. int ret;
  702. list_for_each_entry(vma, vmas, exec_list) {
  703. struct drm_i915_gem_object *obj = vma->obj;
  704. ret = i915_gem_object_sync(obj, ring);
  705. if (ret)
  706. return ret;
  707. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  708. flush_chipset |= i915_gem_clflush_object(obj, false);
  709. flush_domains |= obj->base.write_domain;
  710. }
  711. if (flush_chipset)
  712. i915_gem_chipset_flush(ring->dev);
  713. if (flush_domains & I915_GEM_DOMAIN_GTT)
  714. wmb();
  715. /* Unconditionally invalidate gpu caches and ensure that we do flush
  716. * any residual writes from the previous batch.
  717. */
  718. return intel_ring_invalidate_all_caches(ring);
  719. }
  720. static bool
  721. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  722. {
  723. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  724. return false;
  725. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  726. }
  727. static int
  728. validate_exec_list(struct drm_device *dev,
  729. struct drm_i915_gem_exec_object2 *exec,
  730. int count)
  731. {
  732. unsigned relocs_total = 0;
  733. unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  734. unsigned invalid_flags;
  735. int i;
  736. invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  737. if (USES_FULL_PPGTT(dev))
  738. invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  739. for (i = 0; i < count; i++) {
  740. char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
  741. int length; /* limited by fault_in_pages_readable() */
  742. if (exec[i].flags & invalid_flags)
  743. return -EINVAL;
  744. /* First check for malicious input causing overflow in
  745. * the worst case where we need to allocate the entire
  746. * relocation tree as a single array.
  747. */
  748. if (exec[i].relocation_count > relocs_max - relocs_total)
  749. return -EINVAL;
  750. relocs_total += exec[i].relocation_count;
  751. length = exec[i].relocation_count *
  752. sizeof(struct drm_i915_gem_relocation_entry);
  753. /*
  754. * We must check that the entire relocation array is safe
  755. * to read, but since we may need to update the presumed
  756. * offsets during execution, check for full write access.
  757. */
  758. if (!access_ok(VERIFY_WRITE, ptr, length))
  759. return -EFAULT;
  760. if (likely(!i915.prefault_disable)) {
  761. if (fault_in_multipages_readable(ptr, length))
  762. return -EFAULT;
  763. }
  764. }
  765. return 0;
  766. }
  767. static struct intel_context *
  768. i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
  769. struct intel_engine_cs *ring, const u32 ctx_id)
  770. {
  771. struct intel_context *ctx = NULL;
  772. struct i915_ctx_hang_stats *hs;
  773. if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
  774. return ERR_PTR(-EINVAL);
  775. ctx = i915_gem_context_get(file->driver_priv, ctx_id);
  776. if (IS_ERR(ctx))
  777. return ctx;
  778. hs = &ctx->hang_stats;
  779. if (hs->banned) {
  780. DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
  781. return ERR_PTR(-EIO);
  782. }
  783. return ctx;
  784. }
  785. static void
  786. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  787. struct intel_engine_cs *ring)
  788. {
  789. u32 seqno = intel_ring_get_seqno(ring);
  790. struct i915_vma *vma;
  791. list_for_each_entry(vma, vmas, exec_list) {
  792. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  793. struct drm_i915_gem_object *obj = vma->obj;
  794. u32 old_read = obj->base.read_domains;
  795. u32 old_write = obj->base.write_domain;
  796. obj->base.write_domain = obj->base.pending_write_domain;
  797. if (obj->base.write_domain == 0)
  798. obj->base.pending_read_domains |= obj->base.read_domains;
  799. obj->base.read_domains = obj->base.pending_read_domains;
  800. i915_vma_move_to_active(vma, ring);
  801. if (obj->base.write_domain) {
  802. obj->dirty = 1;
  803. obj->last_write_seqno = seqno;
  804. intel_fb_obj_invalidate(obj, ring);
  805. /* update for the implicit flush after a batch */
  806. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  807. }
  808. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  809. obj->last_fenced_seqno = seqno;
  810. if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
  811. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  812. list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
  813. &dev_priv->mm.fence_list);
  814. }
  815. }
  816. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  817. }
  818. }
  819. static void
  820. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  821. struct drm_file *file,
  822. struct intel_engine_cs *ring,
  823. struct drm_i915_gem_object *obj)
  824. {
  825. /* Unconditionally force add_request to emit a full flush. */
  826. ring->gpu_caches_dirty = true;
  827. /* Add a breadcrumb for the completion of the batch buffer */
  828. (void)__i915_add_request(ring, file, obj, NULL);
  829. }
  830. static int
  831. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  832. struct intel_engine_cs *ring)
  833. {
  834. struct drm_i915_private *dev_priv = dev->dev_private;
  835. int ret, i;
  836. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
  837. DRM_DEBUG("sol reset is gen7/rcs only\n");
  838. return -EINVAL;
  839. }
  840. ret = intel_ring_begin(ring, 4 * 3);
  841. if (ret)
  842. return ret;
  843. for (i = 0; i < 4; i++) {
  844. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  845. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  846. intel_ring_emit(ring, 0);
  847. }
  848. intel_ring_advance(ring);
  849. return 0;
  850. }
  851. static int
  852. legacy_ringbuffer_submission(struct drm_device *dev, struct drm_file *file,
  853. struct intel_engine_cs *ring,
  854. struct intel_context *ctx,
  855. struct drm_i915_gem_execbuffer2 *args,
  856. struct list_head *vmas,
  857. struct drm_i915_gem_object *batch_obj,
  858. u64 exec_start, u32 flags)
  859. {
  860. struct drm_clip_rect *cliprects = NULL;
  861. struct drm_i915_private *dev_priv = dev->dev_private;
  862. u64 exec_len;
  863. int instp_mode;
  864. u32 instp_mask;
  865. int i, ret = 0;
  866. if (args->num_cliprects != 0) {
  867. if (ring != &dev_priv->ring[RCS]) {
  868. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  869. return -EINVAL;
  870. }
  871. if (INTEL_INFO(dev)->gen >= 5) {
  872. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  873. return -EINVAL;
  874. }
  875. if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  876. DRM_DEBUG("execbuf with %u cliprects\n",
  877. args->num_cliprects);
  878. return -EINVAL;
  879. }
  880. cliprects = kcalloc(args->num_cliprects,
  881. sizeof(*cliprects),
  882. GFP_KERNEL);
  883. if (cliprects == NULL) {
  884. ret = -ENOMEM;
  885. goto error;
  886. }
  887. if (copy_from_user(cliprects,
  888. to_user_ptr(args->cliprects_ptr),
  889. sizeof(*cliprects)*args->num_cliprects)) {
  890. ret = -EFAULT;
  891. goto error;
  892. }
  893. } else {
  894. if (args->DR4 == 0xffffffff) {
  895. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  896. args->DR4 = 0;
  897. }
  898. if (args->DR1 || args->DR4 || args->cliprects_ptr) {
  899. DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
  900. return -EINVAL;
  901. }
  902. }
  903. ret = i915_gem_execbuffer_move_to_gpu(ring, vmas);
  904. if (ret)
  905. goto error;
  906. ret = i915_switch_context(ring, ctx);
  907. if (ret)
  908. goto error;
  909. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  910. instp_mask = I915_EXEC_CONSTANTS_MASK;
  911. switch (instp_mode) {
  912. case I915_EXEC_CONSTANTS_REL_GENERAL:
  913. case I915_EXEC_CONSTANTS_ABSOLUTE:
  914. case I915_EXEC_CONSTANTS_REL_SURFACE:
  915. if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
  916. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  917. ret = -EINVAL;
  918. goto error;
  919. }
  920. if (instp_mode != dev_priv->relative_constants_mode) {
  921. if (INTEL_INFO(dev)->gen < 4) {
  922. DRM_DEBUG("no rel constants on pre-gen4\n");
  923. ret = -EINVAL;
  924. goto error;
  925. }
  926. if (INTEL_INFO(dev)->gen > 5 &&
  927. instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  928. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  929. ret = -EINVAL;
  930. goto error;
  931. }
  932. /* The HW changed the meaning on this bit on gen6 */
  933. if (INTEL_INFO(dev)->gen >= 6)
  934. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  935. }
  936. break;
  937. default:
  938. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  939. ret = -EINVAL;
  940. goto error;
  941. }
  942. if (ring == &dev_priv->ring[RCS] &&
  943. instp_mode != dev_priv->relative_constants_mode) {
  944. ret = intel_ring_begin(ring, 4);
  945. if (ret)
  946. goto error;
  947. intel_ring_emit(ring, MI_NOOP);
  948. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  949. intel_ring_emit(ring, INSTPM);
  950. intel_ring_emit(ring, instp_mask << 16 | instp_mode);
  951. intel_ring_advance(ring);
  952. dev_priv->relative_constants_mode = instp_mode;
  953. }
  954. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  955. ret = i915_reset_gen7_sol_offsets(dev, ring);
  956. if (ret)
  957. goto error;
  958. }
  959. exec_len = args->batch_len;
  960. if (cliprects) {
  961. for (i = 0; i < args->num_cliprects; i++) {
  962. ret = i915_emit_box(dev, &cliprects[i],
  963. args->DR1, args->DR4);
  964. if (ret)
  965. goto error;
  966. ret = ring->dispatch_execbuffer(ring,
  967. exec_start, exec_len,
  968. flags);
  969. if (ret)
  970. goto error;
  971. }
  972. } else {
  973. ret = ring->dispatch_execbuffer(ring,
  974. exec_start, exec_len,
  975. flags);
  976. if (ret)
  977. return ret;
  978. }
  979. trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
  980. i915_gem_execbuffer_move_to_active(vmas, ring);
  981. i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  982. error:
  983. kfree(cliprects);
  984. return ret;
  985. }
  986. /**
  987. * Find one BSD ring to dispatch the corresponding BSD command.
  988. * The Ring ID is returned.
  989. */
  990. static int gen8_dispatch_bsd_ring(struct drm_device *dev,
  991. struct drm_file *file)
  992. {
  993. struct drm_i915_private *dev_priv = dev->dev_private;
  994. struct drm_i915_file_private *file_priv = file->driver_priv;
  995. /* Check whether the file_priv is using one ring */
  996. if (file_priv->bsd_ring)
  997. return file_priv->bsd_ring->id;
  998. else {
  999. /* If no, use the ping-pong mechanism to select one ring */
  1000. int ring_id;
  1001. mutex_lock(&dev->struct_mutex);
  1002. if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
  1003. ring_id = VCS;
  1004. dev_priv->mm.bsd_ring_dispatch_index = 1;
  1005. } else {
  1006. ring_id = VCS2;
  1007. dev_priv->mm.bsd_ring_dispatch_index = 0;
  1008. }
  1009. file_priv->bsd_ring = &dev_priv->ring[ring_id];
  1010. mutex_unlock(&dev->struct_mutex);
  1011. return ring_id;
  1012. }
  1013. }
  1014. static struct drm_i915_gem_object *
  1015. eb_get_batch(struct eb_vmas *eb)
  1016. {
  1017. struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
  1018. /*
  1019. * SNA is doing fancy tricks with compressing batch buffers, which leads
  1020. * to negative relocation deltas. Usually that works out ok since the
  1021. * relocate address is still positive, except when the batch is placed
  1022. * very low in the GTT. Ensure this doesn't happen.
  1023. *
  1024. * Note that actual hangs have only been observed on gen7, but for
  1025. * paranoia do it everywhere.
  1026. */
  1027. vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  1028. return vma->obj;
  1029. }
  1030. static int
  1031. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  1032. struct drm_file *file,
  1033. struct drm_i915_gem_execbuffer2 *args,
  1034. struct drm_i915_gem_exec_object2 *exec)
  1035. {
  1036. struct drm_i915_private *dev_priv = dev->dev_private;
  1037. struct eb_vmas *eb;
  1038. struct drm_i915_gem_object *batch_obj;
  1039. struct intel_engine_cs *ring;
  1040. struct intel_context *ctx;
  1041. struct i915_address_space *vm;
  1042. const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  1043. u64 exec_start = args->batch_start_offset;
  1044. u32 flags;
  1045. int ret;
  1046. bool need_relocs;
  1047. if (!i915_gem_check_execbuffer(args))
  1048. return -EINVAL;
  1049. ret = validate_exec_list(dev, exec, args->buffer_count);
  1050. if (ret)
  1051. return ret;
  1052. flags = 0;
  1053. if (args->flags & I915_EXEC_SECURE) {
  1054. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  1055. return -EPERM;
  1056. flags |= I915_DISPATCH_SECURE;
  1057. }
  1058. if (args->flags & I915_EXEC_IS_PINNED)
  1059. flags |= I915_DISPATCH_PINNED;
  1060. if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
  1061. DRM_DEBUG("execbuf with unknown ring: %d\n",
  1062. (int)(args->flags & I915_EXEC_RING_MASK));
  1063. return -EINVAL;
  1064. }
  1065. if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
  1066. ring = &dev_priv->ring[RCS];
  1067. else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
  1068. if (HAS_BSD2(dev)) {
  1069. int ring_id;
  1070. ring_id = gen8_dispatch_bsd_ring(dev, file);
  1071. ring = &dev_priv->ring[ring_id];
  1072. } else
  1073. ring = &dev_priv->ring[VCS];
  1074. } else
  1075. ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
  1076. if (!intel_ring_initialized(ring)) {
  1077. DRM_DEBUG("execbuf with invalid ring: %d\n",
  1078. (int)(args->flags & I915_EXEC_RING_MASK));
  1079. return -EINVAL;
  1080. }
  1081. if (args->buffer_count < 1) {
  1082. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1083. return -EINVAL;
  1084. }
  1085. intel_runtime_pm_get(dev_priv);
  1086. ret = i915_mutex_lock_interruptible(dev);
  1087. if (ret)
  1088. goto pre_mutex_err;
  1089. if (dev_priv->ums.mm_suspended) {
  1090. mutex_unlock(&dev->struct_mutex);
  1091. ret = -EBUSY;
  1092. goto pre_mutex_err;
  1093. }
  1094. ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
  1095. if (IS_ERR(ctx)) {
  1096. mutex_unlock(&dev->struct_mutex);
  1097. ret = PTR_ERR(ctx);
  1098. goto pre_mutex_err;
  1099. }
  1100. i915_gem_context_reference(ctx);
  1101. vm = ctx->vm;
  1102. if (!USES_FULL_PPGTT(dev))
  1103. vm = &dev_priv->gtt.base;
  1104. eb = eb_create(args);
  1105. if (eb == NULL) {
  1106. i915_gem_context_unreference(ctx);
  1107. mutex_unlock(&dev->struct_mutex);
  1108. ret = -ENOMEM;
  1109. goto pre_mutex_err;
  1110. }
  1111. /* Look up object handles */
  1112. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  1113. if (ret)
  1114. goto err;
  1115. /* take note of the batch buffer before we might reorder the lists */
  1116. batch_obj = eb_get_batch(eb);
  1117. /* Move the objects en-masse into the GTT, evicting if necessary. */
  1118. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  1119. ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
  1120. if (ret)
  1121. goto err;
  1122. /* The objects are in their final locations, apply the relocations. */
  1123. if (need_relocs)
  1124. ret = i915_gem_execbuffer_relocate(eb);
  1125. if (ret) {
  1126. if (ret == -EFAULT) {
  1127. ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
  1128. eb, exec);
  1129. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1130. }
  1131. if (ret)
  1132. goto err;
  1133. }
  1134. /* Set the pending read domains for the batch buffer to COMMAND */
  1135. if (batch_obj->base.pending_write_domain) {
  1136. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1137. ret = -EINVAL;
  1138. goto err;
  1139. }
  1140. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1141. if (i915_needs_cmd_parser(ring)) {
  1142. ret = i915_parse_cmds(ring,
  1143. batch_obj,
  1144. args->batch_start_offset,
  1145. file->is_master);
  1146. if (ret)
  1147. goto err;
  1148. /*
  1149. * XXX: Actually do this when enabling batch copy...
  1150. *
  1151. * Set the DISPATCH_SECURE bit to remove the NON_SECURE bit
  1152. * from MI_BATCH_BUFFER_START commands issued in the
  1153. * dispatch_execbuffer implementations. We specifically don't
  1154. * want that set when the command parser is enabled.
  1155. */
  1156. }
  1157. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1158. * batch" bit. Hence we need to pin secure batches into the global gtt.
  1159. * hsw should have this fixed, but bdw mucks it up again. */
  1160. if (flags & I915_DISPATCH_SECURE) {
  1161. /*
  1162. * So on first glance it looks freaky that we pin the batch here
  1163. * outside of the reservation loop. But:
  1164. * - The batch is already pinned into the relevant ppgtt, so we
  1165. * already have the backing storage fully allocated.
  1166. * - No other BO uses the global gtt (well contexts, but meh),
  1167. * so we don't really have issues with mutliple objects not
  1168. * fitting due to fragmentation.
  1169. * So this is actually safe.
  1170. */
  1171. ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
  1172. if (ret)
  1173. goto err;
  1174. exec_start += i915_gem_obj_ggtt_offset(batch_obj);
  1175. } else
  1176. exec_start += i915_gem_obj_offset(batch_obj, vm);
  1177. ret = legacy_ringbuffer_submission(dev, file, ring, ctx,
  1178. args, &eb->vmas, batch_obj, exec_start, flags);
  1179. /*
  1180. * FIXME: We crucially rely upon the active tracking for the (ppgtt)
  1181. * batch vma for correctness. For less ugly and less fragility this
  1182. * needs to be adjusted to also track the ggtt batch vma properly as
  1183. * active.
  1184. */
  1185. if (flags & I915_DISPATCH_SECURE)
  1186. i915_gem_object_ggtt_unpin(batch_obj);
  1187. err:
  1188. /* the request owns the ref now */
  1189. i915_gem_context_unreference(ctx);
  1190. eb_destroy(eb);
  1191. mutex_unlock(&dev->struct_mutex);
  1192. pre_mutex_err:
  1193. /* intel_gpu_busy should also get a ref, so it will free when the device
  1194. * is really idle. */
  1195. intel_runtime_pm_put(dev_priv);
  1196. return ret;
  1197. }
  1198. /*
  1199. * Legacy execbuffer just creates an exec2 list from the original exec object
  1200. * list array and passes it to the real function.
  1201. */
  1202. int
  1203. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1204. struct drm_file *file)
  1205. {
  1206. struct drm_i915_gem_execbuffer *args = data;
  1207. struct drm_i915_gem_execbuffer2 exec2;
  1208. struct drm_i915_gem_exec_object *exec_list = NULL;
  1209. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1210. int ret, i;
  1211. if (args->buffer_count < 1) {
  1212. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1213. return -EINVAL;
  1214. }
  1215. /* Copy in the exec list from userland */
  1216. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1217. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1218. if (exec_list == NULL || exec2_list == NULL) {
  1219. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1220. args->buffer_count);
  1221. drm_free_large(exec_list);
  1222. drm_free_large(exec2_list);
  1223. return -ENOMEM;
  1224. }
  1225. ret = copy_from_user(exec_list,
  1226. to_user_ptr(args->buffers_ptr),
  1227. sizeof(*exec_list) * args->buffer_count);
  1228. if (ret != 0) {
  1229. DRM_DEBUG("copy %d exec entries failed %d\n",
  1230. args->buffer_count, ret);
  1231. drm_free_large(exec_list);
  1232. drm_free_large(exec2_list);
  1233. return -EFAULT;
  1234. }
  1235. for (i = 0; i < args->buffer_count; i++) {
  1236. exec2_list[i].handle = exec_list[i].handle;
  1237. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1238. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1239. exec2_list[i].alignment = exec_list[i].alignment;
  1240. exec2_list[i].offset = exec_list[i].offset;
  1241. if (INTEL_INFO(dev)->gen < 4)
  1242. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1243. else
  1244. exec2_list[i].flags = 0;
  1245. }
  1246. exec2.buffers_ptr = args->buffers_ptr;
  1247. exec2.buffer_count = args->buffer_count;
  1248. exec2.batch_start_offset = args->batch_start_offset;
  1249. exec2.batch_len = args->batch_len;
  1250. exec2.DR1 = args->DR1;
  1251. exec2.DR4 = args->DR4;
  1252. exec2.num_cliprects = args->num_cliprects;
  1253. exec2.cliprects_ptr = args->cliprects_ptr;
  1254. exec2.flags = I915_EXEC_RENDER;
  1255. i915_execbuffer2_set_context_id(exec2, 0);
  1256. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1257. if (!ret) {
  1258. struct drm_i915_gem_exec_object __user *user_exec_list =
  1259. to_user_ptr(args->buffers_ptr);
  1260. /* Copy the new buffer offsets back to the user's exec list. */
  1261. for (i = 0; i < args->buffer_count; i++) {
  1262. ret = __copy_to_user(&user_exec_list[i].offset,
  1263. &exec2_list[i].offset,
  1264. sizeof(user_exec_list[i].offset));
  1265. if (ret) {
  1266. ret = -EFAULT;
  1267. DRM_DEBUG("failed to copy %d exec entries "
  1268. "back to user (%d)\n",
  1269. args->buffer_count, ret);
  1270. break;
  1271. }
  1272. }
  1273. }
  1274. drm_free_large(exec_list);
  1275. drm_free_large(exec2_list);
  1276. return ret;
  1277. }
  1278. int
  1279. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1280. struct drm_file *file)
  1281. {
  1282. struct drm_i915_gem_execbuffer2 *args = data;
  1283. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1284. int ret;
  1285. if (args->buffer_count < 1 ||
  1286. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1287. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1288. return -EINVAL;
  1289. }
  1290. if (args->rsvd2 != 0) {
  1291. DRM_DEBUG("dirty rvsd2 field\n");
  1292. return -EINVAL;
  1293. }
  1294. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1295. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  1296. if (exec2_list == NULL)
  1297. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1298. args->buffer_count);
  1299. if (exec2_list == NULL) {
  1300. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1301. args->buffer_count);
  1302. return -ENOMEM;
  1303. }
  1304. ret = copy_from_user(exec2_list,
  1305. to_user_ptr(args->buffers_ptr),
  1306. sizeof(*exec2_list) * args->buffer_count);
  1307. if (ret != 0) {
  1308. DRM_DEBUG("copy %d exec entries failed %d\n",
  1309. args->buffer_count, ret);
  1310. drm_free_large(exec2_list);
  1311. return -EFAULT;
  1312. }
  1313. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1314. if (!ret) {
  1315. /* Copy the new buffer offsets back to the user's exec list. */
  1316. struct drm_i915_gem_exec_object2 __user *user_exec_list =
  1317. to_user_ptr(args->buffers_ptr);
  1318. int i;
  1319. for (i = 0; i < args->buffer_count; i++) {
  1320. ret = __copy_to_user(&user_exec_list[i].offset,
  1321. &exec2_list[i].offset,
  1322. sizeof(user_exec_list[i].offset));
  1323. if (ret) {
  1324. ret = -EFAULT;
  1325. DRM_DEBUG("failed to copy %d exec entries "
  1326. "back to user\n",
  1327. args->buffer_count);
  1328. break;
  1329. }
  1330. }
  1331. }
  1332. drm_free_large(exec2_list);
  1333. return ret;
  1334. }