i915_gem_execbuffer.c 35 KB

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