i915_gem_execbuffer.c 50 KB

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