i915_gem_execbuffer.c 47 KB

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