i915_gem_execbuffer.c 49 KB

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