i915_gem_execbuffer.c 49 KB

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