i915_gem_execbuffer.c 52 KB

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