i915_gem_execbuffer.c 42 KB

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