i915_gem_execbuffer.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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 "drmP.h"
  29. #include "drm.h"
  30. #include "i915_drm.h"
  31. #include "i915_drv.h"
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. #include <linux/dma_remapping.h>
  35. struct change_domains {
  36. uint32_t invalidate_domains;
  37. uint32_t flush_domains;
  38. uint32_t flush_rings;
  39. uint32_t flips;
  40. };
  41. /*
  42. * Set the next domain for the specified object. This
  43. * may not actually perform the necessary flushing/invaliding though,
  44. * as that may want to be batched with other set_domain operations
  45. *
  46. * This is (we hope) the only really tricky part of gem. The goal
  47. * is fairly simple -- track which caches hold bits of the object
  48. * and make sure they remain coherent. A few concrete examples may
  49. * help to explain how it works. For shorthand, we use the notation
  50. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  51. * a pair of read and write domain masks.
  52. *
  53. * Case 1: the batch buffer
  54. *
  55. * 1. Allocated
  56. * 2. Written by CPU
  57. * 3. Mapped to GTT
  58. * 4. Read by GPU
  59. * 5. Unmapped from GTT
  60. * 6. Freed
  61. *
  62. * Let's take these a step at a time
  63. *
  64. * 1. Allocated
  65. * Pages allocated from the kernel may still have
  66. * cache contents, so we set them to (CPU, CPU) always.
  67. * 2. Written by CPU (using pwrite)
  68. * The pwrite function calls set_domain (CPU, CPU) and
  69. * this function does nothing (as nothing changes)
  70. * 3. Mapped by GTT
  71. * This function asserts that the object is not
  72. * currently in any GPU-based read or write domains
  73. * 4. Read by GPU
  74. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  75. * As write_domain is zero, this function adds in the
  76. * current read domains (CPU+COMMAND, 0).
  77. * flush_domains is set to CPU.
  78. * invalidate_domains is set to COMMAND
  79. * clflush is run to get data out of the CPU caches
  80. * then i915_dev_set_domain calls i915_gem_flush to
  81. * emit an MI_FLUSH and drm_agp_chipset_flush
  82. * 5. Unmapped from GTT
  83. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  84. * flush_domains and invalidate_domains end up both zero
  85. * so no flushing/invalidating happens
  86. * 6. Freed
  87. * yay, done
  88. *
  89. * Case 2: The shared render buffer
  90. *
  91. * 1. Allocated
  92. * 2. Mapped to GTT
  93. * 3. Read/written by GPU
  94. * 4. set_domain to (CPU,CPU)
  95. * 5. Read/written by CPU
  96. * 6. Read/written by GPU
  97. *
  98. * 1. Allocated
  99. * Same as last example, (CPU, CPU)
  100. * 2. Mapped to GTT
  101. * Nothing changes (assertions find that it is not in the GPU)
  102. * 3. Read/written by GPU
  103. * execbuffer calls set_domain (RENDER, RENDER)
  104. * flush_domains gets CPU
  105. * invalidate_domains gets GPU
  106. * clflush (obj)
  107. * MI_FLUSH and drm_agp_chipset_flush
  108. * 4. set_domain (CPU, CPU)
  109. * flush_domains gets GPU
  110. * invalidate_domains gets CPU
  111. * wait_rendering (obj) to make sure all drawing is complete.
  112. * This will include an MI_FLUSH to get the data from GPU
  113. * to memory
  114. * clflush (obj) to invalidate the CPU cache
  115. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  116. * 5. Read/written by CPU
  117. * cache lines are loaded and dirtied
  118. * 6. Read written by GPU
  119. * Same as last GPU access
  120. *
  121. * Case 3: The constant buffer
  122. *
  123. * 1. Allocated
  124. * 2. Written by CPU
  125. * 3. Read by GPU
  126. * 4. Updated (written) by CPU again
  127. * 5. Read by GPU
  128. *
  129. * 1. Allocated
  130. * (CPU, CPU)
  131. * 2. Written by CPU
  132. * (CPU, CPU)
  133. * 3. Read by GPU
  134. * (CPU+RENDER, 0)
  135. * flush_domains = CPU
  136. * invalidate_domains = RENDER
  137. * clflush (obj)
  138. * MI_FLUSH
  139. * drm_agp_chipset_flush
  140. * 4. Updated (written) by CPU again
  141. * (CPU, CPU)
  142. * flush_domains = 0 (no previous write domain)
  143. * invalidate_domains = 0 (no new read domains)
  144. * 5. Read by GPU
  145. * (CPU+RENDER, 0)
  146. * flush_domains = CPU
  147. * invalidate_domains = RENDER
  148. * clflush (obj)
  149. * MI_FLUSH
  150. * drm_agp_chipset_flush
  151. */
  152. static void
  153. i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
  154. struct intel_ring_buffer *ring,
  155. struct change_domains *cd)
  156. {
  157. uint32_t invalidate_domains = 0, flush_domains = 0;
  158. /*
  159. * If the object isn't moving to a new write domain,
  160. * let the object stay in multiple read domains
  161. */
  162. if (obj->base.pending_write_domain == 0)
  163. obj->base.pending_read_domains |= obj->base.read_domains;
  164. /*
  165. * Flush the current write domain if
  166. * the new read domains don't match. Invalidate
  167. * any read domains which differ from the old
  168. * write domain
  169. */
  170. if (obj->base.write_domain &&
  171. (((obj->base.write_domain != obj->base.pending_read_domains ||
  172. obj->ring != ring)) ||
  173. (obj->fenced_gpu_access && !obj->pending_fenced_gpu_access))) {
  174. flush_domains |= obj->base.write_domain;
  175. invalidate_domains |=
  176. obj->base.pending_read_domains & ~obj->base.write_domain;
  177. }
  178. /*
  179. * Invalidate any read caches which may have
  180. * stale data. That is, any new read domains.
  181. */
  182. invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
  183. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
  184. i915_gem_clflush_object(obj);
  185. if (obj->base.pending_write_domain)
  186. cd->flips |= atomic_read(&obj->pending_flip);
  187. /* The actual obj->write_domain will be updated with
  188. * pending_write_domain after we emit the accumulated flush for all
  189. * of our domain changes in execbuffers (which clears objects'
  190. * write_domains). So if we have a current write domain that we
  191. * aren't changing, set pending_write_domain to that.
  192. */
  193. if (flush_domains == 0 && obj->base.pending_write_domain == 0)
  194. obj->base.pending_write_domain = obj->base.write_domain;
  195. cd->invalidate_domains |= invalidate_domains;
  196. cd->flush_domains |= flush_domains;
  197. if (flush_domains & I915_GEM_GPU_DOMAINS)
  198. cd->flush_rings |= obj->ring->id;
  199. if (invalidate_domains & I915_GEM_GPU_DOMAINS)
  200. cd->flush_rings |= ring->id;
  201. }
  202. struct eb_objects {
  203. int and;
  204. struct hlist_head buckets[0];
  205. };
  206. static struct eb_objects *
  207. eb_create(int size)
  208. {
  209. struct eb_objects *eb;
  210. int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  211. while (count > size)
  212. count >>= 1;
  213. eb = kzalloc(count*sizeof(struct hlist_head) +
  214. sizeof(struct eb_objects),
  215. GFP_KERNEL);
  216. if (eb == NULL)
  217. return eb;
  218. eb->and = count - 1;
  219. return eb;
  220. }
  221. static void
  222. eb_reset(struct eb_objects *eb)
  223. {
  224. memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  225. }
  226. static void
  227. eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj)
  228. {
  229. hlist_add_head(&obj->exec_node,
  230. &eb->buckets[obj->exec_handle & eb->and]);
  231. }
  232. static struct drm_i915_gem_object *
  233. eb_get_object(struct eb_objects *eb, unsigned long handle)
  234. {
  235. struct hlist_head *head;
  236. struct hlist_node *node;
  237. struct drm_i915_gem_object *obj;
  238. head = &eb->buckets[handle & eb->and];
  239. hlist_for_each(node, head) {
  240. obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
  241. if (obj->exec_handle == handle)
  242. return obj;
  243. }
  244. return NULL;
  245. }
  246. static void
  247. eb_destroy(struct eb_objects *eb)
  248. {
  249. kfree(eb);
  250. }
  251. static int
  252. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  253. struct eb_objects *eb,
  254. struct drm_i915_gem_relocation_entry *reloc)
  255. {
  256. struct drm_device *dev = obj->base.dev;
  257. struct drm_gem_object *target_obj;
  258. uint32_t target_offset;
  259. int ret = -EINVAL;
  260. /* we've already hold a reference to all valid objects */
  261. target_obj = &eb_get_object(eb, reloc->target_handle)->base;
  262. if (unlikely(target_obj == NULL))
  263. return -ENOENT;
  264. target_offset = to_intel_bo(target_obj)->gtt_offset;
  265. /* The target buffer should have appeared before us in the
  266. * exec_object list, so it should have a GTT space bound by now.
  267. */
  268. if (unlikely(target_offset == 0)) {
  269. DRM_ERROR("No GTT space found for object %d\n",
  270. reloc->target_handle);
  271. return ret;
  272. }
  273. /* Validate that the target is in a valid r/w GPU domain */
  274. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  275. DRM_ERROR("reloc with multiple write domains: "
  276. "obj %p target %d offset %d "
  277. "read %08x write %08x",
  278. obj, reloc->target_handle,
  279. (int) reloc->offset,
  280. reloc->read_domains,
  281. reloc->write_domain);
  282. return ret;
  283. }
  284. if (unlikely((reloc->write_domain | reloc->read_domains) & I915_GEM_DOMAIN_CPU)) {
  285. DRM_ERROR("reloc with read/write CPU domains: "
  286. "obj %p target %d offset %d "
  287. "read %08x write %08x",
  288. obj, reloc->target_handle,
  289. (int) reloc->offset,
  290. reloc->read_domains,
  291. reloc->write_domain);
  292. return ret;
  293. }
  294. if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
  295. reloc->write_domain != target_obj->pending_write_domain)) {
  296. DRM_ERROR("Write domain conflict: "
  297. "obj %p target %d offset %d "
  298. "new %08x old %08x\n",
  299. obj, reloc->target_handle,
  300. (int) reloc->offset,
  301. reloc->write_domain,
  302. target_obj->pending_write_domain);
  303. return ret;
  304. }
  305. target_obj->pending_read_domains |= reloc->read_domains;
  306. target_obj->pending_write_domain |= reloc->write_domain;
  307. /* If the relocation already has the right value in it, no
  308. * more work needs to be done.
  309. */
  310. if (target_offset == reloc->presumed_offset)
  311. return 0;
  312. /* Check that the relocation address is valid... */
  313. if (unlikely(reloc->offset > obj->base.size - 4)) {
  314. DRM_ERROR("Relocation beyond object bounds: "
  315. "obj %p target %d offset %d size %d.\n",
  316. obj, reloc->target_handle,
  317. (int) reloc->offset,
  318. (int) obj->base.size);
  319. return ret;
  320. }
  321. if (unlikely(reloc->offset & 3)) {
  322. DRM_ERROR("Relocation not 4-byte aligned: "
  323. "obj %p target %d offset %d.\n",
  324. obj, reloc->target_handle,
  325. (int) reloc->offset);
  326. return ret;
  327. }
  328. reloc->delta += target_offset;
  329. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
  330. uint32_t page_offset = reloc->offset & ~PAGE_MASK;
  331. char *vaddr;
  332. vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
  333. *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  334. kunmap_atomic(vaddr);
  335. } else {
  336. struct drm_i915_private *dev_priv = dev->dev_private;
  337. uint32_t __iomem *reloc_entry;
  338. void __iomem *reloc_page;
  339. /* We can't wait for rendering with pagefaults disabled */
  340. if (obj->active && in_atomic())
  341. return -EFAULT;
  342. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  343. if (ret)
  344. return ret;
  345. /* Map the page containing the relocation we're going to perform. */
  346. reloc->offset += obj->gtt_offset;
  347. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  348. reloc->offset & PAGE_MASK);
  349. reloc_entry = (uint32_t __iomem *)
  350. (reloc_page + (reloc->offset & ~PAGE_MASK));
  351. iowrite32(reloc->delta, reloc_entry);
  352. io_mapping_unmap_atomic(reloc_page);
  353. }
  354. /* and update the user's relocation entry */
  355. reloc->presumed_offset = target_offset;
  356. return 0;
  357. }
  358. static int
  359. i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
  360. struct eb_objects *eb)
  361. {
  362. struct drm_i915_gem_relocation_entry __user *user_relocs;
  363. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  364. int i, ret;
  365. user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
  366. for (i = 0; i < entry->relocation_count; i++) {
  367. struct drm_i915_gem_relocation_entry reloc;
  368. if (__copy_from_user_inatomic(&reloc,
  369. user_relocs+i,
  370. sizeof(reloc)))
  371. return -EFAULT;
  372. ret = i915_gem_execbuffer_relocate_entry(obj, eb, &reloc);
  373. if (ret)
  374. return ret;
  375. if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
  376. &reloc.presumed_offset,
  377. sizeof(reloc.presumed_offset)))
  378. return -EFAULT;
  379. }
  380. return 0;
  381. }
  382. static int
  383. i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
  384. struct eb_objects *eb,
  385. struct drm_i915_gem_relocation_entry *relocs)
  386. {
  387. const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  388. int i, ret;
  389. for (i = 0; i < entry->relocation_count; i++) {
  390. ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
  391. if (ret)
  392. return ret;
  393. }
  394. return 0;
  395. }
  396. static int
  397. i915_gem_execbuffer_relocate(struct drm_device *dev,
  398. struct eb_objects *eb,
  399. struct list_head *objects)
  400. {
  401. struct drm_i915_gem_object *obj;
  402. int ret = 0;
  403. /* This is the fast path and we cannot handle a pagefault whilst
  404. * holding the struct mutex lest the user pass in the relocations
  405. * contained within a mmaped bo. For in such a case we, the page
  406. * fault handler would call i915_gem_fault() and we would try to
  407. * acquire the struct mutex again. Obviously this is bad and so
  408. * lockdep complains vehemently.
  409. */
  410. pagefault_disable();
  411. list_for_each_entry(obj, objects, exec_list) {
  412. ret = i915_gem_execbuffer_relocate_object(obj, eb);
  413. if (ret)
  414. break;
  415. }
  416. pagefault_enable();
  417. return ret;
  418. }
  419. static int
  420. i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
  421. struct drm_file *file,
  422. struct list_head *objects)
  423. {
  424. struct drm_i915_gem_object *obj;
  425. int ret, retry;
  426. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  427. struct list_head ordered_objects;
  428. INIT_LIST_HEAD(&ordered_objects);
  429. while (!list_empty(objects)) {
  430. struct drm_i915_gem_exec_object2 *entry;
  431. bool need_fence, need_mappable;
  432. obj = list_first_entry(objects,
  433. struct drm_i915_gem_object,
  434. exec_list);
  435. entry = obj->exec_entry;
  436. need_fence =
  437. has_fenced_gpu_access &&
  438. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  439. obj->tiling_mode != I915_TILING_NONE;
  440. need_mappable =
  441. entry->relocation_count ? true : need_fence;
  442. if (need_mappable)
  443. list_move(&obj->exec_list, &ordered_objects);
  444. else
  445. list_move_tail(&obj->exec_list, &ordered_objects);
  446. obj->base.pending_read_domains = 0;
  447. obj->base.pending_write_domain = 0;
  448. }
  449. list_splice(&ordered_objects, objects);
  450. /* Attempt to pin all of the buffers into the GTT.
  451. * This is done in 3 phases:
  452. *
  453. * 1a. Unbind all objects that do not match the GTT constraints for
  454. * the execbuffer (fenceable, mappable, alignment etc).
  455. * 1b. Increment pin count for already bound objects.
  456. * 2. Bind new objects.
  457. * 3. Decrement pin count.
  458. *
  459. * This avoid unnecessary unbinding of later objects in order to makr
  460. * room for the earlier objects *unless* we need to defragment.
  461. */
  462. retry = 0;
  463. do {
  464. ret = 0;
  465. /* Unbind any ill-fitting objects or pin. */
  466. list_for_each_entry(obj, objects, exec_list) {
  467. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  468. bool need_fence, need_mappable;
  469. if (!obj->gtt_space)
  470. continue;
  471. need_fence =
  472. has_fenced_gpu_access &&
  473. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  474. obj->tiling_mode != I915_TILING_NONE;
  475. need_mappable =
  476. entry->relocation_count ? true : need_fence;
  477. if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
  478. (need_mappable && !obj->map_and_fenceable))
  479. ret = i915_gem_object_unbind(obj);
  480. else
  481. ret = i915_gem_object_pin(obj,
  482. entry->alignment,
  483. need_mappable);
  484. if (ret)
  485. goto err;
  486. entry++;
  487. }
  488. /* Bind fresh objects */
  489. list_for_each_entry(obj, objects, exec_list) {
  490. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  491. bool need_fence;
  492. need_fence =
  493. has_fenced_gpu_access &&
  494. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  495. obj->tiling_mode != I915_TILING_NONE;
  496. if (!obj->gtt_space) {
  497. bool need_mappable =
  498. entry->relocation_count ? true : need_fence;
  499. ret = i915_gem_object_pin(obj,
  500. entry->alignment,
  501. need_mappable);
  502. if (ret)
  503. break;
  504. }
  505. if (has_fenced_gpu_access) {
  506. if (need_fence) {
  507. ret = i915_gem_object_get_fence(obj, ring);
  508. if (ret)
  509. break;
  510. } else if (entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  511. obj->tiling_mode == I915_TILING_NONE) {
  512. /* XXX pipelined! */
  513. ret = i915_gem_object_put_fence(obj);
  514. if (ret)
  515. break;
  516. }
  517. obj->pending_fenced_gpu_access = need_fence;
  518. }
  519. entry->offset = obj->gtt_offset;
  520. }
  521. /* Decrement pin count for bound objects */
  522. list_for_each_entry(obj, objects, exec_list) {
  523. if (obj->gtt_space)
  524. i915_gem_object_unpin(obj);
  525. }
  526. if (ret != -ENOSPC || retry > 1)
  527. return ret;
  528. /* First attempt, just clear anything that is purgeable.
  529. * Second attempt, clear the entire GTT.
  530. */
  531. ret = i915_gem_evict_everything(ring->dev, retry == 0);
  532. if (ret)
  533. return ret;
  534. retry++;
  535. } while (1);
  536. err:
  537. obj = list_entry(obj->exec_list.prev,
  538. struct drm_i915_gem_object,
  539. exec_list);
  540. while (objects != &obj->exec_list) {
  541. if (obj->gtt_space)
  542. i915_gem_object_unpin(obj);
  543. obj = list_entry(obj->exec_list.prev,
  544. struct drm_i915_gem_object,
  545. exec_list);
  546. }
  547. return ret;
  548. }
  549. static int
  550. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  551. struct drm_file *file,
  552. struct intel_ring_buffer *ring,
  553. struct list_head *objects,
  554. struct eb_objects *eb,
  555. struct drm_i915_gem_exec_object2 *exec,
  556. int count)
  557. {
  558. struct drm_i915_gem_relocation_entry *reloc;
  559. struct drm_i915_gem_object *obj;
  560. int *reloc_offset;
  561. int i, total, ret;
  562. /* We may process another execbuffer during the unlock... */
  563. while (!list_empty(objects)) {
  564. obj = list_first_entry(objects,
  565. struct drm_i915_gem_object,
  566. exec_list);
  567. list_del_init(&obj->exec_list);
  568. drm_gem_object_unreference(&obj->base);
  569. }
  570. mutex_unlock(&dev->struct_mutex);
  571. total = 0;
  572. for (i = 0; i < count; i++)
  573. total += exec[i].relocation_count;
  574. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  575. reloc = drm_malloc_ab(total, sizeof(*reloc));
  576. if (reloc == NULL || reloc_offset == NULL) {
  577. drm_free_large(reloc);
  578. drm_free_large(reloc_offset);
  579. mutex_lock(&dev->struct_mutex);
  580. return -ENOMEM;
  581. }
  582. total = 0;
  583. for (i = 0; i < count; i++) {
  584. struct drm_i915_gem_relocation_entry __user *user_relocs;
  585. user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
  586. if (copy_from_user(reloc+total, user_relocs,
  587. exec[i].relocation_count * sizeof(*reloc))) {
  588. ret = -EFAULT;
  589. mutex_lock(&dev->struct_mutex);
  590. goto err;
  591. }
  592. reloc_offset[i] = total;
  593. total += exec[i].relocation_count;
  594. }
  595. ret = i915_mutex_lock_interruptible(dev);
  596. if (ret) {
  597. mutex_lock(&dev->struct_mutex);
  598. goto err;
  599. }
  600. /* reacquire the objects */
  601. eb_reset(eb);
  602. for (i = 0; i < count; i++) {
  603. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  604. exec[i].handle));
  605. if (&obj->base == NULL) {
  606. DRM_ERROR("Invalid object handle %d at index %d\n",
  607. exec[i].handle, i);
  608. ret = -ENOENT;
  609. goto err;
  610. }
  611. list_add_tail(&obj->exec_list, objects);
  612. obj->exec_handle = exec[i].handle;
  613. obj->exec_entry = &exec[i];
  614. eb_add_object(eb, obj);
  615. }
  616. ret = i915_gem_execbuffer_reserve(ring, file, objects);
  617. if (ret)
  618. goto err;
  619. list_for_each_entry(obj, objects, exec_list) {
  620. int offset = obj->exec_entry - exec;
  621. ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
  622. reloc + reloc_offset[offset]);
  623. if (ret)
  624. goto err;
  625. }
  626. /* Leave the user relocations as are, this is the painfully slow path,
  627. * and we want to avoid the complication of dropping the lock whilst
  628. * having buffers reserved in the aperture and so causing spurious
  629. * ENOSPC for random operations.
  630. */
  631. err:
  632. drm_free_large(reloc);
  633. drm_free_large(reloc_offset);
  634. return ret;
  635. }
  636. static int
  637. i915_gem_execbuffer_flush(struct drm_device *dev,
  638. uint32_t invalidate_domains,
  639. uint32_t flush_domains,
  640. uint32_t flush_rings)
  641. {
  642. drm_i915_private_t *dev_priv = dev->dev_private;
  643. int i, ret;
  644. if (flush_domains & I915_GEM_DOMAIN_CPU)
  645. intel_gtt_chipset_flush();
  646. if (flush_domains & I915_GEM_DOMAIN_GTT)
  647. wmb();
  648. if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
  649. for (i = 0; i < I915_NUM_RINGS; i++)
  650. if (flush_rings & (1 << i)) {
  651. ret = i915_gem_flush_ring(&dev_priv->ring[i],
  652. invalidate_domains,
  653. flush_domains);
  654. if (ret)
  655. return ret;
  656. }
  657. }
  658. return 0;
  659. }
  660. static bool
  661. intel_enable_semaphores(struct drm_device *dev)
  662. {
  663. if (INTEL_INFO(dev)->gen < 6)
  664. return 0;
  665. if (i915_semaphores >= 0)
  666. return i915_semaphores;
  667. /* Enable semaphores on SNB when IO remapping is off */
  668. if (INTEL_INFO(dev)->gen == 6)
  669. return !intel_iommu_enabled;
  670. return 1;
  671. }
  672. static int
  673. i915_gem_execbuffer_sync_rings(struct drm_i915_gem_object *obj,
  674. struct intel_ring_buffer *to)
  675. {
  676. struct intel_ring_buffer *from = obj->ring;
  677. u32 seqno;
  678. int ret, idx;
  679. if (from == NULL || to == from)
  680. return 0;
  681. /* XXX gpu semaphores are implicated in various hard hangs on SNB */
  682. if (!intel_enable_semaphores(obj->base.dev))
  683. return i915_gem_object_wait_rendering(obj);
  684. idx = intel_ring_sync_index(from, to);
  685. seqno = obj->last_rendering_seqno;
  686. if (seqno <= from->sync_seqno[idx])
  687. return 0;
  688. if (seqno == from->outstanding_lazy_request) {
  689. struct drm_i915_gem_request *request;
  690. request = kzalloc(sizeof(*request), GFP_KERNEL);
  691. if (request == NULL)
  692. return -ENOMEM;
  693. ret = i915_add_request(from, NULL, request);
  694. if (ret) {
  695. kfree(request);
  696. return ret;
  697. }
  698. seqno = request->seqno;
  699. }
  700. from->sync_seqno[idx] = seqno;
  701. return to->sync_to(to, from, seqno - 1);
  702. }
  703. static int
  704. i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
  705. {
  706. u32 plane, flip_mask;
  707. int ret;
  708. /* Check for any pending flips. As we only maintain a flip queue depth
  709. * of 1, we can simply insert a WAIT for the next display flip prior
  710. * to executing the batch and avoid stalling the CPU.
  711. */
  712. for (plane = 0; flips >> plane; plane++) {
  713. if (((flips >> plane) & 1) == 0)
  714. continue;
  715. if (plane)
  716. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  717. else
  718. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  719. ret = intel_ring_begin(ring, 2);
  720. if (ret)
  721. return ret;
  722. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  723. intel_ring_emit(ring, MI_NOOP);
  724. intel_ring_advance(ring);
  725. }
  726. return 0;
  727. }
  728. static int
  729. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  730. struct list_head *objects)
  731. {
  732. struct drm_i915_gem_object *obj;
  733. struct change_domains cd;
  734. int ret;
  735. memset(&cd, 0, sizeof(cd));
  736. list_for_each_entry(obj, objects, exec_list)
  737. i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
  738. if (cd.invalidate_domains | cd.flush_domains) {
  739. ret = i915_gem_execbuffer_flush(ring->dev,
  740. cd.invalidate_domains,
  741. cd.flush_domains,
  742. cd.flush_rings);
  743. if (ret)
  744. return ret;
  745. }
  746. if (cd.flips) {
  747. ret = i915_gem_execbuffer_wait_for_flips(ring, cd.flips);
  748. if (ret)
  749. return ret;
  750. }
  751. list_for_each_entry(obj, objects, exec_list) {
  752. ret = i915_gem_execbuffer_sync_rings(obj, ring);
  753. if (ret)
  754. return ret;
  755. }
  756. return 0;
  757. }
  758. static bool
  759. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  760. {
  761. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  762. }
  763. static int
  764. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  765. int count)
  766. {
  767. int i;
  768. for (i = 0; i < count; i++) {
  769. char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  770. int length; /* limited by fault_in_pages_readable() */
  771. /* First check for malicious input causing overflow */
  772. if (exec[i].relocation_count >
  773. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  774. return -EINVAL;
  775. length = exec[i].relocation_count *
  776. sizeof(struct drm_i915_gem_relocation_entry);
  777. if (!access_ok(VERIFY_READ, ptr, length))
  778. return -EFAULT;
  779. /* we may also need to update the presumed offsets */
  780. if (!access_ok(VERIFY_WRITE, ptr, length))
  781. return -EFAULT;
  782. if (fault_in_pages_readable(ptr, length))
  783. return -EFAULT;
  784. }
  785. return 0;
  786. }
  787. static void
  788. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  789. struct intel_ring_buffer *ring,
  790. u32 seqno)
  791. {
  792. struct drm_i915_gem_object *obj;
  793. list_for_each_entry(obj, objects, exec_list) {
  794. u32 old_read = obj->base.read_domains;
  795. u32 old_write = obj->base.write_domain;
  796. obj->base.read_domains = obj->base.pending_read_domains;
  797. obj->base.write_domain = obj->base.pending_write_domain;
  798. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  799. i915_gem_object_move_to_active(obj, ring, seqno);
  800. if (obj->base.write_domain) {
  801. obj->dirty = 1;
  802. obj->pending_gpu_write = true;
  803. list_move_tail(&obj->gpu_write_list,
  804. &ring->gpu_write_list);
  805. intel_mark_busy(ring->dev, obj);
  806. }
  807. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  808. }
  809. }
  810. static void
  811. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  812. struct drm_file *file,
  813. struct intel_ring_buffer *ring)
  814. {
  815. struct drm_i915_gem_request *request;
  816. u32 invalidate;
  817. /*
  818. * Ensure that the commands in the batch buffer are
  819. * finished before the interrupt fires.
  820. *
  821. * The sampler always gets flushed on i965 (sigh).
  822. */
  823. invalidate = I915_GEM_DOMAIN_COMMAND;
  824. if (INTEL_INFO(dev)->gen >= 4)
  825. invalidate |= I915_GEM_DOMAIN_SAMPLER;
  826. if (ring->flush(ring, invalidate, 0)) {
  827. i915_gem_next_request_seqno(ring);
  828. return;
  829. }
  830. /* Add a breadcrumb for the completion of the batch buffer */
  831. request = kzalloc(sizeof(*request), GFP_KERNEL);
  832. if (request == NULL || i915_add_request(ring, file, request)) {
  833. i915_gem_next_request_seqno(ring);
  834. kfree(request);
  835. }
  836. }
  837. static int
  838. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  839. struct drm_file *file,
  840. struct drm_i915_gem_execbuffer2 *args,
  841. struct drm_i915_gem_exec_object2 *exec)
  842. {
  843. drm_i915_private_t *dev_priv = dev->dev_private;
  844. struct list_head objects;
  845. struct eb_objects *eb;
  846. struct drm_i915_gem_object *batch_obj;
  847. struct drm_clip_rect *cliprects = NULL;
  848. struct intel_ring_buffer *ring;
  849. u32 exec_start, exec_len;
  850. u32 seqno;
  851. u32 mask;
  852. int ret, mode, i;
  853. if (!i915_gem_check_execbuffer(args)) {
  854. DRM_ERROR("execbuf with invalid offset/length\n");
  855. return -EINVAL;
  856. }
  857. ret = validate_exec_list(exec, args->buffer_count);
  858. if (ret)
  859. return ret;
  860. switch (args->flags & I915_EXEC_RING_MASK) {
  861. case I915_EXEC_DEFAULT:
  862. case I915_EXEC_RENDER:
  863. ring = &dev_priv->ring[RCS];
  864. break;
  865. case I915_EXEC_BSD:
  866. if (!HAS_BSD(dev)) {
  867. DRM_ERROR("execbuf with invalid ring (BSD)\n");
  868. return -EINVAL;
  869. }
  870. ring = &dev_priv->ring[VCS];
  871. break;
  872. case I915_EXEC_BLT:
  873. if (!HAS_BLT(dev)) {
  874. DRM_ERROR("execbuf with invalid ring (BLT)\n");
  875. return -EINVAL;
  876. }
  877. ring = &dev_priv->ring[BCS];
  878. break;
  879. default:
  880. DRM_ERROR("execbuf with unknown ring: %d\n",
  881. (int)(args->flags & I915_EXEC_RING_MASK));
  882. return -EINVAL;
  883. }
  884. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  885. mask = I915_EXEC_CONSTANTS_MASK;
  886. switch (mode) {
  887. case I915_EXEC_CONSTANTS_REL_GENERAL:
  888. case I915_EXEC_CONSTANTS_ABSOLUTE:
  889. case I915_EXEC_CONSTANTS_REL_SURFACE:
  890. if (ring == &dev_priv->ring[RCS] &&
  891. mode != dev_priv->relative_constants_mode) {
  892. if (INTEL_INFO(dev)->gen < 4)
  893. return -EINVAL;
  894. if (INTEL_INFO(dev)->gen > 5 &&
  895. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  896. return -EINVAL;
  897. /* The HW changed the meaning on this bit on gen6 */
  898. if (INTEL_INFO(dev)->gen >= 6)
  899. mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  900. }
  901. break;
  902. default:
  903. DRM_ERROR("execbuf with unknown constants: %d\n", mode);
  904. return -EINVAL;
  905. }
  906. if (args->buffer_count < 1) {
  907. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  908. return -EINVAL;
  909. }
  910. if (args->num_cliprects != 0) {
  911. if (ring != &dev_priv->ring[RCS]) {
  912. DRM_ERROR("clip rectangles are only valid with the render ring\n");
  913. return -EINVAL;
  914. }
  915. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  916. GFP_KERNEL);
  917. if (cliprects == NULL) {
  918. ret = -ENOMEM;
  919. goto pre_mutex_err;
  920. }
  921. if (copy_from_user(cliprects,
  922. (struct drm_clip_rect __user *)(uintptr_t)
  923. args->cliprects_ptr,
  924. sizeof(*cliprects)*args->num_cliprects)) {
  925. ret = -EFAULT;
  926. goto pre_mutex_err;
  927. }
  928. }
  929. ret = i915_mutex_lock_interruptible(dev);
  930. if (ret)
  931. goto pre_mutex_err;
  932. if (dev_priv->mm.suspended) {
  933. mutex_unlock(&dev->struct_mutex);
  934. ret = -EBUSY;
  935. goto pre_mutex_err;
  936. }
  937. eb = eb_create(args->buffer_count);
  938. if (eb == NULL) {
  939. mutex_unlock(&dev->struct_mutex);
  940. ret = -ENOMEM;
  941. goto pre_mutex_err;
  942. }
  943. /* Look up object handles */
  944. INIT_LIST_HEAD(&objects);
  945. for (i = 0; i < args->buffer_count; i++) {
  946. struct drm_i915_gem_object *obj;
  947. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  948. exec[i].handle));
  949. if (&obj->base == NULL) {
  950. DRM_ERROR("Invalid object handle %d at index %d\n",
  951. exec[i].handle, i);
  952. /* prevent error path from reading uninitialized data */
  953. ret = -ENOENT;
  954. goto err;
  955. }
  956. if (!list_empty(&obj->exec_list)) {
  957. DRM_ERROR("Object %p [handle %d, index %d] appears more than once in object list\n",
  958. obj, exec[i].handle, i);
  959. ret = -EINVAL;
  960. goto err;
  961. }
  962. list_add_tail(&obj->exec_list, &objects);
  963. obj->exec_handle = exec[i].handle;
  964. obj->exec_entry = &exec[i];
  965. eb_add_object(eb, obj);
  966. }
  967. /* take note of the batch buffer before we might reorder the lists */
  968. batch_obj = list_entry(objects.prev,
  969. struct drm_i915_gem_object,
  970. exec_list);
  971. /* Move the objects en-masse into the GTT, evicting if necessary. */
  972. ret = i915_gem_execbuffer_reserve(ring, file, &objects);
  973. if (ret)
  974. goto err;
  975. /* The objects are in their final locations, apply the relocations. */
  976. ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
  977. if (ret) {
  978. if (ret == -EFAULT) {
  979. ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
  980. &objects, eb,
  981. exec,
  982. args->buffer_count);
  983. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  984. }
  985. if (ret)
  986. goto err;
  987. }
  988. /* Set the pending read domains for the batch buffer to COMMAND */
  989. if (batch_obj->base.pending_write_domain) {
  990. DRM_ERROR("Attempting to use self-modifying batch buffer\n");
  991. ret = -EINVAL;
  992. goto err;
  993. }
  994. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  995. ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
  996. if (ret)
  997. goto err;
  998. seqno = i915_gem_next_request_seqno(ring);
  999. for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++) {
  1000. if (seqno < ring->sync_seqno[i]) {
  1001. /* The GPU can not handle its semaphore value wrapping,
  1002. * so every billion or so execbuffers, we need to stall
  1003. * the GPU in order to reset the counters.
  1004. */
  1005. ret = i915_gpu_idle(dev);
  1006. if (ret)
  1007. goto err;
  1008. BUG_ON(ring->sync_seqno[i]);
  1009. }
  1010. }
  1011. if (ring == &dev_priv->ring[RCS] &&
  1012. mode != dev_priv->relative_constants_mode) {
  1013. ret = intel_ring_begin(ring, 4);
  1014. if (ret)
  1015. goto err;
  1016. intel_ring_emit(ring, MI_NOOP);
  1017. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1018. intel_ring_emit(ring, INSTPM);
  1019. intel_ring_emit(ring, mask << 16 | mode);
  1020. intel_ring_advance(ring);
  1021. dev_priv->relative_constants_mode = mode;
  1022. }
  1023. trace_i915_gem_ring_dispatch(ring, seqno);
  1024. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  1025. exec_len = args->batch_len;
  1026. if (cliprects) {
  1027. for (i = 0; i < args->num_cliprects; i++) {
  1028. ret = i915_emit_box(dev, &cliprects[i],
  1029. args->DR1, args->DR4);
  1030. if (ret)
  1031. goto err;
  1032. ret = ring->dispatch_execbuffer(ring,
  1033. exec_start, exec_len);
  1034. if (ret)
  1035. goto err;
  1036. }
  1037. } else {
  1038. ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
  1039. if (ret)
  1040. goto err;
  1041. }
  1042. i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
  1043. i915_gem_execbuffer_retire_commands(dev, file, ring);
  1044. err:
  1045. eb_destroy(eb);
  1046. while (!list_empty(&objects)) {
  1047. struct drm_i915_gem_object *obj;
  1048. obj = list_first_entry(&objects,
  1049. struct drm_i915_gem_object,
  1050. exec_list);
  1051. list_del_init(&obj->exec_list);
  1052. drm_gem_object_unreference(&obj->base);
  1053. }
  1054. mutex_unlock(&dev->struct_mutex);
  1055. pre_mutex_err:
  1056. kfree(cliprects);
  1057. return ret;
  1058. }
  1059. /*
  1060. * Legacy execbuffer just creates an exec2 list from the original exec object
  1061. * list array and passes it to the real function.
  1062. */
  1063. int
  1064. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1065. struct drm_file *file)
  1066. {
  1067. struct drm_i915_gem_execbuffer *args = data;
  1068. struct drm_i915_gem_execbuffer2 exec2;
  1069. struct drm_i915_gem_exec_object *exec_list = NULL;
  1070. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1071. int ret, i;
  1072. if (args->buffer_count < 1) {
  1073. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  1074. return -EINVAL;
  1075. }
  1076. /* Copy in the exec list from userland */
  1077. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1078. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1079. if (exec_list == NULL || exec2_list == NULL) {
  1080. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  1081. args->buffer_count);
  1082. drm_free_large(exec_list);
  1083. drm_free_large(exec2_list);
  1084. return -ENOMEM;
  1085. }
  1086. ret = copy_from_user(exec_list,
  1087. (struct drm_i915_relocation_entry __user *)
  1088. (uintptr_t) args->buffers_ptr,
  1089. sizeof(*exec_list) * args->buffer_count);
  1090. if (ret != 0) {
  1091. DRM_ERROR("copy %d exec entries failed %d\n",
  1092. args->buffer_count, ret);
  1093. drm_free_large(exec_list);
  1094. drm_free_large(exec2_list);
  1095. return -EFAULT;
  1096. }
  1097. for (i = 0; i < args->buffer_count; i++) {
  1098. exec2_list[i].handle = exec_list[i].handle;
  1099. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1100. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1101. exec2_list[i].alignment = exec_list[i].alignment;
  1102. exec2_list[i].offset = exec_list[i].offset;
  1103. if (INTEL_INFO(dev)->gen < 4)
  1104. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1105. else
  1106. exec2_list[i].flags = 0;
  1107. }
  1108. exec2.buffers_ptr = args->buffers_ptr;
  1109. exec2.buffer_count = args->buffer_count;
  1110. exec2.batch_start_offset = args->batch_start_offset;
  1111. exec2.batch_len = args->batch_len;
  1112. exec2.DR1 = args->DR1;
  1113. exec2.DR4 = args->DR4;
  1114. exec2.num_cliprects = args->num_cliprects;
  1115. exec2.cliprects_ptr = args->cliprects_ptr;
  1116. exec2.flags = I915_EXEC_RENDER;
  1117. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1118. if (!ret) {
  1119. /* Copy the new buffer offsets back to the user's exec list. */
  1120. for (i = 0; i < args->buffer_count; i++)
  1121. exec_list[i].offset = exec2_list[i].offset;
  1122. /* ... and back out to userspace */
  1123. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1124. (uintptr_t) args->buffers_ptr,
  1125. exec_list,
  1126. sizeof(*exec_list) * args->buffer_count);
  1127. if (ret) {
  1128. ret = -EFAULT;
  1129. DRM_ERROR("failed to copy %d exec entries "
  1130. "back to user (%d)\n",
  1131. args->buffer_count, ret);
  1132. }
  1133. }
  1134. drm_free_large(exec_list);
  1135. drm_free_large(exec2_list);
  1136. return ret;
  1137. }
  1138. int
  1139. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1140. struct drm_file *file)
  1141. {
  1142. struct drm_i915_gem_execbuffer2 *args = data;
  1143. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1144. int ret;
  1145. if (args->buffer_count < 1) {
  1146. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  1147. return -EINVAL;
  1148. }
  1149. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1150. GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1151. if (exec2_list == NULL)
  1152. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1153. args->buffer_count);
  1154. if (exec2_list == NULL) {
  1155. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  1156. args->buffer_count);
  1157. return -ENOMEM;
  1158. }
  1159. ret = copy_from_user(exec2_list,
  1160. (struct drm_i915_relocation_entry __user *)
  1161. (uintptr_t) args->buffers_ptr,
  1162. sizeof(*exec2_list) * args->buffer_count);
  1163. if (ret != 0) {
  1164. DRM_ERROR("copy %d exec entries failed %d\n",
  1165. args->buffer_count, ret);
  1166. drm_free_large(exec2_list);
  1167. return -EFAULT;
  1168. }
  1169. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1170. if (!ret) {
  1171. /* Copy the new buffer offsets back to the user's exec list. */
  1172. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1173. (uintptr_t) args->buffers_ptr,
  1174. exec2_list,
  1175. sizeof(*exec2_list) * args->buffer_count);
  1176. if (ret) {
  1177. ret = -EFAULT;
  1178. DRM_ERROR("failed to copy %d exec entries "
  1179. "back to user (%d)\n",
  1180. args->buffer_count, ret);
  1181. }
  1182. }
  1183. drm_free_large(exec2_list);
  1184. return ret;
  1185. }