i915_gem_execbuffer.c 34 KB

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