i915_gem_execbuffer.c 50 KB

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