i915_gem_execbuffer.c 43 KB

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