i915_gem_execbuffer.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
  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. struct intel_context *ctx,
  567. bool *need_relocs)
  568. {
  569. struct drm_i915_gem_object *obj;
  570. struct i915_vma *vma;
  571. struct i915_address_space *vm;
  572. struct list_head ordered_vmas;
  573. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  574. int retry;
  575. i915_gem_retire_requests_ring(ring);
  576. vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
  577. INIT_LIST_HEAD(&ordered_vmas);
  578. while (!list_empty(vmas)) {
  579. struct drm_i915_gem_exec_object2 *entry;
  580. bool need_fence, need_mappable;
  581. vma = list_first_entry(vmas, struct i915_vma, exec_list);
  582. obj = vma->obj;
  583. entry = vma->exec_entry;
  584. if (ctx->flags & CONTEXT_NO_ZEROMAP)
  585. entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  586. if (!has_fenced_gpu_access)
  587. entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
  588. need_fence =
  589. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  590. obj->tiling_mode != I915_TILING_NONE;
  591. need_mappable = need_fence || need_reloc_mappable(vma);
  592. if (need_mappable) {
  593. entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
  594. list_move(&vma->exec_list, &ordered_vmas);
  595. } else
  596. list_move_tail(&vma->exec_list, &ordered_vmas);
  597. obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  598. obj->base.pending_write_domain = 0;
  599. }
  600. list_splice(&ordered_vmas, vmas);
  601. /* Attempt to pin all of the buffers into the GTT.
  602. * This is done in 3 phases:
  603. *
  604. * 1a. Unbind all objects that do not match the GTT constraints for
  605. * the execbuffer (fenceable, mappable, alignment etc).
  606. * 1b. Increment pin count for already bound objects.
  607. * 2. Bind new objects.
  608. * 3. Decrement pin count.
  609. *
  610. * This avoid unnecessary unbinding of later objects in order to make
  611. * room for the earlier objects *unless* we need to defragment.
  612. */
  613. retry = 0;
  614. do {
  615. int ret = 0;
  616. /* Unbind any ill-fitting objects or pin. */
  617. list_for_each_entry(vma, vmas, exec_list) {
  618. if (!drm_mm_node_allocated(&vma->node))
  619. continue;
  620. if (eb_vma_misplaced(vma))
  621. ret = i915_vma_unbind(vma);
  622. else
  623. ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  624. if (ret)
  625. goto err;
  626. }
  627. /* Bind fresh objects */
  628. list_for_each_entry(vma, vmas, exec_list) {
  629. if (drm_mm_node_allocated(&vma->node))
  630. continue;
  631. ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  632. if (ret)
  633. goto err;
  634. }
  635. err:
  636. if (ret != -ENOSPC || retry++)
  637. return ret;
  638. /* Decrement pin count for bound objects */
  639. list_for_each_entry(vma, vmas, exec_list)
  640. i915_gem_execbuffer_unreserve_vma(vma);
  641. ret = i915_gem_evict_vm(vm, true);
  642. if (ret)
  643. return ret;
  644. } while (1);
  645. }
  646. static int
  647. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  648. struct drm_i915_gem_execbuffer2 *args,
  649. struct drm_file *file,
  650. struct intel_engine_cs *ring,
  651. struct eb_vmas *eb,
  652. struct drm_i915_gem_exec_object2 *exec,
  653. struct intel_context *ctx)
  654. {
  655. struct drm_i915_gem_relocation_entry *reloc;
  656. struct i915_address_space *vm;
  657. struct i915_vma *vma;
  658. bool need_relocs;
  659. int *reloc_offset;
  660. int i, total, ret;
  661. unsigned count = args->buffer_count;
  662. vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
  663. /* We may process another execbuffer during the unlock... */
  664. while (!list_empty(&eb->vmas)) {
  665. vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
  666. list_del_init(&vma->exec_list);
  667. i915_gem_execbuffer_unreserve_vma(vma);
  668. drm_gem_object_unreference(&vma->obj->base);
  669. }
  670. mutex_unlock(&dev->struct_mutex);
  671. total = 0;
  672. for (i = 0; i < count; i++)
  673. total += exec[i].relocation_count;
  674. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  675. reloc = drm_malloc_ab(total, sizeof(*reloc));
  676. if (reloc == NULL || reloc_offset == NULL) {
  677. drm_free_large(reloc);
  678. drm_free_large(reloc_offset);
  679. mutex_lock(&dev->struct_mutex);
  680. return -ENOMEM;
  681. }
  682. total = 0;
  683. for (i = 0; i < count; i++) {
  684. struct drm_i915_gem_relocation_entry __user *user_relocs;
  685. u64 invalid_offset = (u64)-1;
  686. int j;
  687. user_relocs = to_user_ptr(exec[i].relocs_ptr);
  688. if (copy_from_user(reloc+total, user_relocs,
  689. exec[i].relocation_count * sizeof(*reloc))) {
  690. ret = -EFAULT;
  691. mutex_lock(&dev->struct_mutex);
  692. goto err;
  693. }
  694. /* As we do not update the known relocation offsets after
  695. * relocating (due to the complexities in lock handling),
  696. * we need to mark them as invalid now so that we force the
  697. * relocation processing next time. Just in case the target
  698. * object is evicted and then rebound into its old
  699. * presumed_offset before the next execbuffer - if that
  700. * happened we would make the mistake of assuming that the
  701. * relocations were valid.
  702. */
  703. for (j = 0; j < exec[i].relocation_count; j++) {
  704. if (__copy_to_user(&user_relocs[j].presumed_offset,
  705. &invalid_offset,
  706. sizeof(invalid_offset))) {
  707. ret = -EFAULT;
  708. mutex_lock(&dev->struct_mutex);
  709. goto err;
  710. }
  711. }
  712. reloc_offset[i] = total;
  713. total += exec[i].relocation_count;
  714. }
  715. ret = i915_mutex_lock_interruptible(dev);
  716. if (ret) {
  717. mutex_lock(&dev->struct_mutex);
  718. goto err;
  719. }
  720. /* reacquire the objects */
  721. eb_reset(eb);
  722. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  723. if (ret)
  724. goto err;
  725. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  726. ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, ctx, &need_relocs);
  727. if (ret)
  728. goto err;
  729. list_for_each_entry(vma, &eb->vmas, exec_list) {
  730. int offset = vma->exec_entry - exec;
  731. ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
  732. reloc + reloc_offset[offset]);
  733. if (ret)
  734. goto err;
  735. }
  736. /* Leave the user relocations as are, this is the painfully slow path,
  737. * and we want to avoid the complication of dropping the lock whilst
  738. * having buffers reserved in the aperture and so causing spurious
  739. * ENOSPC for random operations.
  740. */
  741. err:
  742. drm_free_large(reloc);
  743. drm_free_large(reloc_offset);
  744. return ret;
  745. }
  746. static int
  747. i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
  748. struct list_head *vmas)
  749. {
  750. const unsigned other_rings = ~intel_ring_flag(req->ring);
  751. struct i915_vma *vma;
  752. uint32_t flush_domains = 0;
  753. bool flush_chipset = false;
  754. int ret;
  755. list_for_each_entry(vma, vmas, exec_list) {
  756. struct drm_i915_gem_object *obj = vma->obj;
  757. if (obj->active & other_rings) {
  758. ret = i915_gem_object_sync(obj, req->ring, &req);
  759. if (ret)
  760. return ret;
  761. }
  762. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  763. flush_chipset |= i915_gem_clflush_object(obj, false);
  764. flush_domains |= obj->base.write_domain;
  765. }
  766. if (flush_chipset)
  767. i915_gem_chipset_flush(req->ring->dev);
  768. if (flush_domains & I915_GEM_DOMAIN_GTT)
  769. wmb();
  770. /* Unconditionally invalidate gpu caches and ensure that we do flush
  771. * any residual writes from the previous batch.
  772. */
  773. return intel_ring_invalidate_all_caches(req->ring);
  774. }
  775. static bool
  776. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  777. {
  778. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  779. return false;
  780. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  781. }
  782. static int
  783. validate_exec_list(struct drm_device *dev,
  784. struct drm_i915_gem_exec_object2 *exec,
  785. int count)
  786. {
  787. unsigned relocs_total = 0;
  788. unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  789. unsigned invalid_flags;
  790. int i;
  791. invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  792. if (USES_FULL_PPGTT(dev))
  793. invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  794. for (i = 0; i < count; i++) {
  795. char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
  796. int length; /* limited by fault_in_pages_readable() */
  797. if (exec[i].flags & invalid_flags)
  798. return -EINVAL;
  799. if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
  800. return -EINVAL;
  801. /* First check for malicious input causing overflow in
  802. * the worst case where we need to allocate the entire
  803. * relocation tree as a single array.
  804. */
  805. if (exec[i].relocation_count > relocs_max - relocs_total)
  806. return -EINVAL;
  807. relocs_total += exec[i].relocation_count;
  808. length = exec[i].relocation_count *
  809. sizeof(struct drm_i915_gem_relocation_entry);
  810. /*
  811. * We must check that the entire relocation array is safe
  812. * to read, but since we may need to update the presumed
  813. * offsets during execution, check for full write access.
  814. */
  815. if (!access_ok(VERIFY_WRITE, ptr, length))
  816. return -EFAULT;
  817. if (likely(!i915.prefault_disable)) {
  818. if (fault_in_multipages_readable(ptr, length))
  819. return -EFAULT;
  820. }
  821. }
  822. return 0;
  823. }
  824. static struct intel_context *
  825. i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
  826. struct intel_engine_cs *ring, const u32 ctx_id)
  827. {
  828. struct intel_context *ctx = NULL;
  829. struct i915_ctx_hang_stats *hs;
  830. if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
  831. return ERR_PTR(-EINVAL);
  832. ctx = i915_gem_context_get(file->driver_priv, ctx_id);
  833. if (IS_ERR(ctx))
  834. return ctx;
  835. hs = &ctx->hang_stats;
  836. if (hs->banned) {
  837. DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
  838. return ERR_PTR(-EIO);
  839. }
  840. if (i915.enable_execlists && !ctx->engine[ring->id].state) {
  841. int ret = intel_lr_context_deferred_create(ctx, ring);
  842. if (ret) {
  843. DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
  844. return ERR_PTR(ret);
  845. }
  846. }
  847. return ctx;
  848. }
  849. void
  850. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  851. struct drm_i915_gem_request *req)
  852. {
  853. struct intel_engine_cs *ring = i915_gem_request_get_ring(req);
  854. struct i915_vma *vma;
  855. list_for_each_entry(vma, vmas, exec_list) {
  856. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  857. struct drm_i915_gem_object *obj = vma->obj;
  858. u32 old_read = obj->base.read_domains;
  859. u32 old_write = obj->base.write_domain;
  860. obj->base.write_domain = obj->base.pending_write_domain;
  861. if (obj->base.write_domain == 0)
  862. obj->base.pending_read_domains |= obj->base.read_domains;
  863. obj->base.read_domains = obj->base.pending_read_domains;
  864. i915_vma_move_to_active(vma, ring);
  865. if (obj->base.write_domain) {
  866. obj->dirty = 1;
  867. i915_gem_request_assign(&obj->last_write_req, req);
  868. intel_fb_obj_invalidate(obj, ORIGIN_CS);
  869. /* update for the implicit flush after a batch */
  870. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  871. }
  872. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  873. i915_gem_request_assign(&obj->last_fenced_req, req);
  874. if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
  875. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  876. list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
  877. &dev_priv->mm.fence_list);
  878. }
  879. }
  880. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  881. }
  882. }
  883. void
  884. i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params *params)
  885. {
  886. /* Unconditionally force add_request to emit a full flush. */
  887. params->ring->gpu_caches_dirty = true;
  888. /* Add a breadcrumb for the completion of the batch buffer */
  889. __i915_add_request(params->request, params->file, params->batch_obj, true);
  890. }
  891. static int
  892. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  893. struct intel_engine_cs *ring)
  894. {
  895. struct drm_i915_private *dev_priv = dev->dev_private;
  896. int ret, i;
  897. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
  898. DRM_DEBUG("sol reset is gen7/rcs only\n");
  899. return -EINVAL;
  900. }
  901. ret = intel_ring_begin(ring, 4 * 3);
  902. if (ret)
  903. return ret;
  904. for (i = 0; i < 4; i++) {
  905. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  906. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  907. intel_ring_emit(ring, 0);
  908. }
  909. intel_ring_advance(ring);
  910. return 0;
  911. }
  912. static int
  913. i915_emit_box(struct intel_engine_cs *ring,
  914. struct drm_clip_rect *box,
  915. int DR1, int DR4)
  916. {
  917. int ret;
  918. if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
  919. box->y2 <= 0 || box->x2 <= 0) {
  920. DRM_ERROR("Bad box %d,%d..%d,%d\n",
  921. box->x1, box->y1, box->x2, box->y2);
  922. return -EINVAL;
  923. }
  924. if (INTEL_INFO(ring->dev)->gen >= 4) {
  925. ret = intel_ring_begin(ring, 4);
  926. if (ret)
  927. return ret;
  928. intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO_I965);
  929. intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
  930. intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
  931. intel_ring_emit(ring, DR4);
  932. } else {
  933. ret = intel_ring_begin(ring, 6);
  934. if (ret)
  935. return ret;
  936. intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO);
  937. intel_ring_emit(ring, DR1);
  938. intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
  939. intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
  940. intel_ring_emit(ring, DR4);
  941. intel_ring_emit(ring, 0);
  942. }
  943. intel_ring_advance(ring);
  944. return 0;
  945. }
  946. static struct drm_i915_gem_object*
  947. i915_gem_execbuffer_parse(struct intel_engine_cs *ring,
  948. struct drm_i915_gem_exec_object2 *shadow_exec_entry,
  949. struct eb_vmas *eb,
  950. struct drm_i915_gem_object *batch_obj,
  951. u32 batch_start_offset,
  952. u32 batch_len,
  953. bool is_master)
  954. {
  955. struct drm_i915_gem_object *shadow_batch_obj;
  956. struct i915_vma *vma;
  957. int ret;
  958. shadow_batch_obj = i915_gem_batch_pool_get(&ring->batch_pool,
  959. PAGE_ALIGN(batch_len));
  960. if (IS_ERR(shadow_batch_obj))
  961. return shadow_batch_obj;
  962. ret = i915_parse_cmds(ring,
  963. batch_obj,
  964. shadow_batch_obj,
  965. batch_start_offset,
  966. batch_len,
  967. is_master);
  968. if (ret)
  969. goto err;
  970. ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
  971. if (ret)
  972. goto err;
  973. i915_gem_object_unpin_pages(shadow_batch_obj);
  974. memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
  975. vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
  976. vma->exec_entry = shadow_exec_entry;
  977. vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
  978. drm_gem_object_reference(&shadow_batch_obj->base);
  979. list_add_tail(&vma->exec_list, &eb->vmas);
  980. shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  981. return shadow_batch_obj;
  982. err:
  983. i915_gem_object_unpin_pages(shadow_batch_obj);
  984. if (ret == -EACCES) /* unhandled chained batch */
  985. return batch_obj;
  986. else
  987. return ERR_PTR(ret);
  988. }
  989. int
  990. i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
  991. struct drm_i915_gem_execbuffer2 *args,
  992. struct list_head *vmas)
  993. {
  994. struct drm_clip_rect *cliprects = NULL;
  995. struct drm_device *dev = params->dev;
  996. struct intel_engine_cs *ring = params->ring;
  997. struct drm_i915_private *dev_priv = dev->dev_private;
  998. u64 exec_start, exec_len;
  999. int instp_mode;
  1000. u32 instp_mask;
  1001. int i, ret = 0;
  1002. if (args->num_cliprects != 0) {
  1003. if (ring != &dev_priv->ring[RCS]) {
  1004. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  1005. return -EINVAL;
  1006. }
  1007. if (INTEL_INFO(dev)->gen >= 5) {
  1008. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  1009. return -EINVAL;
  1010. }
  1011. if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  1012. DRM_DEBUG("execbuf with %u cliprects\n",
  1013. args->num_cliprects);
  1014. return -EINVAL;
  1015. }
  1016. cliprects = kcalloc(args->num_cliprects,
  1017. sizeof(*cliprects),
  1018. GFP_KERNEL);
  1019. if (cliprects == NULL) {
  1020. ret = -ENOMEM;
  1021. goto error;
  1022. }
  1023. if (copy_from_user(cliprects,
  1024. to_user_ptr(args->cliprects_ptr),
  1025. sizeof(*cliprects)*args->num_cliprects)) {
  1026. ret = -EFAULT;
  1027. goto error;
  1028. }
  1029. } else {
  1030. if (args->DR4 == 0xffffffff) {
  1031. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  1032. args->DR4 = 0;
  1033. }
  1034. if (args->DR1 || args->DR4 || args->cliprects_ptr) {
  1035. DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
  1036. return -EINVAL;
  1037. }
  1038. }
  1039. ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
  1040. if (ret)
  1041. goto error;
  1042. ret = i915_switch_context(params->request);
  1043. if (ret)
  1044. goto error;
  1045. WARN(params->ctx->ppgtt && params->ctx->ppgtt->pd_dirty_rings & (1<<ring->id),
  1046. "%s didn't clear reload\n", ring->name);
  1047. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  1048. instp_mask = I915_EXEC_CONSTANTS_MASK;
  1049. switch (instp_mode) {
  1050. case I915_EXEC_CONSTANTS_REL_GENERAL:
  1051. case I915_EXEC_CONSTANTS_ABSOLUTE:
  1052. case I915_EXEC_CONSTANTS_REL_SURFACE:
  1053. if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
  1054. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  1055. ret = -EINVAL;
  1056. goto error;
  1057. }
  1058. if (instp_mode != dev_priv->relative_constants_mode) {
  1059. if (INTEL_INFO(dev)->gen < 4) {
  1060. DRM_DEBUG("no rel constants on pre-gen4\n");
  1061. ret = -EINVAL;
  1062. goto error;
  1063. }
  1064. if (INTEL_INFO(dev)->gen > 5 &&
  1065. instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  1066. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  1067. ret = -EINVAL;
  1068. goto error;
  1069. }
  1070. /* The HW changed the meaning on this bit on gen6 */
  1071. if (INTEL_INFO(dev)->gen >= 6)
  1072. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  1073. }
  1074. break;
  1075. default:
  1076. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  1077. ret = -EINVAL;
  1078. goto error;
  1079. }
  1080. if (ring == &dev_priv->ring[RCS] &&
  1081. instp_mode != dev_priv->relative_constants_mode) {
  1082. ret = intel_ring_begin(ring, 4);
  1083. if (ret)
  1084. goto error;
  1085. intel_ring_emit(ring, MI_NOOP);
  1086. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1087. intel_ring_emit(ring, INSTPM);
  1088. intel_ring_emit(ring, instp_mask << 16 | instp_mode);
  1089. intel_ring_advance(ring);
  1090. dev_priv->relative_constants_mode = instp_mode;
  1091. }
  1092. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1093. ret = i915_reset_gen7_sol_offsets(dev, ring);
  1094. if (ret)
  1095. goto error;
  1096. }
  1097. exec_len = args->batch_len;
  1098. exec_start = params->batch_obj_vm_offset +
  1099. params->args_batch_start_offset;
  1100. if (cliprects) {
  1101. for (i = 0; i < args->num_cliprects; i++) {
  1102. ret = i915_emit_box(ring, &cliprects[i],
  1103. args->DR1, args->DR4);
  1104. if (ret)
  1105. goto error;
  1106. ret = ring->dispatch_execbuffer(ring,
  1107. exec_start, exec_len,
  1108. params->dispatch_flags);
  1109. if (ret)
  1110. goto error;
  1111. }
  1112. } else {
  1113. ret = ring->dispatch_execbuffer(ring,
  1114. exec_start, exec_len,
  1115. params->dispatch_flags);
  1116. if (ret)
  1117. return ret;
  1118. }
  1119. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  1120. i915_gem_execbuffer_move_to_active(vmas, params->request);
  1121. i915_gem_execbuffer_retire_commands(params);
  1122. error:
  1123. kfree(cliprects);
  1124. return ret;
  1125. }
  1126. /**
  1127. * Find one BSD ring to dispatch the corresponding BSD command.
  1128. * The Ring ID is returned.
  1129. */
  1130. static int gen8_dispatch_bsd_ring(struct drm_device *dev,
  1131. struct drm_file *file)
  1132. {
  1133. struct drm_i915_private *dev_priv = dev->dev_private;
  1134. struct drm_i915_file_private *file_priv = file->driver_priv;
  1135. /* Check whether the file_priv is using one ring */
  1136. if (file_priv->bsd_ring)
  1137. return file_priv->bsd_ring->id;
  1138. else {
  1139. /* If no, use the ping-pong mechanism to select one ring */
  1140. int ring_id;
  1141. mutex_lock(&dev->struct_mutex);
  1142. if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
  1143. ring_id = VCS;
  1144. dev_priv->mm.bsd_ring_dispatch_index = 1;
  1145. } else {
  1146. ring_id = VCS2;
  1147. dev_priv->mm.bsd_ring_dispatch_index = 0;
  1148. }
  1149. file_priv->bsd_ring = &dev_priv->ring[ring_id];
  1150. mutex_unlock(&dev->struct_mutex);
  1151. return ring_id;
  1152. }
  1153. }
  1154. static struct drm_i915_gem_object *
  1155. eb_get_batch(struct eb_vmas *eb)
  1156. {
  1157. struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
  1158. /*
  1159. * SNA is doing fancy tricks with compressing batch buffers, which leads
  1160. * to negative relocation deltas. Usually that works out ok since the
  1161. * relocate address is still positive, except when the batch is placed
  1162. * very low in the GTT. Ensure this doesn't happen.
  1163. *
  1164. * Note that actual hangs have only been observed on gen7, but for
  1165. * paranoia do it everywhere.
  1166. */
  1167. vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  1168. return vma->obj;
  1169. }
  1170. static int
  1171. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  1172. struct drm_file *file,
  1173. struct drm_i915_gem_execbuffer2 *args,
  1174. struct drm_i915_gem_exec_object2 *exec)
  1175. {
  1176. struct drm_i915_private *dev_priv = dev->dev_private;
  1177. struct eb_vmas *eb;
  1178. struct drm_i915_gem_object *batch_obj;
  1179. struct drm_i915_gem_exec_object2 shadow_exec_entry;
  1180. struct intel_engine_cs *ring;
  1181. struct intel_context *ctx;
  1182. struct i915_address_space *vm;
  1183. struct i915_execbuffer_params params_master; /* XXX: will be removed later */
  1184. struct i915_execbuffer_params *params = &params_master;
  1185. const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  1186. u32 dispatch_flags;
  1187. int ret;
  1188. bool need_relocs;
  1189. if (!i915_gem_check_execbuffer(args))
  1190. return -EINVAL;
  1191. ret = validate_exec_list(dev, exec, args->buffer_count);
  1192. if (ret)
  1193. return ret;
  1194. dispatch_flags = 0;
  1195. if (args->flags & I915_EXEC_SECURE) {
  1196. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  1197. return -EPERM;
  1198. dispatch_flags |= I915_DISPATCH_SECURE;
  1199. }
  1200. if (args->flags & I915_EXEC_IS_PINNED)
  1201. dispatch_flags |= I915_DISPATCH_PINNED;
  1202. if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
  1203. DRM_DEBUG("execbuf with unknown ring: %d\n",
  1204. (int)(args->flags & I915_EXEC_RING_MASK));
  1205. return -EINVAL;
  1206. }
  1207. if (((args->flags & I915_EXEC_RING_MASK) != I915_EXEC_BSD) &&
  1208. ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
  1209. DRM_DEBUG("execbuf with non bsd ring but with invalid "
  1210. "bsd dispatch flags: %d\n", (int)(args->flags));
  1211. return -EINVAL;
  1212. }
  1213. if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
  1214. ring = &dev_priv->ring[RCS];
  1215. else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
  1216. if (HAS_BSD2(dev)) {
  1217. int ring_id;
  1218. switch (args->flags & I915_EXEC_BSD_MASK) {
  1219. case I915_EXEC_BSD_DEFAULT:
  1220. ring_id = gen8_dispatch_bsd_ring(dev, file);
  1221. ring = &dev_priv->ring[ring_id];
  1222. break;
  1223. case I915_EXEC_BSD_RING1:
  1224. ring = &dev_priv->ring[VCS];
  1225. break;
  1226. case I915_EXEC_BSD_RING2:
  1227. ring = &dev_priv->ring[VCS2];
  1228. break;
  1229. default:
  1230. DRM_DEBUG("execbuf with unknown bsd ring: %d\n",
  1231. (int)(args->flags & I915_EXEC_BSD_MASK));
  1232. return -EINVAL;
  1233. }
  1234. } else
  1235. ring = &dev_priv->ring[VCS];
  1236. } else
  1237. ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
  1238. if (!intel_ring_initialized(ring)) {
  1239. DRM_DEBUG("execbuf with invalid ring: %d\n",
  1240. (int)(args->flags & I915_EXEC_RING_MASK));
  1241. return -EINVAL;
  1242. }
  1243. if (args->buffer_count < 1) {
  1244. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1245. return -EINVAL;
  1246. }
  1247. intel_runtime_pm_get(dev_priv);
  1248. ret = i915_mutex_lock_interruptible(dev);
  1249. if (ret)
  1250. goto pre_mutex_err;
  1251. ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
  1252. if (IS_ERR(ctx)) {
  1253. mutex_unlock(&dev->struct_mutex);
  1254. ret = PTR_ERR(ctx);
  1255. goto pre_mutex_err;
  1256. }
  1257. i915_gem_context_reference(ctx);
  1258. if (ctx->ppgtt)
  1259. vm = &ctx->ppgtt->base;
  1260. else
  1261. vm = &dev_priv->gtt.base;
  1262. memset(&params_master, 0x00, sizeof(params_master));
  1263. eb = eb_create(args);
  1264. if (eb == NULL) {
  1265. i915_gem_context_unreference(ctx);
  1266. mutex_unlock(&dev->struct_mutex);
  1267. ret = -ENOMEM;
  1268. goto pre_mutex_err;
  1269. }
  1270. /* Look up object handles */
  1271. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  1272. if (ret)
  1273. goto err;
  1274. /* take note of the batch buffer before we might reorder the lists */
  1275. batch_obj = eb_get_batch(eb);
  1276. /* Move the objects en-masse into the GTT, evicting if necessary. */
  1277. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  1278. ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, ctx, &need_relocs);
  1279. if (ret)
  1280. goto err;
  1281. /* The objects are in their final locations, apply the relocations. */
  1282. if (need_relocs)
  1283. ret = i915_gem_execbuffer_relocate(eb);
  1284. if (ret) {
  1285. if (ret == -EFAULT) {
  1286. ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
  1287. eb, exec, ctx);
  1288. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1289. }
  1290. if (ret)
  1291. goto err;
  1292. }
  1293. /* Set the pending read domains for the batch buffer to COMMAND */
  1294. if (batch_obj->base.pending_write_domain) {
  1295. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1296. ret = -EINVAL;
  1297. goto err;
  1298. }
  1299. params->args_batch_start_offset = args->batch_start_offset;
  1300. if (i915_needs_cmd_parser(ring) && args->batch_len) {
  1301. struct drm_i915_gem_object *parsed_batch_obj;
  1302. parsed_batch_obj = i915_gem_execbuffer_parse(ring,
  1303. &shadow_exec_entry,
  1304. eb,
  1305. batch_obj,
  1306. args->batch_start_offset,
  1307. args->batch_len,
  1308. file->is_master);
  1309. if (IS_ERR(parsed_batch_obj)) {
  1310. ret = PTR_ERR(parsed_batch_obj);
  1311. goto err;
  1312. }
  1313. /*
  1314. * parsed_batch_obj == batch_obj means batch not fully parsed:
  1315. * Accept, but don't promote to secure.
  1316. */
  1317. if (parsed_batch_obj != batch_obj) {
  1318. /*
  1319. * Batch parsed and accepted:
  1320. *
  1321. * Set the DISPATCH_SECURE bit to remove the NON_SECURE
  1322. * bit from MI_BATCH_BUFFER_START commands issued in
  1323. * the dispatch_execbuffer implementations. We
  1324. * specifically don't want that set on batches the
  1325. * command parser has accepted.
  1326. */
  1327. dispatch_flags |= I915_DISPATCH_SECURE;
  1328. params->args_batch_start_offset = 0;
  1329. batch_obj = parsed_batch_obj;
  1330. }
  1331. }
  1332. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1333. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1334. * batch" bit. Hence we need to pin secure batches into the global gtt.
  1335. * hsw should have this fixed, but bdw mucks it up again. */
  1336. if (dispatch_flags & I915_DISPATCH_SECURE) {
  1337. /*
  1338. * So on first glance it looks freaky that we pin the batch here
  1339. * outside of the reservation loop. But:
  1340. * - The batch is already pinned into the relevant ppgtt, so we
  1341. * already have the backing storage fully allocated.
  1342. * - No other BO uses the global gtt (well contexts, but meh),
  1343. * so we don't really have issues with multiple objects not
  1344. * fitting due to fragmentation.
  1345. * So this is actually safe.
  1346. */
  1347. ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
  1348. if (ret)
  1349. goto err;
  1350. params->batch_obj_vm_offset = i915_gem_obj_ggtt_offset(batch_obj);
  1351. } else
  1352. params->batch_obj_vm_offset = i915_gem_obj_offset(batch_obj, vm);
  1353. /* Allocate a request for this batch buffer nice and early. */
  1354. ret = i915_gem_request_alloc(ring, ctx, &params->request);
  1355. if (ret)
  1356. goto err_batch_unpin;
  1357. /*
  1358. * Save assorted stuff away to pass through to *_submission().
  1359. * NB: This data should be 'persistent' and not local as it will
  1360. * kept around beyond the duration of the IOCTL once the GPU
  1361. * scheduler arrives.
  1362. */
  1363. params->dev = dev;
  1364. params->file = file;
  1365. params->ring = ring;
  1366. params->dispatch_flags = dispatch_flags;
  1367. params->batch_obj = batch_obj;
  1368. params->ctx = ctx;
  1369. ret = dev_priv->gt.execbuf_submit(params, args, &eb->vmas);
  1370. err_batch_unpin:
  1371. /*
  1372. * FIXME: We crucially rely upon the active tracking for the (ppgtt)
  1373. * batch vma for correctness. For less ugly and less fragility this
  1374. * needs to be adjusted to also track the ggtt batch vma properly as
  1375. * active.
  1376. */
  1377. if (dispatch_flags & I915_DISPATCH_SECURE)
  1378. i915_gem_object_ggtt_unpin(batch_obj);
  1379. err:
  1380. /* the request owns the ref now */
  1381. i915_gem_context_unreference(ctx);
  1382. eb_destroy(eb);
  1383. /*
  1384. * If the request was created but not successfully submitted then it
  1385. * must be freed again. If it was submitted then it is being tracked
  1386. * on the active request list and no clean up is required here.
  1387. */
  1388. if (ret && params->request) {
  1389. i915_gem_request_cancel(params->request);
  1390. ring->outstanding_lazy_request = NULL;
  1391. }
  1392. mutex_unlock(&dev->struct_mutex);
  1393. pre_mutex_err:
  1394. /* intel_gpu_busy should also get a ref, so it will free when the device
  1395. * is really idle. */
  1396. intel_runtime_pm_put(dev_priv);
  1397. return ret;
  1398. }
  1399. /*
  1400. * Legacy execbuffer just creates an exec2 list from the original exec object
  1401. * list array and passes it to the real function.
  1402. */
  1403. int
  1404. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1405. struct drm_file *file)
  1406. {
  1407. struct drm_i915_gem_execbuffer *args = data;
  1408. struct drm_i915_gem_execbuffer2 exec2;
  1409. struct drm_i915_gem_exec_object *exec_list = NULL;
  1410. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1411. int ret, i;
  1412. if (args->buffer_count < 1) {
  1413. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1414. return -EINVAL;
  1415. }
  1416. /* Copy in the exec list from userland */
  1417. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1418. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1419. if (exec_list == NULL || exec2_list == NULL) {
  1420. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1421. args->buffer_count);
  1422. drm_free_large(exec_list);
  1423. drm_free_large(exec2_list);
  1424. return -ENOMEM;
  1425. }
  1426. ret = copy_from_user(exec_list,
  1427. to_user_ptr(args->buffers_ptr),
  1428. sizeof(*exec_list) * args->buffer_count);
  1429. if (ret != 0) {
  1430. DRM_DEBUG("copy %d exec entries failed %d\n",
  1431. args->buffer_count, ret);
  1432. drm_free_large(exec_list);
  1433. drm_free_large(exec2_list);
  1434. return -EFAULT;
  1435. }
  1436. for (i = 0; i < args->buffer_count; i++) {
  1437. exec2_list[i].handle = exec_list[i].handle;
  1438. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1439. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1440. exec2_list[i].alignment = exec_list[i].alignment;
  1441. exec2_list[i].offset = exec_list[i].offset;
  1442. if (INTEL_INFO(dev)->gen < 4)
  1443. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1444. else
  1445. exec2_list[i].flags = 0;
  1446. }
  1447. exec2.buffers_ptr = args->buffers_ptr;
  1448. exec2.buffer_count = args->buffer_count;
  1449. exec2.batch_start_offset = args->batch_start_offset;
  1450. exec2.batch_len = args->batch_len;
  1451. exec2.DR1 = args->DR1;
  1452. exec2.DR4 = args->DR4;
  1453. exec2.num_cliprects = args->num_cliprects;
  1454. exec2.cliprects_ptr = args->cliprects_ptr;
  1455. exec2.flags = I915_EXEC_RENDER;
  1456. i915_execbuffer2_set_context_id(exec2, 0);
  1457. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1458. if (!ret) {
  1459. struct drm_i915_gem_exec_object __user *user_exec_list =
  1460. to_user_ptr(args->buffers_ptr);
  1461. /* Copy the new buffer offsets back to the user's exec list. */
  1462. for (i = 0; i < args->buffer_count; i++) {
  1463. ret = __copy_to_user(&user_exec_list[i].offset,
  1464. &exec2_list[i].offset,
  1465. sizeof(user_exec_list[i].offset));
  1466. if (ret) {
  1467. ret = -EFAULT;
  1468. DRM_DEBUG("failed to copy %d exec entries "
  1469. "back to user (%d)\n",
  1470. args->buffer_count, ret);
  1471. break;
  1472. }
  1473. }
  1474. }
  1475. drm_free_large(exec_list);
  1476. drm_free_large(exec2_list);
  1477. return ret;
  1478. }
  1479. int
  1480. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1481. struct drm_file *file)
  1482. {
  1483. struct drm_i915_gem_execbuffer2 *args = data;
  1484. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1485. int ret;
  1486. if (args->buffer_count < 1 ||
  1487. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1488. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1489. return -EINVAL;
  1490. }
  1491. if (args->rsvd2 != 0) {
  1492. DRM_DEBUG("dirty rvsd2 field\n");
  1493. return -EINVAL;
  1494. }
  1495. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1496. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  1497. if (exec2_list == NULL)
  1498. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1499. args->buffer_count);
  1500. if (exec2_list == NULL) {
  1501. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1502. args->buffer_count);
  1503. return -ENOMEM;
  1504. }
  1505. ret = copy_from_user(exec2_list,
  1506. to_user_ptr(args->buffers_ptr),
  1507. sizeof(*exec2_list) * args->buffer_count);
  1508. if (ret != 0) {
  1509. DRM_DEBUG("copy %d exec entries failed %d\n",
  1510. args->buffer_count, ret);
  1511. drm_free_large(exec2_list);
  1512. return -EFAULT;
  1513. }
  1514. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1515. if (!ret) {
  1516. /* Copy the new buffer offsets back to the user's exec list. */
  1517. struct drm_i915_gem_exec_object2 __user *user_exec_list =
  1518. to_user_ptr(args->buffers_ptr);
  1519. int i;
  1520. for (i = 0; i < args->buffer_count; i++) {
  1521. ret = __copy_to_user(&user_exec_list[i].offset,
  1522. &exec2_list[i].offset,
  1523. sizeof(user_exec_list[i].offset));
  1524. if (ret) {
  1525. ret = -EFAULT;
  1526. DRM_DEBUG("failed to copy %d exec entries "
  1527. "back to user\n",
  1528. args->buffer_count);
  1529. break;
  1530. }
  1531. }
  1532. }
  1533. drm_free_large(exec2_list);
  1534. return ret;
  1535. }