i915_gem_execbuffer.c 34 KB

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