i915_gem_execbuffer.c 36 KB

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