i915_gem_execbuffer.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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_exec_object2 *entry,
  254. struct drm_i915_gem_relocation_entry *reloc)
  255. {
  256. struct drm_device *dev = obj->base.dev;
  257. struct drm_gem_object *target_obj;
  258. uint32_t target_offset;
  259. int ret = -EINVAL;
  260. /* we've already hold a reference to all valid objects */
  261. target_obj = &eb_get_object(eb, reloc->target_handle)->base;
  262. if (unlikely(target_obj == NULL))
  263. return -ENOENT;
  264. target_offset = to_intel_bo(target_obj)->gtt_offset;
  265. #if WATCH_RELOC
  266. DRM_INFO("%s: obj %p offset %08x target %d "
  267. "read %08x write %08x gtt %08x "
  268. "presumed %08x delta %08x\n",
  269. __func__,
  270. obj,
  271. (int) reloc->offset,
  272. (int) reloc->target_handle,
  273. (int) reloc->read_domains,
  274. (int) reloc->write_domain,
  275. (int) target_offset,
  276. (int) reloc->presumed_offset,
  277. reloc->delta);
  278. #endif
  279. /* The target buffer should have appeared before us in the
  280. * exec_object list, so it should have a GTT space bound by now.
  281. */
  282. if (unlikely(target_offset == 0)) {
  283. DRM_ERROR("No GTT space found for object %d\n",
  284. reloc->target_handle);
  285. return ret;
  286. }
  287. /* Validate that the target is in a valid r/w GPU domain */
  288. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  289. DRM_ERROR("reloc with multiple write domains: "
  290. "obj %p target %d offset %d "
  291. "read %08x write %08x",
  292. obj, reloc->target_handle,
  293. (int) reloc->offset,
  294. reloc->read_domains,
  295. reloc->write_domain);
  296. return ret;
  297. }
  298. if (unlikely((reloc->write_domain | reloc->read_domains) & I915_GEM_DOMAIN_CPU)) {
  299. DRM_ERROR("reloc with read/write CPU domains: "
  300. "obj %p target %d offset %d "
  301. "read %08x write %08x",
  302. obj, reloc->target_handle,
  303. (int) reloc->offset,
  304. reloc->read_domains,
  305. reloc->write_domain);
  306. return ret;
  307. }
  308. if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
  309. reloc->write_domain != target_obj->pending_write_domain)) {
  310. DRM_ERROR("Write domain conflict: "
  311. "obj %p target %d offset %d "
  312. "new %08x old %08x\n",
  313. obj, reloc->target_handle,
  314. (int) reloc->offset,
  315. reloc->write_domain,
  316. target_obj->pending_write_domain);
  317. return ret;
  318. }
  319. target_obj->pending_read_domains |= reloc->read_domains;
  320. target_obj->pending_write_domain |= reloc->write_domain;
  321. /* If the relocation already has the right value in it, no
  322. * more work needs to be done.
  323. */
  324. if (target_offset == reloc->presumed_offset)
  325. return 0;
  326. /* Check that the relocation address is valid... */
  327. if (unlikely(reloc->offset > obj->base.size - 4)) {
  328. DRM_ERROR("Relocation beyond object bounds: "
  329. "obj %p target %d offset %d size %d.\n",
  330. obj, reloc->target_handle,
  331. (int) reloc->offset,
  332. (int) obj->base.size);
  333. return ret;
  334. }
  335. if (unlikely(reloc->offset & 3)) {
  336. DRM_ERROR("Relocation not 4-byte aligned: "
  337. "obj %p target %d offset %d.\n",
  338. obj, reloc->target_handle,
  339. (int) reloc->offset);
  340. return ret;
  341. }
  342. /* and points to somewhere within the target object. */
  343. if (unlikely(reloc->delta >= target_obj->size)) {
  344. DRM_ERROR("Relocation beyond target object bounds: "
  345. "obj %p target %d delta %d size %d.\n",
  346. obj, reloc->target_handle,
  347. (int) reloc->delta,
  348. (int) target_obj->size);
  349. return ret;
  350. }
  351. reloc->delta += target_offset;
  352. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
  353. uint32_t page_offset = reloc->offset & ~PAGE_MASK;
  354. char *vaddr;
  355. vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
  356. *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  357. kunmap_atomic(vaddr);
  358. } else {
  359. struct drm_i915_private *dev_priv = dev->dev_private;
  360. uint32_t __iomem *reloc_entry;
  361. void __iomem *reloc_page;
  362. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  363. if (ret)
  364. return ret;
  365. /* Map the page containing the relocation we're going to perform. */
  366. reloc->offset += obj->gtt_offset;
  367. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  368. reloc->offset & PAGE_MASK);
  369. reloc_entry = (uint32_t __iomem *)
  370. (reloc_page + (reloc->offset & ~PAGE_MASK));
  371. iowrite32(reloc->delta, reloc_entry);
  372. io_mapping_unmap_atomic(reloc_page);
  373. }
  374. /* and update the user's relocation entry */
  375. reloc->presumed_offset = target_offset;
  376. return 0;
  377. }
  378. static int
  379. i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
  380. struct eb_objects *eb,
  381. struct drm_i915_gem_exec_object2 *entry)
  382. {
  383. struct drm_i915_gem_relocation_entry __user *user_relocs;
  384. int i, ret;
  385. user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
  386. for (i = 0; i < entry->relocation_count; i++) {
  387. struct drm_i915_gem_relocation_entry reloc;
  388. if (__copy_from_user_inatomic(&reloc,
  389. user_relocs+i,
  390. sizeof(reloc)))
  391. return -EFAULT;
  392. ret = i915_gem_execbuffer_relocate_entry(obj, eb, entry, &reloc);
  393. if (ret)
  394. return ret;
  395. if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
  396. &reloc.presumed_offset,
  397. sizeof(reloc.presumed_offset)))
  398. return -EFAULT;
  399. }
  400. return 0;
  401. }
  402. static int
  403. i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
  404. struct eb_objects *eb,
  405. struct drm_i915_gem_exec_object2 *entry,
  406. struct drm_i915_gem_relocation_entry *relocs)
  407. {
  408. int i, ret;
  409. for (i = 0; i < entry->relocation_count; i++) {
  410. ret = i915_gem_execbuffer_relocate_entry(obj, eb, entry, &relocs[i]);
  411. if (ret)
  412. return ret;
  413. }
  414. return 0;
  415. }
  416. static int
  417. i915_gem_execbuffer_relocate(struct drm_device *dev,
  418. struct eb_objects *eb,
  419. struct list_head *objects,
  420. struct drm_i915_gem_exec_object2 *exec)
  421. {
  422. struct drm_i915_gem_object *obj;
  423. int ret;
  424. list_for_each_entry(obj, objects, exec_list) {
  425. obj->base.pending_read_domains = 0;
  426. obj->base.pending_write_domain = 0;
  427. ret = i915_gem_execbuffer_relocate_object(obj, eb, exec++);
  428. if (ret)
  429. return ret;
  430. }
  431. return 0;
  432. }
  433. static int
  434. i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
  435. struct drm_file *file,
  436. struct list_head *objects,
  437. struct drm_i915_gem_exec_object2 *exec)
  438. {
  439. struct drm_i915_gem_object *obj;
  440. struct drm_i915_gem_exec_object2 *entry;
  441. int ret, retry;
  442. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  443. /* Attempt to pin all of the buffers into the GTT.
  444. * This is done in 3 phases:
  445. *
  446. * 1a. Unbind all objects that do not match the GTT constraints for
  447. * the execbuffer (fenceable, mappable, alignment etc).
  448. * 1b. Increment pin count for already bound objects.
  449. * 2. Bind new objects.
  450. * 3. Decrement pin count.
  451. *
  452. * This avoid unnecessary unbinding of later objects in order to makr
  453. * room for the earlier objects *unless* we need to defragment.
  454. */
  455. retry = 0;
  456. do {
  457. ret = 0;
  458. /* Unbind any ill-fitting objects or pin. */
  459. entry = exec;
  460. list_for_each_entry(obj, objects, exec_list) {
  461. bool need_fence, need_mappable;
  462. if (!obj->gtt_space) {
  463. entry++;
  464. continue;
  465. }
  466. need_fence =
  467. has_fenced_gpu_access &&
  468. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  469. obj->tiling_mode != I915_TILING_NONE;
  470. need_mappable =
  471. entry->relocation_count ? true : need_fence;
  472. if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
  473. (need_mappable && !obj->map_and_fenceable))
  474. ret = i915_gem_object_unbind(obj);
  475. else
  476. ret = i915_gem_object_pin(obj,
  477. entry->alignment,
  478. need_mappable);
  479. if (ret)
  480. goto err;
  481. entry++;
  482. }
  483. /* Bind fresh objects */
  484. entry = exec;
  485. list_for_each_entry(obj, objects, exec_list) {
  486. bool need_fence;
  487. need_fence =
  488. has_fenced_gpu_access &&
  489. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  490. obj->tiling_mode != I915_TILING_NONE;
  491. if (!obj->gtt_space) {
  492. bool need_mappable =
  493. entry->relocation_count ? true : need_fence;
  494. ret = i915_gem_object_pin(obj,
  495. entry->alignment,
  496. need_mappable);
  497. if (ret)
  498. break;
  499. }
  500. if (has_fenced_gpu_access) {
  501. if (need_fence) {
  502. ret = i915_gem_object_get_fence(obj, ring, 1);
  503. if (ret)
  504. break;
  505. } else if (entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  506. obj->tiling_mode == I915_TILING_NONE) {
  507. /* XXX pipelined! */
  508. ret = i915_gem_object_put_fence(obj);
  509. if (ret)
  510. break;
  511. }
  512. obj->pending_fenced_gpu_access = need_fence;
  513. }
  514. entry->offset = obj->gtt_offset;
  515. entry++;
  516. }
  517. /* Decrement pin count for bound objects */
  518. list_for_each_entry(obj, objects, exec_list) {
  519. if (obj->gtt_space)
  520. i915_gem_object_unpin(obj);
  521. }
  522. if (ret != -ENOSPC || retry > 1)
  523. return ret;
  524. /* First attempt, just clear anything that is purgeable.
  525. * Second attempt, clear the entire GTT.
  526. */
  527. ret = i915_gem_evict_everything(ring->dev, retry == 0);
  528. if (ret)
  529. return ret;
  530. retry++;
  531. } while (1);
  532. err:
  533. obj = list_entry(obj->exec_list.prev,
  534. struct drm_i915_gem_object,
  535. exec_list);
  536. while (objects != &obj->exec_list) {
  537. if (obj->gtt_space)
  538. i915_gem_object_unpin(obj);
  539. obj = list_entry(obj->exec_list.prev,
  540. struct drm_i915_gem_object,
  541. exec_list);
  542. }
  543. return ret;
  544. }
  545. static int
  546. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  547. struct drm_file *file,
  548. struct intel_ring_buffer *ring,
  549. struct list_head *objects,
  550. struct eb_objects *eb,
  551. struct drm_i915_gem_exec_object2 *exec,
  552. int count)
  553. {
  554. struct drm_i915_gem_relocation_entry *reloc;
  555. struct drm_i915_gem_object *obj;
  556. int i, total, ret;
  557. /* We may process another execbuffer during the unlock... */
  558. while (list_empty(objects)) {
  559. obj = list_first_entry(objects,
  560. struct drm_i915_gem_object,
  561. exec_list);
  562. list_del_init(&obj->exec_list);
  563. drm_gem_object_unreference(&obj->base);
  564. }
  565. mutex_unlock(&dev->struct_mutex);
  566. total = 0;
  567. for (i = 0; i < count; i++)
  568. total += exec[i].relocation_count;
  569. reloc = drm_malloc_ab(total, sizeof(*reloc));
  570. if (reloc == NULL) {
  571. mutex_lock(&dev->struct_mutex);
  572. return -ENOMEM;
  573. }
  574. total = 0;
  575. for (i = 0; i < count; i++) {
  576. struct drm_i915_gem_relocation_entry __user *user_relocs;
  577. user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
  578. if (copy_from_user(reloc+total, user_relocs,
  579. exec[i].relocation_count * sizeof(*reloc))) {
  580. ret = -EFAULT;
  581. mutex_lock(&dev->struct_mutex);
  582. goto err;
  583. }
  584. total += exec[i].relocation_count;
  585. }
  586. ret = i915_mutex_lock_interruptible(dev);
  587. if (ret) {
  588. mutex_lock(&dev->struct_mutex);
  589. goto err;
  590. }
  591. /* reacquire the objects */
  592. INIT_LIST_HEAD(objects);
  593. eb_reset(eb);
  594. for (i = 0; i < count; i++) {
  595. struct drm_i915_gem_object *obj;
  596. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  597. exec[i].handle));
  598. if (obj == NULL) {
  599. DRM_ERROR("Invalid object handle %d at index %d\n",
  600. exec[i].handle, i);
  601. ret = -ENOENT;
  602. goto err;
  603. }
  604. list_add_tail(&obj->exec_list, objects);
  605. obj->exec_handle = exec[i].handle;
  606. eb_add_object(eb, obj);
  607. }
  608. ret = i915_gem_execbuffer_reserve(ring, file, objects, exec);
  609. if (ret)
  610. goto err;
  611. total = 0;
  612. list_for_each_entry(obj, objects, exec_list) {
  613. obj->base.pending_read_domains = 0;
  614. obj->base.pending_write_domain = 0;
  615. ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
  616. exec,
  617. reloc + total);
  618. if (ret)
  619. goto err;
  620. total += exec->relocation_count;
  621. exec++;
  622. }
  623. /* Leave the user relocations as are, this is the painfully slow path,
  624. * and we want to avoid the complication of dropping the lock whilst
  625. * having buffers reserved in the aperture and so causing spurious
  626. * ENOSPC for random operations.
  627. */
  628. err:
  629. drm_free_large(reloc);
  630. return ret;
  631. }
  632. static int
  633. i915_gem_execbuffer_flush(struct drm_device *dev,
  634. uint32_t invalidate_domains,
  635. uint32_t flush_domains,
  636. uint32_t flush_rings)
  637. {
  638. drm_i915_private_t *dev_priv = dev->dev_private;
  639. int i, ret;
  640. if (flush_domains & I915_GEM_DOMAIN_CPU)
  641. intel_gtt_chipset_flush();
  642. if (flush_domains & I915_GEM_DOMAIN_GTT)
  643. wmb();
  644. if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
  645. for (i = 0; i < I915_NUM_RINGS; i++)
  646. if (flush_rings & (1 << i)) {
  647. ret = i915_gem_flush_ring(dev,
  648. &dev_priv->ring[i],
  649. invalidate_domains,
  650. flush_domains);
  651. if (ret)
  652. return ret;
  653. }
  654. }
  655. return 0;
  656. }
  657. static int
  658. i915_gem_execbuffer_sync_rings(struct drm_i915_gem_object *obj,
  659. struct intel_ring_buffer *to)
  660. {
  661. struct intel_ring_buffer *from = obj->ring;
  662. u32 seqno;
  663. int ret, idx;
  664. if (from == NULL || to == from)
  665. return 0;
  666. if (INTEL_INFO(obj->base.dev)->gen < 6)
  667. return i915_gem_object_wait_rendering(obj, true);
  668. idx = intel_ring_sync_index(from, to);
  669. seqno = obj->last_rendering_seqno;
  670. if (seqno <= from->sync_seqno[idx])
  671. return 0;
  672. if (seqno == from->outstanding_lazy_request) {
  673. struct drm_i915_gem_request *request;
  674. request = kzalloc(sizeof(*request), GFP_KERNEL);
  675. if (request == NULL)
  676. return -ENOMEM;
  677. ret = i915_add_request(obj->base.dev, NULL, request, from);
  678. if (ret) {
  679. kfree(request);
  680. return ret;
  681. }
  682. seqno = request->seqno;
  683. }
  684. from->sync_seqno[idx] = seqno;
  685. return intel_ring_sync(to, from, seqno - 1);
  686. }
  687. static int
  688. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  689. struct list_head *objects)
  690. {
  691. struct drm_i915_gem_object *obj;
  692. struct change_domains cd;
  693. int ret;
  694. cd.invalidate_domains = 0;
  695. cd.flush_domains = 0;
  696. cd.flush_rings = 0;
  697. list_for_each_entry(obj, objects, exec_list)
  698. i915_gem_object_set_to_gpu_domain(obj, ring, &cd);
  699. if (cd.invalidate_domains | cd.flush_domains) {
  700. #if WATCH_EXEC
  701. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  702. __func__,
  703. cd.invalidate_domains,
  704. cd.flush_domains);
  705. #endif
  706. ret = i915_gem_execbuffer_flush(ring->dev,
  707. cd.invalidate_domains,
  708. cd.flush_domains,
  709. cd.flush_rings);
  710. if (ret)
  711. return ret;
  712. }
  713. list_for_each_entry(obj, objects, exec_list) {
  714. ret = i915_gem_execbuffer_sync_rings(obj, ring);
  715. if (ret)
  716. return ret;
  717. }
  718. return 0;
  719. }
  720. static bool
  721. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  722. {
  723. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  724. }
  725. static int
  726. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  727. int count)
  728. {
  729. int i;
  730. for (i = 0; i < count; i++) {
  731. char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  732. int length; /* limited by fault_in_pages_readable() */
  733. /* First check for malicious input causing overflow */
  734. if (exec[i].relocation_count >
  735. INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  736. return -EINVAL;
  737. length = exec[i].relocation_count *
  738. sizeof(struct drm_i915_gem_relocation_entry);
  739. if (!access_ok(VERIFY_READ, ptr, length))
  740. return -EFAULT;
  741. /* we may also need to update the presumed offsets */
  742. if (!access_ok(VERIFY_WRITE, ptr, length))
  743. return -EFAULT;
  744. if (fault_in_pages_readable(ptr, length))
  745. return -EFAULT;
  746. }
  747. return 0;
  748. }
  749. static int
  750. i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring,
  751. struct list_head *objects)
  752. {
  753. struct drm_i915_gem_object *obj;
  754. int flips;
  755. /* Check for any pending flips. As we only maintain a flip queue depth
  756. * of 1, we can simply insert a WAIT for the next display flip prior
  757. * to executing the batch and avoid stalling the CPU.
  758. */
  759. flips = 0;
  760. list_for_each_entry(obj, objects, exec_list) {
  761. if (obj->base.write_domain)
  762. flips |= atomic_read(&obj->pending_flip);
  763. }
  764. if (flips) {
  765. int plane, flip_mask, ret;
  766. for (plane = 0; flips >> plane; plane++) {
  767. if (((flips >> plane) & 1) == 0)
  768. continue;
  769. if (plane)
  770. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  771. else
  772. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  773. ret = intel_ring_begin(ring, 2);
  774. if (ret)
  775. return ret;
  776. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  777. intel_ring_emit(ring, MI_NOOP);
  778. intel_ring_advance(ring);
  779. }
  780. }
  781. return 0;
  782. }
  783. static void
  784. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  785. struct intel_ring_buffer *ring,
  786. u32 seqno)
  787. {
  788. struct drm_i915_gem_object *obj;
  789. list_for_each_entry(obj, objects, exec_list) {
  790. obj->base.read_domains = obj->base.pending_read_domains;
  791. obj->base.write_domain = obj->base.pending_write_domain;
  792. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  793. i915_gem_object_move_to_active(obj, ring, seqno);
  794. if (obj->base.write_domain) {
  795. obj->dirty = 1;
  796. obj->pending_gpu_write = true;
  797. list_move_tail(&obj->gpu_write_list,
  798. &ring->gpu_write_list);
  799. intel_mark_busy(ring->dev, obj);
  800. }
  801. trace_i915_gem_object_change_domain(obj,
  802. obj->base.read_domains,
  803. obj->base.write_domain);
  804. }
  805. }
  806. static void
  807. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  808. struct drm_file *file,
  809. struct intel_ring_buffer *ring)
  810. {
  811. struct drm_i915_gem_request *request;
  812. u32 invalidate;
  813. /*
  814. * Ensure that the commands in the batch buffer are
  815. * finished before the interrupt fires.
  816. *
  817. * The sampler always gets flushed on i965 (sigh).
  818. */
  819. invalidate = I915_GEM_DOMAIN_COMMAND;
  820. if (INTEL_INFO(dev)->gen >= 4)
  821. invalidate |= I915_GEM_DOMAIN_SAMPLER;
  822. if (ring->flush(ring, invalidate, 0)) {
  823. i915_gem_next_request_seqno(dev, ring);
  824. return;
  825. }
  826. /* Add a breadcrumb for the completion of the batch buffer */
  827. request = kzalloc(sizeof(*request), GFP_KERNEL);
  828. if (request == NULL || i915_add_request(dev, file, request, ring)) {
  829. i915_gem_next_request_seqno(dev, ring);
  830. kfree(request);
  831. }
  832. }
  833. static int
  834. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  835. struct drm_file *file,
  836. struct drm_i915_gem_execbuffer2 *args,
  837. struct drm_i915_gem_exec_object2 *exec)
  838. {
  839. drm_i915_private_t *dev_priv = dev->dev_private;
  840. struct list_head objects;
  841. struct eb_objects *eb;
  842. struct drm_i915_gem_object *batch_obj;
  843. struct drm_clip_rect *cliprects = NULL;
  844. struct intel_ring_buffer *ring;
  845. u32 exec_start, exec_len;
  846. u32 seqno;
  847. int ret, mode, i;
  848. if (!i915_gem_check_execbuffer(args)) {
  849. DRM_ERROR("execbuf with invalid offset/length\n");
  850. return -EINVAL;
  851. }
  852. ret = validate_exec_list(exec, args->buffer_count);
  853. if (ret)
  854. return ret;
  855. #if WATCH_EXEC
  856. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  857. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  858. #endif
  859. switch (args->flags & I915_EXEC_RING_MASK) {
  860. case I915_EXEC_DEFAULT:
  861. case I915_EXEC_RENDER:
  862. ring = &dev_priv->ring[RCS];
  863. break;
  864. case I915_EXEC_BSD:
  865. if (!HAS_BSD(dev)) {
  866. DRM_ERROR("execbuf with invalid ring (BSD)\n");
  867. return -EINVAL;
  868. }
  869. ring = &dev_priv->ring[VCS];
  870. break;
  871. case I915_EXEC_BLT:
  872. if (!HAS_BLT(dev)) {
  873. DRM_ERROR("execbuf with invalid ring (BLT)\n");
  874. return -EINVAL;
  875. }
  876. ring = &dev_priv->ring[BCS];
  877. break;
  878. default:
  879. DRM_ERROR("execbuf with unknown ring: %d\n",
  880. (int)(args->flags & I915_EXEC_RING_MASK));
  881. return -EINVAL;
  882. }
  883. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  884. switch (mode) {
  885. case I915_EXEC_CONSTANTS_REL_GENERAL:
  886. case I915_EXEC_CONSTANTS_ABSOLUTE:
  887. case I915_EXEC_CONSTANTS_REL_SURFACE:
  888. if (ring == &dev_priv->ring[RCS] &&
  889. mode != dev_priv->relative_constants_mode) {
  890. if (INTEL_INFO(dev)->gen < 4)
  891. return -EINVAL;
  892. if (INTEL_INFO(dev)->gen > 5 &&
  893. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  894. return -EINVAL;
  895. ret = intel_ring_begin(ring, 4);
  896. if (ret)
  897. return ret;
  898. intel_ring_emit(ring, MI_NOOP);
  899. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  900. intel_ring_emit(ring, INSTPM);
  901. intel_ring_emit(ring,
  902. I915_EXEC_CONSTANTS_MASK << 16 | mode);
  903. intel_ring_advance(ring);
  904. dev_priv->relative_constants_mode = mode;
  905. }
  906. break;
  907. default:
  908. DRM_ERROR("execbuf with unknown constants: %d\n", mode);
  909. return -EINVAL;
  910. }
  911. if (args->buffer_count < 1) {
  912. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  913. return -EINVAL;
  914. }
  915. if (args->num_cliprects != 0) {
  916. if (ring != &dev_priv->ring[RCS]) {
  917. DRM_ERROR("clip rectangles are only valid with the render ring\n");
  918. return -EINVAL;
  919. }
  920. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  921. GFP_KERNEL);
  922. if (cliprects == NULL) {
  923. ret = -ENOMEM;
  924. goto pre_mutex_err;
  925. }
  926. if (copy_from_user(cliprects,
  927. (struct drm_clip_rect __user *)(uintptr_t)
  928. args->cliprects_ptr,
  929. sizeof(*cliprects)*args->num_cliprects)) {
  930. ret = -EFAULT;
  931. goto pre_mutex_err;
  932. }
  933. }
  934. ret = i915_mutex_lock_interruptible(dev);
  935. if (ret)
  936. goto pre_mutex_err;
  937. if (dev_priv->mm.suspended) {
  938. mutex_unlock(&dev->struct_mutex);
  939. ret = -EBUSY;
  940. goto pre_mutex_err;
  941. }
  942. eb = eb_create(args->buffer_count);
  943. if (eb == NULL) {
  944. mutex_unlock(&dev->struct_mutex);
  945. ret = -ENOMEM;
  946. goto pre_mutex_err;
  947. }
  948. /* Look up object handles */
  949. INIT_LIST_HEAD(&objects);
  950. for (i = 0; i < args->buffer_count; i++) {
  951. struct drm_i915_gem_object *obj;
  952. obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  953. exec[i].handle));
  954. if (obj == NULL) {
  955. DRM_ERROR("Invalid object handle %d at index %d\n",
  956. exec[i].handle, i);
  957. /* prevent error path from reading uninitialized data */
  958. ret = -ENOENT;
  959. goto err;
  960. }
  961. if (!list_empty(&obj->exec_list)) {
  962. DRM_ERROR("Object %p [handle %d, index %d] appears more than once in object list\n",
  963. obj, exec[i].handle, i);
  964. ret = -EINVAL;
  965. goto err;
  966. }
  967. list_add_tail(&obj->exec_list, &objects);
  968. obj->exec_handle = exec[i].handle;
  969. eb_add_object(eb, obj);
  970. }
  971. /* Move the objects en-masse into the GTT, evicting if necessary. */
  972. ret = i915_gem_execbuffer_reserve(ring, file, &objects, exec);
  973. if (ret)
  974. goto err;
  975. /* The objects are in their final locations, apply the relocations. */
  976. ret = i915_gem_execbuffer_relocate(dev, eb, &objects, exec);
  977. if (ret) {
  978. if (ret == -EFAULT) {
  979. ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
  980. &objects, eb,
  981. exec,
  982. args->buffer_count);
  983. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  984. }
  985. if (ret)
  986. goto err;
  987. }
  988. /* Set the pending read domains for the batch buffer to COMMAND */
  989. batch_obj = list_entry(objects.prev,
  990. struct drm_i915_gem_object,
  991. exec_list);
  992. if (batch_obj->base.pending_write_domain) {
  993. DRM_ERROR("Attempting to use self-modifying batch buffer\n");
  994. ret = -EINVAL;
  995. goto err;
  996. }
  997. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  998. ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
  999. if (ret)
  1000. goto err;
  1001. ret = i915_gem_execbuffer_wait_for_flips(ring, &objects);
  1002. if (ret)
  1003. goto err;
  1004. seqno = i915_gem_next_request_seqno(dev, ring);
  1005. for (i = 0; i < I915_NUM_RINGS-1; i++) {
  1006. if (seqno < ring->sync_seqno[i]) {
  1007. /* The GPU can not handle its semaphore value wrapping,
  1008. * so every billion or so execbuffers, we need to stall
  1009. * the GPU in order to reset the counters.
  1010. */
  1011. ret = i915_gpu_idle(dev);
  1012. if (ret)
  1013. goto err;
  1014. BUG_ON(ring->sync_seqno[i]);
  1015. }
  1016. }
  1017. exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  1018. exec_len = args->batch_len;
  1019. if (cliprects) {
  1020. for (i = 0; i < args->num_cliprects; i++) {
  1021. ret = i915_emit_box(dev, &cliprects[i],
  1022. args->DR1, args->DR4);
  1023. if (ret)
  1024. goto err;
  1025. ret = ring->dispatch_execbuffer(ring,
  1026. exec_start, exec_len);
  1027. if (ret)
  1028. goto err;
  1029. }
  1030. } else {
  1031. ret = ring->dispatch_execbuffer(ring, exec_start, exec_len);
  1032. if (ret)
  1033. goto err;
  1034. }
  1035. i915_gem_execbuffer_move_to_active(&objects, ring, seqno);
  1036. i915_gem_execbuffer_retire_commands(dev, file, ring);
  1037. err:
  1038. eb_destroy(eb);
  1039. while (!list_empty(&objects)) {
  1040. struct drm_i915_gem_object *obj;
  1041. obj = list_first_entry(&objects,
  1042. struct drm_i915_gem_object,
  1043. exec_list);
  1044. list_del_init(&obj->exec_list);
  1045. drm_gem_object_unreference(&obj->base);
  1046. }
  1047. mutex_unlock(&dev->struct_mutex);
  1048. pre_mutex_err:
  1049. kfree(cliprects);
  1050. return ret;
  1051. }
  1052. /*
  1053. * Legacy execbuffer just creates an exec2 list from the original exec object
  1054. * list array and passes it to the real function.
  1055. */
  1056. int
  1057. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1058. struct drm_file *file)
  1059. {
  1060. struct drm_i915_gem_execbuffer *args = data;
  1061. struct drm_i915_gem_execbuffer2 exec2;
  1062. struct drm_i915_gem_exec_object *exec_list = NULL;
  1063. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1064. int ret, i;
  1065. #if WATCH_EXEC
  1066. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  1067. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  1068. #endif
  1069. if (args->buffer_count < 1) {
  1070. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  1071. return -EINVAL;
  1072. }
  1073. /* Copy in the exec list from userland */
  1074. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1075. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1076. if (exec_list == NULL || exec2_list == NULL) {
  1077. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  1078. args->buffer_count);
  1079. drm_free_large(exec_list);
  1080. drm_free_large(exec2_list);
  1081. return -ENOMEM;
  1082. }
  1083. ret = copy_from_user(exec_list,
  1084. (struct drm_i915_relocation_entry __user *)
  1085. (uintptr_t) args->buffers_ptr,
  1086. sizeof(*exec_list) * args->buffer_count);
  1087. if (ret != 0) {
  1088. DRM_ERROR("copy %d exec entries failed %d\n",
  1089. args->buffer_count, ret);
  1090. drm_free_large(exec_list);
  1091. drm_free_large(exec2_list);
  1092. return -EFAULT;
  1093. }
  1094. for (i = 0; i < args->buffer_count; i++) {
  1095. exec2_list[i].handle = exec_list[i].handle;
  1096. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1097. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1098. exec2_list[i].alignment = exec_list[i].alignment;
  1099. exec2_list[i].offset = exec_list[i].offset;
  1100. if (INTEL_INFO(dev)->gen < 4)
  1101. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1102. else
  1103. exec2_list[i].flags = 0;
  1104. }
  1105. exec2.buffers_ptr = args->buffers_ptr;
  1106. exec2.buffer_count = args->buffer_count;
  1107. exec2.batch_start_offset = args->batch_start_offset;
  1108. exec2.batch_len = args->batch_len;
  1109. exec2.DR1 = args->DR1;
  1110. exec2.DR4 = args->DR4;
  1111. exec2.num_cliprects = args->num_cliprects;
  1112. exec2.cliprects_ptr = args->cliprects_ptr;
  1113. exec2.flags = I915_EXEC_RENDER;
  1114. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1115. if (!ret) {
  1116. /* Copy the new buffer offsets back to the user's exec list. */
  1117. for (i = 0; i < args->buffer_count; i++)
  1118. exec_list[i].offset = exec2_list[i].offset;
  1119. /* ... and back out to userspace */
  1120. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1121. (uintptr_t) args->buffers_ptr,
  1122. exec_list,
  1123. sizeof(*exec_list) * args->buffer_count);
  1124. if (ret) {
  1125. ret = -EFAULT;
  1126. DRM_ERROR("failed to copy %d exec entries "
  1127. "back to user (%d)\n",
  1128. args->buffer_count, ret);
  1129. }
  1130. }
  1131. drm_free_large(exec_list);
  1132. drm_free_large(exec2_list);
  1133. return ret;
  1134. }
  1135. int
  1136. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1137. struct drm_file *file)
  1138. {
  1139. struct drm_i915_gem_execbuffer2 *args = data;
  1140. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1141. int ret;
  1142. #if WATCH_EXEC
  1143. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  1144. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  1145. #endif
  1146. if (args->buffer_count < 1) {
  1147. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  1148. return -EINVAL;
  1149. }
  1150. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1151. if (exec2_list == NULL) {
  1152. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  1153. args->buffer_count);
  1154. return -ENOMEM;
  1155. }
  1156. ret = copy_from_user(exec2_list,
  1157. (struct drm_i915_relocation_entry __user *)
  1158. (uintptr_t) args->buffers_ptr,
  1159. sizeof(*exec2_list) * args->buffer_count);
  1160. if (ret != 0) {
  1161. DRM_ERROR("copy %d exec entries failed %d\n",
  1162. args->buffer_count, ret);
  1163. drm_free_large(exec2_list);
  1164. return -EFAULT;
  1165. }
  1166. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1167. if (!ret) {
  1168. /* Copy the new buffer offsets back to the user's exec list. */
  1169. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1170. (uintptr_t) args->buffers_ptr,
  1171. exec2_list,
  1172. sizeof(*exec2_list) * args->buffer_count);
  1173. if (ret) {
  1174. ret = -EFAULT;
  1175. DRM_ERROR("failed to copy %d exec entries "
  1176. "back to user (%d)\n",
  1177. args->buffer_count, ret);
  1178. }
  1179. }
  1180. drm_free_large(exec2_list);
  1181. return ret;
  1182. }