i915_gem_execbuffer.c 52 KB

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