i915_gem_execbuffer.c 36 KB

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