i915_gem_execbuffer.c 35 KB

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