i915_gem_execbuffer.c 34 KB

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