i915_gem_execbuffer.c 39 KB

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