i915_gem_execbuffer.c 36 KB

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