i915_gem_execbuffer.c 41 KB

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