i915_gem_execbuffer.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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. int ret;
  604. list_for_each_entry(obj, objects, exec_list) {
  605. ret = i915_gem_object_sync(obj, ring);
  606. if (ret)
  607. return ret;
  608. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  609. i915_gem_clflush_object(obj);
  610. flush_domains |= obj->base.write_domain;
  611. }
  612. if (flush_domains & I915_GEM_DOMAIN_CPU)
  613. i915_gem_chipset_flush(ring->dev);
  614. if (flush_domains & I915_GEM_DOMAIN_GTT)
  615. wmb();
  616. /* Unconditionally invalidate gpu caches and ensure that we do flush
  617. * any residual writes from the previous batch.
  618. */
  619. return intel_ring_invalidate_all_caches(ring);
  620. }
  621. static bool
  622. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  623. {
  624. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  625. return false;
  626. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  627. }
  628. static int
  629. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  630. int count)
  631. {
  632. int i;
  633. int relocs_total = 0;
  634. int relocs_max = INT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  635. for (i = 0; i < count; i++) {
  636. char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
  637. int length; /* limited by fault_in_pages_readable() */
  638. if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
  639. return -EINVAL;
  640. /* First check for malicious input causing overflow in
  641. * the worst case where we need to allocate the entire
  642. * relocation tree as a single array.
  643. */
  644. if (exec[i].relocation_count > relocs_max - relocs_total)
  645. return -EINVAL;
  646. relocs_total += exec[i].relocation_count;
  647. length = exec[i].relocation_count *
  648. sizeof(struct drm_i915_gem_relocation_entry);
  649. /*
  650. * We must check that the entire relocation array is safe
  651. * to read, but since we may need to update the presumed
  652. * offsets during execution, check for full write access.
  653. */
  654. if (!access_ok(VERIFY_WRITE, ptr, length))
  655. return -EFAULT;
  656. if (likely(!i915_prefault_disable)) {
  657. if (fault_in_multipages_readable(ptr, length))
  658. return -EFAULT;
  659. }
  660. }
  661. return 0;
  662. }
  663. static void
  664. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  665. struct i915_address_space *vm,
  666. struct intel_ring_buffer *ring)
  667. {
  668. struct drm_i915_gem_object *obj;
  669. list_for_each_entry(obj, objects, exec_list) {
  670. u32 old_read = obj->base.read_domains;
  671. u32 old_write = obj->base.write_domain;
  672. obj->base.write_domain = obj->base.pending_write_domain;
  673. if (obj->base.write_domain == 0)
  674. obj->base.pending_read_domains |= obj->base.read_domains;
  675. obj->base.read_domains = obj->base.pending_read_domains;
  676. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  677. /* FIXME: This lookup gets fixed later <-- danvet */
  678. list_move_tail(&i915_gem_obj_to_vma(obj, vm)->mm_list, &vm->active_list);
  679. i915_gem_object_move_to_active(obj, ring);
  680. if (obj->base.write_domain) {
  681. obj->dirty = 1;
  682. obj->last_write_seqno = intel_ring_get_seqno(ring);
  683. if (obj->pin_count) /* check for potential scanout */
  684. intel_mark_fb_busy(obj, ring);
  685. }
  686. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  687. }
  688. }
  689. static void
  690. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  691. struct drm_file *file,
  692. struct intel_ring_buffer *ring,
  693. struct drm_i915_gem_object *obj)
  694. {
  695. /* Unconditionally force add_request to emit a full flush. */
  696. ring->gpu_caches_dirty = true;
  697. /* Add a breadcrumb for the completion of the batch buffer */
  698. (void)__i915_add_request(ring, file, obj, NULL);
  699. }
  700. static int
  701. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  702. struct intel_ring_buffer *ring)
  703. {
  704. drm_i915_private_t *dev_priv = dev->dev_private;
  705. int ret, i;
  706. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
  707. return 0;
  708. ret = intel_ring_begin(ring, 4 * 3);
  709. if (ret)
  710. return ret;
  711. for (i = 0; i < 4; i++) {
  712. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  713. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  714. intel_ring_emit(ring, 0);
  715. }
  716. intel_ring_advance(ring);
  717. return 0;
  718. }
  719. static int
  720. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  721. struct drm_file *file,
  722. struct drm_i915_gem_execbuffer2 *args,
  723. struct drm_i915_gem_exec_object2 *exec,
  724. struct i915_address_space *vm)
  725. {
  726. drm_i915_private_t *dev_priv = dev->dev_private;
  727. struct eb_objects *eb;
  728. struct drm_i915_gem_object *batch_obj;
  729. struct drm_clip_rect *cliprects = NULL;
  730. struct intel_ring_buffer *ring;
  731. u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  732. u32 exec_start, exec_len;
  733. u32 mask, flags;
  734. int ret, mode, i;
  735. bool need_relocs;
  736. if (!i915_gem_check_execbuffer(args))
  737. return -EINVAL;
  738. ret = validate_exec_list(exec, args->buffer_count);
  739. if (ret)
  740. return ret;
  741. flags = 0;
  742. if (args->flags & I915_EXEC_SECURE) {
  743. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  744. return -EPERM;
  745. flags |= I915_DISPATCH_SECURE;
  746. }
  747. if (args->flags & I915_EXEC_IS_PINNED)
  748. flags |= I915_DISPATCH_PINNED;
  749. switch (args->flags & I915_EXEC_RING_MASK) {
  750. case I915_EXEC_DEFAULT:
  751. case I915_EXEC_RENDER:
  752. ring = &dev_priv->ring[RCS];
  753. break;
  754. case I915_EXEC_BSD:
  755. ring = &dev_priv->ring[VCS];
  756. if (ctx_id != DEFAULT_CONTEXT_ID) {
  757. DRM_DEBUG("Ring %s doesn't support contexts\n",
  758. ring->name);
  759. return -EPERM;
  760. }
  761. break;
  762. case I915_EXEC_BLT:
  763. ring = &dev_priv->ring[BCS];
  764. if (ctx_id != DEFAULT_CONTEXT_ID) {
  765. DRM_DEBUG("Ring %s doesn't support contexts\n",
  766. ring->name);
  767. return -EPERM;
  768. }
  769. break;
  770. case I915_EXEC_VEBOX:
  771. ring = &dev_priv->ring[VECS];
  772. if (ctx_id != DEFAULT_CONTEXT_ID) {
  773. DRM_DEBUG("Ring %s doesn't support contexts\n",
  774. ring->name);
  775. return -EPERM;
  776. }
  777. break;
  778. default:
  779. DRM_DEBUG("execbuf with unknown ring: %d\n",
  780. (int)(args->flags & I915_EXEC_RING_MASK));
  781. return -EINVAL;
  782. }
  783. if (!intel_ring_initialized(ring)) {
  784. DRM_DEBUG("execbuf with invalid ring: %d\n",
  785. (int)(args->flags & I915_EXEC_RING_MASK));
  786. return -EINVAL;
  787. }
  788. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  789. mask = I915_EXEC_CONSTANTS_MASK;
  790. switch (mode) {
  791. case I915_EXEC_CONSTANTS_REL_GENERAL:
  792. case I915_EXEC_CONSTANTS_ABSOLUTE:
  793. case I915_EXEC_CONSTANTS_REL_SURFACE:
  794. if (ring == &dev_priv->ring[RCS] &&
  795. mode != dev_priv->relative_constants_mode) {
  796. if (INTEL_INFO(dev)->gen < 4)
  797. return -EINVAL;
  798. if (INTEL_INFO(dev)->gen > 5 &&
  799. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  800. return -EINVAL;
  801. /* The HW changed the meaning on this bit on gen6 */
  802. if (INTEL_INFO(dev)->gen >= 6)
  803. mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  804. }
  805. break;
  806. default:
  807. DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
  808. return -EINVAL;
  809. }
  810. if (args->buffer_count < 1) {
  811. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  812. return -EINVAL;
  813. }
  814. if (args->num_cliprects != 0) {
  815. if (ring != &dev_priv->ring[RCS]) {
  816. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  817. return -EINVAL;
  818. }
  819. if (INTEL_INFO(dev)->gen >= 5) {
  820. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  821. return -EINVAL;
  822. }
  823. if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  824. DRM_DEBUG("execbuf with %u cliprects\n",
  825. args->num_cliprects);
  826. return -EINVAL;
  827. }
  828. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  829. GFP_KERNEL);
  830. if (cliprects == NULL) {
  831. ret = -ENOMEM;
  832. goto pre_mutex_err;
  833. }
  834. if (copy_from_user(cliprects,
  835. to_user_ptr(args->cliprects_ptr),
  836. sizeof(*cliprects)*args->num_cliprects)) {
  837. ret = -EFAULT;
  838. goto pre_mutex_err;
  839. }
  840. }
  841. ret = i915_mutex_lock_interruptible(dev);
  842. if (ret)
  843. goto pre_mutex_err;
  844. if (dev_priv->ums.mm_suspended) {
  845. mutex_unlock(&dev->struct_mutex);
  846. ret = -EBUSY;
  847. goto pre_mutex_err;
  848. }
  849. eb = eb_create(args);
  850. if (eb == NULL) {
  851. mutex_unlock(&dev->struct_mutex);
  852. ret = -ENOMEM;
  853. goto pre_mutex_err;
  854. }
  855. /* Look up object handles */
  856. ret = eb_lookup_objects(eb, exec, args, file);
  857. if (ret)
  858. goto err;
  859. /* take note of the batch buffer before we might reorder the lists */
  860. batch_obj = list_entry(eb->objects.prev,
  861. struct drm_i915_gem_object,
  862. exec_list);
  863. /* Move the objects en-masse into the GTT, evicting if necessary. */
  864. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  865. ret = i915_gem_execbuffer_reserve(ring, &eb->objects, vm, &need_relocs);
  866. if (ret)
  867. goto err;
  868. /* The objects are in their final locations, apply the relocations. */
  869. if (need_relocs)
  870. ret = i915_gem_execbuffer_relocate(eb, vm);
  871. if (ret) {
  872. if (ret == -EFAULT) {
  873. ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
  874. eb, exec, vm);
  875. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  876. }
  877. if (ret)
  878. goto err;
  879. }
  880. /* Set the pending read domains for the batch buffer to COMMAND */
  881. if (batch_obj->base.pending_write_domain) {
  882. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  883. ret = -EINVAL;
  884. goto err;
  885. }
  886. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  887. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  888. * batch" bit. Hence we need to pin secure batches into the global gtt.
  889. * hsw should have this fixed, but let's be paranoid and do it
  890. * unconditionally for now. */
  891. if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
  892. i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
  893. ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->objects);
  894. if (ret)
  895. goto err;
  896. ret = i915_switch_context(ring, file, ctx_id);
  897. if (ret)
  898. goto err;
  899. if (ring == &dev_priv->ring[RCS] &&
  900. mode != dev_priv->relative_constants_mode) {
  901. ret = intel_ring_begin(ring, 4);
  902. if (ret)
  903. goto err;
  904. intel_ring_emit(ring, MI_NOOP);
  905. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  906. intel_ring_emit(ring, INSTPM);
  907. intel_ring_emit(ring, mask << 16 | mode);
  908. intel_ring_advance(ring);
  909. dev_priv->relative_constants_mode = mode;
  910. }
  911. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  912. ret = i915_reset_gen7_sol_offsets(dev, ring);
  913. if (ret)
  914. goto err;
  915. }
  916. exec_start = i915_gem_obj_offset(batch_obj, vm) +
  917. args->batch_start_offset;
  918. exec_len = args->batch_len;
  919. if (cliprects) {
  920. for (i = 0; i < args->num_cliprects; i++) {
  921. ret = i915_emit_box(dev, &cliprects[i],
  922. args->DR1, args->DR4);
  923. if (ret)
  924. goto err;
  925. ret = ring->dispatch_execbuffer(ring,
  926. exec_start, exec_len,
  927. flags);
  928. if (ret)
  929. goto err;
  930. }
  931. } else {
  932. ret = ring->dispatch_execbuffer(ring,
  933. exec_start, exec_len,
  934. flags);
  935. if (ret)
  936. goto err;
  937. }
  938. trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
  939. i915_gem_execbuffer_move_to_active(&eb->objects, vm, ring);
  940. i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  941. err:
  942. eb_destroy(eb);
  943. mutex_unlock(&dev->struct_mutex);
  944. pre_mutex_err:
  945. kfree(cliprects);
  946. return ret;
  947. }
  948. /*
  949. * Legacy execbuffer just creates an exec2 list from the original exec object
  950. * list array and passes it to the real function.
  951. */
  952. int
  953. i915_gem_execbuffer(struct drm_device *dev, void *data,
  954. struct drm_file *file)
  955. {
  956. struct drm_i915_private *dev_priv = dev->dev_private;
  957. struct drm_i915_gem_execbuffer *args = data;
  958. struct drm_i915_gem_execbuffer2 exec2;
  959. struct drm_i915_gem_exec_object *exec_list = NULL;
  960. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  961. int ret, i;
  962. if (args->buffer_count < 1) {
  963. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  964. return -EINVAL;
  965. }
  966. /* Copy in the exec list from userland */
  967. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  968. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  969. if (exec_list == NULL || exec2_list == NULL) {
  970. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  971. args->buffer_count);
  972. drm_free_large(exec_list);
  973. drm_free_large(exec2_list);
  974. return -ENOMEM;
  975. }
  976. ret = copy_from_user(exec_list,
  977. to_user_ptr(args->buffers_ptr),
  978. sizeof(*exec_list) * args->buffer_count);
  979. if (ret != 0) {
  980. DRM_DEBUG("copy %d exec entries failed %d\n",
  981. args->buffer_count, ret);
  982. drm_free_large(exec_list);
  983. drm_free_large(exec2_list);
  984. return -EFAULT;
  985. }
  986. for (i = 0; i < args->buffer_count; i++) {
  987. exec2_list[i].handle = exec_list[i].handle;
  988. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  989. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  990. exec2_list[i].alignment = exec_list[i].alignment;
  991. exec2_list[i].offset = exec_list[i].offset;
  992. if (INTEL_INFO(dev)->gen < 4)
  993. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  994. else
  995. exec2_list[i].flags = 0;
  996. }
  997. exec2.buffers_ptr = args->buffers_ptr;
  998. exec2.buffer_count = args->buffer_count;
  999. exec2.batch_start_offset = args->batch_start_offset;
  1000. exec2.batch_len = args->batch_len;
  1001. exec2.DR1 = args->DR1;
  1002. exec2.DR4 = args->DR4;
  1003. exec2.num_cliprects = args->num_cliprects;
  1004. exec2.cliprects_ptr = args->cliprects_ptr;
  1005. exec2.flags = I915_EXEC_RENDER;
  1006. i915_execbuffer2_set_context_id(exec2, 0);
  1007. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list,
  1008. &dev_priv->gtt.base);
  1009. if (!ret) {
  1010. /* Copy the new buffer offsets back to the user's exec list. */
  1011. for (i = 0; i < args->buffer_count; i++)
  1012. exec_list[i].offset = exec2_list[i].offset;
  1013. /* ... and back out to userspace */
  1014. ret = copy_to_user(to_user_ptr(args->buffers_ptr),
  1015. exec_list,
  1016. sizeof(*exec_list) * args->buffer_count);
  1017. if (ret) {
  1018. ret = -EFAULT;
  1019. DRM_DEBUG("failed to copy %d exec entries "
  1020. "back to user (%d)\n",
  1021. args->buffer_count, ret);
  1022. }
  1023. }
  1024. drm_free_large(exec_list);
  1025. drm_free_large(exec2_list);
  1026. return ret;
  1027. }
  1028. int
  1029. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1030. struct drm_file *file)
  1031. {
  1032. struct drm_i915_private *dev_priv = dev->dev_private;
  1033. struct drm_i915_gem_execbuffer2 *args = data;
  1034. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1035. int ret;
  1036. if (args->buffer_count < 1 ||
  1037. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1038. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1039. return -EINVAL;
  1040. }
  1041. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1042. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  1043. if (exec2_list == NULL)
  1044. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1045. args->buffer_count);
  1046. if (exec2_list == NULL) {
  1047. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1048. args->buffer_count);
  1049. return -ENOMEM;
  1050. }
  1051. ret = copy_from_user(exec2_list,
  1052. to_user_ptr(args->buffers_ptr),
  1053. sizeof(*exec2_list) * args->buffer_count);
  1054. if (ret != 0) {
  1055. DRM_DEBUG("copy %d exec entries failed %d\n",
  1056. args->buffer_count, ret);
  1057. drm_free_large(exec2_list);
  1058. return -EFAULT;
  1059. }
  1060. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list,
  1061. &dev_priv->gtt.base);
  1062. if (!ret) {
  1063. /* Copy the new buffer offsets back to the user's exec list. */
  1064. ret = copy_to_user(to_user_ptr(args->buffers_ptr),
  1065. exec2_list,
  1066. sizeof(*exec2_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(exec2_list);
  1075. return ret;
  1076. }