i915_gem_execbuffer.c 42 KB

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