i915_gem_execbuffer.c 38 KB

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