i915_gem_execbuffer.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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 <drm/drmP.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/dma_remapping.h>
  34. struct eb_vmas {
  35. struct list_head vmas;
  36. int and;
  37. union {
  38. struct i915_vma *lut[0];
  39. struct hlist_head buckets[0];
  40. };
  41. };
  42. static struct eb_vmas *
  43. eb_create(struct drm_i915_gem_execbuffer2 *args, struct i915_address_space *vm)
  44. {
  45. struct eb_vmas *eb = NULL;
  46. if (args->flags & I915_EXEC_HANDLE_LUT) {
  47. unsigned size = args->buffer_count;
  48. size *= sizeof(struct i915_vma *);
  49. size += sizeof(struct eb_vmas);
  50. eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  51. }
  52. if (eb == NULL) {
  53. unsigned size = args->buffer_count;
  54. unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  55. BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
  56. while (count > 2*size)
  57. count >>= 1;
  58. eb = kzalloc(count*sizeof(struct hlist_head) +
  59. sizeof(struct eb_vmas),
  60. GFP_TEMPORARY);
  61. if (eb == NULL)
  62. return eb;
  63. eb->and = count - 1;
  64. } else
  65. eb->and = -args->buffer_count;
  66. INIT_LIST_HEAD(&eb->vmas);
  67. return eb;
  68. }
  69. static void
  70. eb_reset(struct eb_vmas *eb)
  71. {
  72. if (eb->and >= 0)
  73. memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  74. }
  75. static int
  76. eb_lookup_vmas(struct eb_vmas *eb,
  77. struct drm_i915_gem_exec_object2 *exec,
  78. const struct drm_i915_gem_execbuffer2 *args,
  79. struct i915_address_space *vm,
  80. struct drm_file *file)
  81. {
  82. struct drm_i915_gem_object *obj;
  83. struct list_head objects;
  84. int i, ret = 0;
  85. INIT_LIST_HEAD(&objects);
  86. spin_lock(&file->table_lock);
  87. /* Grab a reference to the object and release the lock so we can lookup
  88. * or create the VMA without using GFP_ATOMIC */
  89. for (i = 0; i < args->buffer_count; i++) {
  90. obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
  91. if (obj == NULL) {
  92. spin_unlock(&file->table_lock);
  93. DRM_DEBUG("Invalid object handle %d at index %d\n",
  94. exec[i].handle, i);
  95. ret = -ENOENT;
  96. goto out;
  97. }
  98. if (!list_empty(&obj->obj_exec_link)) {
  99. spin_unlock(&file->table_lock);
  100. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  101. obj, exec[i].handle, i);
  102. ret = -EINVAL;
  103. goto out;
  104. }
  105. drm_gem_object_reference(&obj->base);
  106. list_add_tail(&obj->obj_exec_link, &objects);
  107. }
  108. spin_unlock(&file->table_lock);
  109. i = 0;
  110. list_for_each_entry(obj, &objects, obj_exec_link) {
  111. struct i915_vma *vma;
  112. /*
  113. * NOTE: We can leak any vmas created here when something fails
  114. * later on. But that's no issue since vma_unbind can deal with
  115. * vmas which are not actually bound. And since only
  116. * lookup_or_create exists as an interface to get at the vma
  117. * from the (obj, vm) we don't run the risk of creating
  118. * duplicated vmas for the same vm.
  119. */
  120. vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
  121. if (IS_ERR(vma)) {
  122. DRM_DEBUG("Failed to lookup VMA\n");
  123. ret = PTR_ERR(vma);
  124. goto out;
  125. }
  126. list_add_tail(&vma->exec_list, &eb->vmas);
  127. vma->exec_entry = &exec[i];
  128. if (eb->and < 0) {
  129. eb->lut[i] = vma;
  130. } else {
  131. uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
  132. vma->exec_handle = handle;
  133. hlist_add_head(&vma->exec_node,
  134. &eb->buckets[handle & eb->and]);
  135. }
  136. ++i;
  137. }
  138. out:
  139. while (!list_empty(&objects)) {
  140. obj = list_first_entry(&objects,
  141. struct drm_i915_gem_object,
  142. obj_exec_link);
  143. list_del_init(&obj->obj_exec_link);
  144. if (ret)
  145. drm_gem_object_unreference(&obj->base);
  146. }
  147. return ret;
  148. }
  149. static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
  150. {
  151. if (eb->and < 0) {
  152. if (handle >= -eb->and)
  153. return NULL;
  154. return eb->lut[handle];
  155. } else {
  156. struct hlist_head *head;
  157. struct hlist_node *node;
  158. head = &eb->buckets[handle & eb->and];
  159. hlist_for_each(node, head) {
  160. struct i915_vma *vma;
  161. vma = hlist_entry(node, struct i915_vma, exec_node);
  162. if (vma->exec_handle == handle)
  163. return vma;
  164. }
  165. return NULL;
  166. }
  167. }
  168. static void eb_destroy(struct eb_vmas *eb) {
  169. while (!list_empty(&eb->vmas)) {
  170. struct i915_vma *vma;
  171. vma = list_first_entry(&eb->vmas,
  172. struct i915_vma,
  173. exec_list);
  174. list_del_init(&vma->exec_list);
  175. drm_gem_object_unreference(&vma->obj->base);
  176. }
  177. kfree(eb);
  178. }
  179. static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
  180. {
  181. return (HAS_LLC(obj->base.dev) ||
  182. obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
  183. !obj->map_and_fenceable ||
  184. obj->cache_level != I915_CACHE_NONE);
  185. }
  186. static int
  187. relocate_entry_cpu(struct drm_i915_gem_object *obj,
  188. struct drm_i915_gem_relocation_entry *reloc)
  189. {
  190. struct drm_device *dev = obj->base.dev;
  191. uint32_t page_offset = offset_in_page(reloc->offset);
  192. char *vaddr;
  193. int ret = -EINVAL;
  194. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  195. if (ret)
  196. return ret;
  197. vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  198. reloc->offset >> PAGE_SHIFT));
  199. *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  200. if (INTEL_INFO(dev)->gen >= 8) {
  201. page_offset = offset_in_page(page_offset + sizeof(uint32_t));
  202. if (page_offset == 0) {
  203. kunmap_atomic(vaddr);
  204. vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  205. (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
  206. }
  207. *(uint32_t *)(vaddr + page_offset) = 0;
  208. }
  209. kunmap_atomic(vaddr);
  210. return 0;
  211. }
  212. static int
  213. relocate_entry_gtt(struct drm_i915_gem_object *obj,
  214. struct drm_i915_gem_relocation_entry *reloc)
  215. {
  216. struct drm_device *dev = obj->base.dev;
  217. struct drm_i915_private *dev_priv = dev->dev_private;
  218. uint32_t __iomem *reloc_entry;
  219. void __iomem *reloc_page;
  220. int ret = -EINVAL;
  221. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  222. if (ret)
  223. return ret;
  224. ret = i915_gem_object_put_fence(obj);
  225. if (ret)
  226. return ret;
  227. /* Map the page containing the relocation we're going to perform. */
  228. reloc->offset += i915_gem_obj_ggtt_offset(obj);
  229. reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
  230. reloc->offset & PAGE_MASK);
  231. reloc_entry = (uint32_t __iomem *)
  232. (reloc_page + offset_in_page(reloc->offset));
  233. iowrite32(reloc->delta, reloc_entry);
  234. if (INTEL_INFO(dev)->gen >= 8) {
  235. reloc_entry += 1;
  236. if (offset_in_page(reloc->offset + sizeof(uint32_t)) == 0) {
  237. io_mapping_unmap_atomic(reloc_page);
  238. reloc_page = io_mapping_map_atomic_wc(
  239. dev_priv->gtt.mappable,
  240. reloc->offset + sizeof(uint32_t));
  241. reloc_entry = reloc_page;
  242. }
  243. iowrite32(0, reloc_entry);
  244. }
  245. io_mapping_unmap_atomic(reloc_page);
  246. return 0;
  247. }
  248. static int
  249. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  250. struct eb_vmas *eb,
  251. struct drm_i915_gem_relocation_entry *reloc,
  252. struct i915_address_space *vm)
  253. {
  254. struct drm_device *dev = obj->base.dev;
  255. struct drm_gem_object *target_obj;
  256. struct drm_i915_gem_object *target_i915_obj;
  257. struct i915_vma *target_vma;
  258. uint32_t target_offset;
  259. int ret = -EINVAL;
  260. /* we've already hold a reference to all valid objects */
  261. target_vma = eb_get_vma(eb, reloc->target_handle);
  262. if (unlikely(target_vma == NULL))
  263. return -ENOENT;
  264. target_i915_obj = target_vma->obj;
  265. target_obj = &target_vma->obj->base;
  266. target_offset = i915_gem_obj_ggtt_offset(target_i915_obj);
  267. /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
  268. * pipe_control writes because the gpu doesn't properly redirect them
  269. * through the ppgtt for non_secure batchbuffers. */
  270. if (unlikely(IS_GEN6(dev) &&
  271. reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
  272. !target_i915_obj->has_global_gtt_mapping)) {
  273. i915_gem_gtt_bind_object(target_i915_obj,
  274. target_i915_obj->cache_level);
  275. }
  276. /* Validate that the target is in a valid r/w GPU domain */
  277. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  278. DRM_DEBUG("reloc with multiple write domains: "
  279. "obj %p target %d offset %d "
  280. "read %08x write %08x",
  281. obj, reloc->target_handle,
  282. (int) reloc->offset,
  283. reloc->read_domains,
  284. reloc->write_domain);
  285. return ret;
  286. }
  287. if (unlikely((reloc->write_domain | reloc->read_domains)
  288. & ~I915_GEM_GPU_DOMAINS)) {
  289. DRM_DEBUG("reloc with read/write non-GPU 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. target_obj->pending_read_domains |= reloc->read_domains;
  299. target_obj->pending_write_domain |= reloc->write_domain;
  300. /* If the relocation already has the right value in it, no
  301. * more work needs to be done.
  302. */
  303. if (target_offset == reloc->presumed_offset)
  304. return 0;
  305. /* Check that the relocation address is valid... */
  306. if (unlikely(reloc->offset >
  307. obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
  308. DRM_DEBUG("Relocation beyond object bounds: "
  309. "obj %p target %d offset %d size %d.\n",
  310. obj, reloc->target_handle,
  311. (int) reloc->offset,
  312. (int) obj->base.size);
  313. return ret;
  314. }
  315. if (unlikely(reloc->offset & 3)) {
  316. DRM_DEBUG("Relocation not 4-byte aligned: "
  317. "obj %p target %d offset %d.\n",
  318. obj, reloc->target_handle,
  319. (int) reloc->offset);
  320. return ret;
  321. }
  322. /* We can't wait for rendering with pagefaults disabled */
  323. if (obj->active && in_atomic())
  324. return -EFAULT;
  325. reloc->delta += target_offset;
  326. if (use_cpu_reloc(obj))
  327. ret = relocate_entry_cpu(obj, reloc);
  328. else
  329. ret = relocate_entry_gtt(obj, reloc);
  330. if (ret)
  331. return ret;
  332. /* and update the user's relocation entry */
  333. reloc->presumed_offset = target_offset;
  334. return 0;
  335. }
  336. static int
  337. i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
  338. struct eb_vmas *eb)
  339. {
  340. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  341. struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
  342. struct drm_i915_gem_relocation_entry __user *user_relocs;
  343. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  344. int remain, ret;
  345. user_relocs = to_user_ptr(entry->relocs_ptr);
  346. remain = entry->relocation_count;
  347. while (remain) {
  348. struct drm_i915_gem_relocation_entry *r = stack_reloc;
  349. int count = remain;
  350. if (count > ARRAY_SIZE(stack_reloc))
  351. count = ARRAY_SIZE(stack_reloc);
  352. remain -= count;
  353. if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
  354. return -EFAULT;
  355. do {
  356. u64 offset = r->presumed_offset;
  357. ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r,
  358. vma->vm);
  359. if (ret)
  360. return ret;
  361. if (r->presumed_offset != offset &&
  362. __copy_to_user_inatomic(&user_relocs->presumed_offset,
  363. &r->presumed_offset,
  364. sizeof(r->presumed_offset))) {
  365. return -EFAULT;
  366. }
  367. user_relocs++;
  368. r++;
  369. } while (--count);
  370. }
  371. return 0;
  372. #undef N_RELOC
  373. }
  374. static int
  375. i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
  376. struct eb_vmas *eb,
  377. struct drm_i915_gem_relocation_entry *relocs)
  378. {
  379. const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  380. int i, ret;
  381. for (i = 0; i < entry->relocation_count; i++) {
  382. ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i],
  383. vma->vm);
  384. if (ret)
  385. return ret;
  386. }
  387. return 0;
  388. }
  389. static int
  390. i915_gem_execbuffer_relocate(struct eb_vmas *eb,
  391. struct i915_address_space *vm)
  392. {
  393. struct i915_vma *vma;
  394. int ret = 0;
  395. /* This is the fast path and we cannot handle a pagefault whilst
  396. * holding the struct mutex lest the user pass in the relocations
  397. * contained within a mmaped bo. For in such a case we, the page
  398. * fault handler would call i915_gem_fault() and we would try to
  399. * acquire the struct mutex again. Obviously this is bad and so
  400. * lockdep complains vehemently.
  401. */
  402. pagefault_disable();
  403. list_for_each_entry(vma, &eb->vmas, exec_list) {
  404. ret = i915_gem_execbuffer_relocate_vma(vma, eb);
  405. if (ret)
  406. break;
  407. }
  408. pagefault_enable();
  409. return ret;
  410. }
  411. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  412. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  413. static int
  414. need_reloc_mappable(struct i915_vma *vma)
  415. {
  416. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  417. return entry->relocation_count && !use_cpu_reloc(vma->obj) &&
  418. i915_is_ggtt(vma->vm);
  419. }
  420. static int
  421. i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
  422. struct intel_ring_buffer *ring,
  423. bool *need_reloc)
  424. {
  425. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  426. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  427. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  428. bool need_fence, need_mappable;
  429. struct drm_i915_gem_object *obj = vma->obj;
  430. int ret;
  431. need_fence =
  432. has_fenced_gpu_access &&
  433. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  434. obj->tiling_mode != I915_TILING_NONE;
  435. need_mappable = need_fence || need_reloc_mappable(vma);
  436. ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, need_mappable,
  437. false);
  438. if (ret)
  439. return ret;
  440. entry->flags |= __EXEC_OBJECT_HAS_PIN;
  441. if (has_fenced_gpu_access) {
  442. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  443. ret = i915_gem_object_get_fence(obj);
  444. if (ret)
  445. return ret;
  446. if (i915_gem_object_pin_fence(obj))
  447. entry->flags |= __EXEC_OBJECT_HAS_FENCE;
  448. obj->pending_fenced_gpu_access = true;
  449. }
  450. }
  451. /* Ensure ppgtt mapping exists if needed */
  452. if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
  453. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  454. obj, obj->cache_level);
  455. obj->has_aliasing_ppgtt_mapping = 1;
  456. }
  457. if (entry->offset != vma->node.start) {
  458. entry->offset = vma->node.start;
  459. *need_reloc = true;
  460. }
  461. if (entry->flags & EXEC_OBJECT_WRITE) {
  462. obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
  463. obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
  464. }
  465. if (entry->flags & EXEC_OBJECT_NEEDS_GTT &&
  466. !obj->has_global_gtt_mapping)
  467. i915_gem_gtt_bind_object(obj, obj->cache_level);
  468. return 0;
  469. }
  470. static void
  471. i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
  472. {
  473. struct drm_i915_gem_exec_object2 *entry;
  474. struct drm_i915_gem_object *obj = vma->obj;
  475. if (!drm_mm_node_allocated(&vma->node))
  476. return;
  477. entry = vma->exec_entry;
  478. if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
  479. i915_gem_object_unpin_fence(obj);
  480. if (entry->flags & __EXEC_OBJECT_HAS_PIN)
  481. i915_gem_object_unpin(obj);
  482. entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  483. }
  484. static int
  485. i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
  486. struct list_head *vmas,
  487. bool *need_relocs)
  488. {
  489. struct drm_i915_gem_object *obj;
  490. struct i915_vma *vma;
  491. struct i915_address_space *vm;
  492. struct list_head ordered_vmas;
  493. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  494. int retry;
  495. if (list_empty(vmas))
  496. return 0;
  497. vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
  498. INIT_LIST_HEAD(&ordered_vmas);
  499. while (!list_empty(vmas)) {
  500. struct drm_i915_gem_exec_object2 *entry;
  501. bool need_fence, need_mappable;
  502. vma = list_first_entry(vmas, struct i915_vma, exec_list);
  503. obj = vma->obj;
  504. entry = vma->exec_entry;
  505. need_fence =
  506. has_fenced_gpu_access &&
  507. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  508. obj->tiling_mode != I915_TILING_NONE;
  509. need_mappable = need_fence || need_reloc_mappable(vma);
  510. if (need_mappable)
  511. list_move(&vma->exec_list, &ordered_vmas);
  512. else
  513. list_move_tail(&vma->exec_list, &ordered_vmas);
  514. obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  515. obj->base.pending_write_domain = 0;
  516. obj->pending_fenced_gpu_access = false;
  517. }
  518. list_splice(&ordered_vmas, vmas);
  519. /* Attempt to pin all of the buffers into the GTT.
  520. * This is done in 3 phases:
  521. *
  522. * 1a. Unbind all objects that do not match the GTT constraints for
  523. * the execbuffer (fenceable, mappable, alignment etc).
  524. * 1b. Increment pin count for already bound objects.
  525. * 2. Bind new objects.
  526. * 3. Decrement pin count.
  527. *
  528. * This avoid unnecessary unbinding of later objects in order to make
  529. * room for the earlier objects *unless* we need to defragment.
  530. */
  531. retry = 0;
  532. do {
  533. int ret = 0;
  534. /* Unbind any ill-fitting objects or pin. */
  535. list_for_each_entry(vma, vmas, exec_list) {
  536. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  537. bool need_fence, need_mappable;
  538. obj = vma->obj;
  539. if (!drm_mm_node_allocated(&vma->node))
  540. continue;
  541. need_fence =
  542. has_fenced_gpu_access &&
  543. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  544. obj->tiling_mode != I915_TILING_NONE;
  545. need_mappable = need_fence || need_reloc_mappable(vma);
  546. WARN_ON((need_mappable || need_fence) &&
  547. !i915_is_ggtt(vma->vm));
  548. if ((entry->alignment &&
  549. vma->node.start & (entry->alignment - 1)) ||
  550. (need_mappable && !obj->map_and_fenceable))
  551. ret = i915_vma_unbind(vma);
  552. else
  553. ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  554. if (ret)
  555. goto err;
  556. }
  557. /* Bind fresh objects */
  558. list_for_each_entry(vma, vmas, exec_list) {
  559. if (drm_mm_node_allocated(&vma->node))
  560. continue;
  561. ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  562. if (ret)
  563. goto err;
  564. }
  565. err: /* Decrement pin count for bound objects */
  566. list_for_each_entry(vma, vmas, exec_list)
  567. i915_gem_execbuffer_unreserve_vma(vma);
  568. if (ret != -ENOSPC || retry++)
  569. return ret;
  570. ret = i915_gem_evict_vm(vm, true);
  571. if (ret)
  572. return ret;
  573. } while (1);
  574. }
  575. static int
  576. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  577. struct drm_i915_gem_execbuffer2 *args,
  578. struct drm_file *file,
  579. struct intel_ring_buffer *ring,
  580. struct eb_vmas *eb,
  581. struct drm_i915_gem_exec_object2 *exec)
  582. {
  583. struct drm_i915_gem_relocation_entry *reloc;
  584. struct i915_address_space *vm;
  585. struct i915_vma *vma;
  586. bool need_relocs;
  587. int *reloc_offset;
  588. int i, total, ret;
  589. unsigned count = args->buffer_count;
  590. if (WARN_ON(list_empty(&eb->vmas)))
  591. return 0;
  592. vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
  593. /* We may process another execbuffer during the unlock... */
  594. while (!list_empty(&eb->vmas)) {
  595. vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
  596. list_del_init(&vma->exec_list);
  597. drm_gem_object_unreference(&vma->obj->base);
  598. }
  599. mutex_unlock(&dev->struct_mutex);
  600. total = 0;
  601. for (i = 0; i < count; i++)
  602. total += exec[i].relocation_count;
  603. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  604. reloc = drm_malloc_ab(total, sizeof(*reloc));
  605. if (reloc == NULL || reloc_offset == NULL) {
  606. drm_free_large(reloc);
  607. drm_free_large(reloc_offset);
  608. mutex_lock(&dev->struct_mutex);
  609. return -ENOMEM;
  610. }
  611. total = 0;
  612. for (i = 0; i < count; i++) {
  613. struct drm_i915_gem_relocation_entry __user *user_relocs;
  614. u64 invalid_offset = (u64)-1;
  615. int j;
  616. user_relocs = to_user_ptr(exec[i].relocs_ptr);
  617. if (copy_from_user(reloc+total, user_relocs,
  618. exec[i].relocation_count * sizeof(*reloc))) {
  619. ret = -EFAULT;
  620. mutex_lock(&dev->struct_mutex);
  621. goto err;
  622. }
  623. /* As we do not update the known relocation offsets after
  624. * relocating (due to the complexities in lock handling),
  625. * we need to mark them as invalid now so that we force the
  626. * relocation processing next time. Just in case the target
  627. * object is evicted and then rebound into its old
  628. * presumed_offset before the next execbuffer - if that
  629. * happened we would make the mistake of assuming that the
  630. * relocations were valid.
  631. */
  632. for (j = 0; j < exec[i].relocation_count; j++) {
  633. if (copy_to_user(&user_relocs[j].presumed_offset,
  634. &invalid_offset,
  635. sizeof(invalid_offset))) {
  636. ret = -EFAULT;
  637. mutex_lock(&dev->struct_mutex);
  638. goto err;
  639. }
  640. }
  641. reloc_offset[i] = total;
  642. total += exec[i].relocation_count;
  643. }
  644. ret = i915_mutex_lock_interruptible(dev);
  645. if (ret) {
  646. mutex_lock(&dev->struct_mutex);
  647. goto err;
  648. }
  649. /* reacquire the objects */
  650. eb_reset(eb);
  651. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  652. if (ret)
  653. goto err;
  654. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  655. ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
  656. if (ret)
  657. goto err;
  658. list_for_each_entry(vma, &eb->vmas, exec_list) {
  659. int offset = vma->exec_entry - exec;
  660. ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
  661. reloc + reloc_offset[offset]);
  662. if (ret)
  663. goto err;
  664. }
  665. /* Leave the user relocations as are, this is the painfully slow path,
  666. * and we want to avoid the complication of dropping the lock whilst
  667. * having buffers reserved in the aperture and so causing spurious
  668. * ENOSPC for random operations.
  669. */
  670. err:
  671. drm_free_large(reloc);
  672. drm_free_large(reloc_offset);
  673. return ret;
  674. }
  675. static int
  676. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  677. struct list_head *vmas)
  678. {
  679. struct i915_vma *vma;
  680. uint32_t flush_domains = 0;
  681. bool flush_chipset = false;
  682. int ret;
  683. list_for_each_entry(vma, vmas, exec_list) {
  684. struct drm_i915_gem_object *obj = vma->obj;
  685. ret = i915_gem_object_sync(obj, ring);
  686. if (ret)
  687. return ret;
  688. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  689. flush_chipset |= i915_gem_clflush_object(obj, false);
  690. flush_domains |= obj->base.write_domain;
  691. }
  692. if (flush_chipset)
  693. i915_gem_chipset_flush(ring->dev);
  694. if (flush_domains & I915_GEM_DOMAIN_GTT)
  695. wmb();
  696. /* Unconditionally invalidate gpu caches and ensure that we do flush
  697. * any residual writes from the previous batch.
  698. */
  699. return intel_ring_invalidate_all_caches(ring);
  700. }
  701. static bool
  702. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  703. {
  704. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  705. return false;
  706. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  707. }
  708. static int
  709. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  710. int count)
  711. {
  712. int i;
  713. unsigned relocs_total = 0;
  714. unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  715. for (i = 0; i < count; i++) {
  716. char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
  717. int length; /* limited by fault_in_pages_readable() */
  718. if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
  719. return -EINVAL;
  720. /* First check for malicious input causing overflow in
  721. * the worst case where we need to allocate the entire
  722. * relocation tree as a single array.
  723. */
  724. if (exec[i].relocation_count > relocs_max - relocs_total)
  725. return -EINVAL;
  726. relocs_total += exec[i].relocation_count;
  727. length = exec[i].relocation_count *
  728. sizeof(struct drm_i915_gem_relocation_entry);
  729. /*
  730. * We must check that the entire relocation array is safe
  731. * to read, but since we may need to update the presumed
  732. * offsets during execution, check for full write access.
  733. */
  734. if (!access_ok(VERIFY_WRITE, ptr, length))
  735. return -EFAULT;
  736. if (likely(!i915_prefault_disable)) {
  737. if (fault_in_multipages_readable(ptr, length))
  738. return -EFAULT;
  739. }
  740. }
  741. return 0;
  742. }
  743. static void
  744. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  745. struct intel_ring_buffer *ring)
  746. {
  747. struct i915_vma *vma;
  748. list_for_each_entry(vma, vmas, exec_list) {
  749. struct drm_i915_gem_object *obj = vma->obj;
  750. u32 old_read = obj->base.read_domains;
  751. u32 old_write = obj->base.write_domain;
  752. obj->base.write_domain = obj->base.pending_write_domain;
  753. if (obj->base.write_domain == 0)
  754. obj->base.pending_read_domains |= obj->base.read_domains;
  755. obj->base.read_domains = obj->base.pending_read_domains;
  756. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  757. i915_vma_move_to_active(vma, ring);
  758. if (obj->base.write_domain) {
  759. obj->dirty = 1;
  760. obj->last_write_seqno = intel_ring_get_seqno(ring);
  761. if (obj->pin_count) /* check for potential scanout */
  762. intel_mark_fb_busy(obj, ring);
  763. }
  764. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  765. }
  766. }
  767. static void
  768. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  769. struct drm_file *file,
  770. struct intel_ring_buffer *ring,
  771. struct drm_i915_gem_object *obj)
  772. {
  773. /* Unconditionally force add_request to emit a full flush. */
  774. ring->gpu_caches_dirty = true;
  775. /* Add a breadcrumb for the completion of the batch buffer */
  776. (void)__i915_add_request(ring, file, obj, NULL);
  777. }
  778. static int
  779. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  780. struct intel_ring_buffer *ring)
  781. {
  782. drm_i915_private_t *dev_priv = dev->dev_private;
  783. int ret, i;
  784. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
  785. return 0;
  786. ret = intel_ring_begin(ring, 4 * 3);
  787. if (ret)
  788. return ret;
  789. for (i = 0; i < 4; i++) {
  790. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  791. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  792. intel_ring_emit(ring, 0);
  793. }
  794. intel_ring_advance(ring);
  795. return 0;
  796. }
  797. static int
  798. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  799. struct drm_file *file,
  800. struct drm_i915_gem_execbuffer2 *args,
  801. struct drm_i915_gem_exec_object2 *exec,
  802. struct i915_address_space *vm)
  803. {
  804. drm_i915_private_t *dev_priv = dev->dev_private;
  805. struct eb_vmas *eb;
  806. struct drm_i915_gem_object *batch_obj;
  807. struct drm_clip_rect *cliprects = NULL;
  808. struct intel_ring_buffer *ring;
  809. struct i915_ctx_hang_stats *hs;
  810. u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  811. u32 exec_start, exec_len;
  812. u32 mask, flags;
  813. int ret, mode, i;
  814. bool need_relocs;
  815. if (!i915_gem_check_execbuffer(args))
  816. return -EINVAL;
  817. ret = validate_exec_list(exec, args->buffer_count);
  818. if (ret)
  819. return ret;
  820. flags = 0;
  821. if (args->flags & I915_EXEC_SECURE) {
  822. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  823. return -EPERM;
  824. flags |= I915_DISPATCH_SECURE;
  825. }
  826. if (args->flags & I915_EXEC_IS_PINNED)
  827. flags |= I915_DISPATCH_PINNED;
  828. switch (args->flags & I915_EXEC_RING_MASK) {
  829. case I915_EXEC_DEFAULT:
  830. case I915_EXEC_RENDER:
  831. ring = &dev_priv->ring[RCS];
  832. break;
  833. case I915_EXEC_BSD:
  834. ring = &dev_priv->ring[VCS];
  835. if (ctx_id != DEFAULT_CONTEXT_ID) {
  836. DRM_DEBUG("Ring %s doesn't support contexts\n",
  837. ring->name);
  838. return -EPERM;
  839. }
  840. break;
  841. case I915_EXEC_BLT:
  842. ring = &dev_priv->ring[BCS];
  843. if (ctx_id != DEFAULT_CONTEXT_ID) {
  844. DRM_DEBUG("Ring %s doesn't support contexts\n",
  845. ring->name);
  846. return -EPERM;
  847. }
  848. break;
  849. case I915_EXEC_VEBOX:
  850. ring = &dev_priv->ring[VECS];
  851. if (ctx_id != DEFAULT_CONTEXT_ID) {
  852. DRM_DEBUG("Ring %s doesn't support contexts\n",
  853. ring->name);
  854. return -EPERM;
  855. }
  856. break;
  857. default:
  858. DRM_DEBUG("execbuf with unknown ring: %d\n",
  859. (int)(args->flags & I915_EXEC_RING_MASK));
  860. return -EINVAL;
  861. }
  862. if (!intel_ring_initialized(ring)) {
  863. DRM_DEBUG("execbuf with invalid ring: %d\n",
  864. (int)(args->flags & I915_EXEC_RING_MASK));
  865. return -EINVAL;
  866. }
  867. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  868. mask = I915_EXEC_CONSTANTS_MASK;
  869. switch (mode) {
  870. case I915_EXEC_CONSTANTS_REL_GENERAL:
  871. case I915_EXEC_CONSTANTS_ABSOLUTE:
  872. case I915_EXEC_CONSTANTS_REL_SURFACE:
  873. if (ring == &dev_priv->ring[RCS] &&
  874. mode != dev_priv->relative_constants_mode) {
  875. if (INTEL_INFO(dev)->gen < 4)
  876. return -EINVAL;
  877. if (INTEL_INFO(dev)->gen > 5 &&
  878. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  879. return -EINVAL;
  880. /* The HW changed the meaning on this bit on gen6 */
  881. if (INTEL_INFO(dev)->gen >= 6)
  882. mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  883. }
  884. break;
  885. default:
  886. DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
  887. return -EINVAL;
  888. }
  889. if (args->buffer_count < 1) {
  890. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  891. return -EINVAL;
  892. }
  893. if (args->num_cliprects != 0) {
  894. if (ring != &dev_priv->ring[RCS]) {
  895. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  896. return -EINVAL;
  897. }
  898. if (INTEL_INFO(dev)->gen >= 5) {
  899. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  900. return -EINVAL;
  901. }
  902. if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  903. DRM_DEBUG("execbuf with %u cliprects\n",
  904. args->num_cliprects);
  905. return -EINVAL;
  906. }
  907. cliprects = kcalloc(args->num_cliprects,
  908. sizeof(*cliprects),
  909. GFP_KERNEL);
  910. if (cliprects == NULL) {
  911. ret = -ENOMEM;
  912. goto pre_mutex_err;
  913. }
  914. if (copy_from_user(cliprects,
  915. to_user_ptr(args->cliprects_ptr),
  916. sizeof(*cliprects)*args->num_cliprects)) {
  917. ret = -EFAULT;
  918. goto pre_mutex_err;
  919. }
  920. }
  921. ret = i915_mutex_lock_interruptible(dev);
  922. if (ret)
  923. goto pre_mutex_err;
  924. if (dev_priv->ums.mm_suspended) {
  925. mutex_unlock(&dev->struct_mutex);
  926. ret = -EBUSY;
  927. goto pre_mutex_err;
  928. }
  929. eb = eb_create(args, vm);
  930. if (eb == NULL) {
  931. mutex_unlock(&dev->struct_mutex);
  932. ret = -ENOMEM;
  933. goto pre_mutex_err;
  934. }
  935. /* Look up object handles */
  936. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  937. if (ret)
  938. goto err;
  939. /* take note of the batch buffer before we might reorder the lists */
  940. batch_obj = list_entry(eb->vmas.prev, struct i915_vma, exec_list)->obj;
  941. /* Move the objects en-masse into the GTT, evicting if necessary. */
  942. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  943. ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
  944. if (ret)
  945. goto err;
  946. /* The objects are in their final locations, apply the relocations. */
  947. if (need_relocs)
  948. ret = i915_gem_execbuffer_relocate(eb, vm);
  949. if (ret) {
  950. if (ret == -EFAULT) {
  951. ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
  952. eb, exec);
  953. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  954. }
  955. if (ret)
  956. goto err;
  957. }
  958. /* Set the pending read domains for the batch buffer to COMMAND */
  959. if (batch_obj->base.pending_write_domain) {
  960. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  961. ret = -EINVAL;
  962. goto err;
  963. }
  964. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  965. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  966. * batch" bit. Hence we need to pin secure batches into the global gtt.
  967. * hsw should have this fixed, but bdw mucks it up again. */
  968. if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
  969. i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
  970. ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->vmas);
  971. if (ret)
  972. goto err;
  973. hs = i915_gem_context_get_hang_stats(dev, file, ctx_id);
  974. if (IS_ERR(hs)) {
  975. ret = PTR_ERR(hs);
  976. goto err;
  977. }
  978. if (hs->banned) {
  979. ret = -EIO;
  980. goto err;
  981. }
  982. ret = i915_switch_context(ring, file, ctx_id);
  983. if (ret)
  984. goto err;
  985. if (ring == &dev_priv->ring[RCS] &&
  986. mode != dev_priv->relative_constants_mode) {
  987. ret = intel_ring_begin(ring, 4);
  988. if (ret)
  989. goto err;
  990. intel_ring_emit(ring, MI_NOOP);
  991. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  992. intel_ring_emit(ring, INSTPM);
  993. intel_ring_emit(ring, mask << 16 | mode);
  994. intel_ring_advance(ring);
  995. dev_priv->relative_constants_mode = mode;
  996. }
  997. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  998. ret = i915_reset_gen7_sol_offsets(dev, ring);
  999. if (ret)
  1000. goto err;
  1001. }
  1002. exec_start = i915_gem_obj_offset(batch_obj, vm) +
  1003. args->batch_start_offset;
  1004. exec_len = args->batch_len;
  1005. if (cliprects) {
  1006. for (i = 0; i < args->num_cliprects; i++) {
  1007. ret = i915_emit_box(dev, &cliprects[i],
  1008. args->DR1, args->DR4);
  1009. if (ret)
  1010. goto err;
  1011. ret = ring->dispatch_execbuffer(ring,
  1012. exec_start, exec_len,
  1013. flags);
  1014. if (ret)
  1015. goto err;
  1016. }
  1017. } else {
  1018. ret = ring->dispatch_execbuffer(ring,
  1019. exec_start, exec_len,
  1020. flags);
  1021. if (ret)
  1022. goto err;
  1023. }
  1024. trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
  1025. i915_gem_execbuffer_move_to_active(&eb->vmas, ring);
  1026. i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  1027. err:
  1028. eb_destroy(eb);
  1029. mutex_unlock(&dev->struct_mutex);
  1030. pre_mutex_err:
  1031. kfree(cliprects);
  1032. return ret;
  1033. }
  1034. /*
  1035. * Legacy execbuffer just creates an exec2 list from the original exec object
  1036. * list array and passes it to the real function.
  1037. */
  1038. int
  1039. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1040. struct drm_file *file)
  1041. {
  1042. struct drm_i915_private *dev_priv = dev->dev_private;
  1043. struct drm_i915_gem_execbuffer *args = data;
  1044. struct drm_i915_gem_execbuffer2 exec2;
  1045. struct drm_i915_gem_exec_object *exec_list = NULL;
  1046. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1047. int ret, i;
  1048. if (args->buffer_count < 1) {
  1049. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1050. return -EINVAL;
  1051. }
  1052. /* Copy in the exec list from userland */
  1053. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1054. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1055. if (exec_list == NULL || exec2_list == NULL) {
  1056. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1057. args->buffer_count);
  1058. drm_free_large(exec_list);
  1059. drm_free_large(exec2_list);
  1060. return -ENOMEM;
  1061. }
  1062. ret = copy_from_user(exec_list,
  1063. to_user_ptr(args->buffers_ptr),
  1064. sizeof(*exec_list) * args->buffer_count);
  1065. if (ret != 0) {
  1066. DRM_DEBUG("copy %d exec entries failed %d\n",
  1067. args->buffer_count, ret);
  1068. drm_free_large(exec_list);
  1069. drm_free_large(exec2_list);
  1070. return -EFAULT;
  1071. }
  1072. for (i = 0; i < args->buffer_count; i++) {
  1073. exec2_list[i].handle = exec_list[i].handle;
  1074. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1075. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1076. exec2_list[i].alignment = exec_list[i].alignment;
  1077. exec2_list[i].offset = exec_list[i].offset;
  1078. if (INTEL_INFO(dev)->gen < 4)
  1079. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1080. else
  1081. exec2_list[i].flags = 0;
  1082. }
  1083. exec2.buffers_ptr = args->buffers_ptr;
  1084. exec2.buffer_count = args->buffer_count;
  1085. exec2.batch_start_offset = args->batch_start_offset;
  1086. exec2.batch_len = args->batch_len;
  1087. exec2.DR1 = args->DR1;
  1088. exec2.DR4 = args->DR4;
  1089. exec2.num_cliprects = args->num_cliprects;
  1090. exec2.cliprects_ptr = args->cliprects_ptr;
  1091. exec2.flags = I915_EXEC_RENDER;
  1092. i915_execbuffer2_set_context_id(exec2, 0);
  1093. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list,
  1094. &dev_priv->gtt.base);
  1095. if (!ret) {
  1096. /* Copy the new buffer offsets back to the user's exec list. */
  1097. for (i = 0; i < args->buffer_count; i++)
  1098. exec_list[i].offset = exec2_list[i].offset;
  1099. /* ... and back out to userspace */
  1100. ret = copy_to_user(to_user_ptr(args->buffers_ptr),
  1101. exec_list,
  1102. sizeof(*exec_list) * args->buffer_count);
  1103. if (ret) {
  1104. ret = -EFAULT;
  1105. DRM_DEBUG("failed to copy %d exec entries "
  1106. "back to user (%d)\n",
  1107. args->buffer_count, ret);
  1108. }
  1109. }
  1110. drm_free_large(exec_list);
  1111. drm_free_large(exec2_list);
  1112. return ret;
  1113. }
  1114. int
  1115. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1116. struct drm_file *file)
  1117. {
  1118. struct drm_i915_private *dev_priv = dev->dev_private;
  1119. struct drm_i915_gem_execbuffer2 *args = data;
  1120. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1121. int ret;
  1122. if (args->buffer_count < 1 ||
  1123. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1124. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1125. return -EINVAL;
  1126. }
  1127. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1128. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  1129. if (exec2_list == NULL)
  1130. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1131. args->buffer_count);
  1132. if (exec2_list == NULL) {
  1133. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1134. args->buffer_count);
  1135. return -ENOMEM;
  1136. }
  1137. ret = copy_from_user(exec2_list,
  1138. to_user_ptr(args->buffers_ptr),
  1139. sizeof(*exec2_list) * args->buffer_count);
  1140. if (ret != 0) {
  1141. DRM_DEBUG("copy %d exec entries failed %d\n",
  1142. args->buffer_count, ret);
  1143. drm_free_large(exec2_list);
  1144. return -EFAULT;
  1145. }
  1146. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list,
  1147. &dev_priv->gtt.base);
  1148. if (!ret) {
  1149. /* Copy the new buffer offsets back to the user's exec list. */
  1150. ret = copy_to_user(to_user_ptr(args->buffers_ptr),
  1151. exec2_list,
  1152. sizeof(*exec2_list) * args->buffer_count);
  1153. if (ret) {
  1154. ret = -EFAULT;
  1155. DRM_DEBUG("failed to copy %d exec entries "
  1156. "back to user (%d)\n",
  1157. args->buffer_count, ret);
  1158. }
  1159. }
  1160. drm_free_large(exec2_list);
  1161. return ret;
  1162. }