i915_gem_execbuffer.c 49 KB

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