i915_gem_execbuffer.c 49 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. #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 intel_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_INFO(engine->dev)->gen < 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 intel_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. uint32_t flush_domains = 0;
  796. bool flush_chipset = false;
  797. int ret;
  798. list_for_each_entry(vma, vmas, exec_list) {
  799. struct drm_i915_gem_object *obj = vma->obj;
  800. if (obj->active & other_rings) {
  801. ret = i915_gem_object_sync(obj, req->engine, &req);
  802. if (ret)
  803. return ret;
  804. }
  805. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  806. flush_chipset |= i915_gem_clflush_object(obj, false);
  807. flush_domains |= obj->base.write_domain;
  808. }
  809. if (flush_chipset)
  810. i915_gem_chipset_flush(req->engine->dev);
  811. if (flush_domains & I915_GEM_DOMAIN_GTT)
  812. wmb();
  813. /* Unconditionally invalidate gpu caches and ensure that we do flush
  814. * any residual writes from the previous batch.
  815. */
  816. return intel_ring_invalidate_all_caches(req);
  817. }
  818. static bool
  819. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  820. {
  821. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  822. return false;
  823. /* Kernel clipping was a DRI1 misfeature */
  824. if (exec->num_cliprects || exec->cliprects_ptr)
  825. return false;
  826. if (exec->DR4 == 0xffffffff) {
  827. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  828. exec->DR4 = 0;
  829. }
  830. if (exec->DR1 || exec->DR4)
  831. return false;
  832. if ((exec->batch_start_offset | exec->batch_len) & 0x7)
  833. return false;
  834. return true;
  835. }
  836. static int
  837. validate_exec_list(struct drm_device *dev,
  838. struct drm_i915_gem_exec_object2 *exec,
  839. int count)
  840. {
  841. unsigned relocs_total = 0;
  842. unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  843. unsigned invalid_flags;
  844. int i;
  845. invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  846. if (USES_FULL_PPGTT(dev))
  847. invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  848. for (i = 0; i < count; i++) {
  849. char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
  850. int length; /* limited by fault_in_pages_readable() */
  851. if (exec[i].flags & invalid_flags)
  852. return -EINVAL;
  853. /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
  854. * any non-page-aligned or non-canonical addresses.
  855. */
  856. if (exec[i].flags & EXEC_OBJECT_PINNED) {
  857. if (exec[i].offset !=
  858. gen8_canonical_addr(exec[i].offset & PAGE_MASK))
  859. return -EINVAL;
  860. /* From drm_mm perspective address space is continuous,
  861. * so from this point we're always using non-canonical
  862. * form internally.
  863. */
  864. exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
  865. }
  866. if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
  867. return -EINVAL;
  868. /* First check for malicious input causing overflow in
  869. * the worst case where we need to allocate the entire
  870. * relocation tree as a single array.
  871. */
  872. if (exec[i].relocation_count > relocs_max - relocs_total)
  873. return -EINVAL;
  874. relocs_total += exec[i].relocation_count;
  875. length = exec[i].relocation_count *
  876. sizeof(struct drm_i915_gem_relocation_entry);
  877. /*
  878. * We must check that the entire relocation array is safe
  879. * to read, but since we may need to update the presumed
  880. * offsets during execution, check for full write access.
  881. */
  882. if (!access_ok(VERIFY_WRITE, ptr, length))
  883. return -EFAULT;
  884. if (likely(!i915.prefault_disable)) {
  885. if (fault_in_multipages_readable(ptr, length))
  886. return -EFAULT;
  887. }
  888. }
  889. return 0;
  890. }
  891. static struct intel_context *
  892. i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
  893. struct intel_engine_cs *engine, const u32 ctx_id)
  894. {
  895. struct intel_context *ctx = NULL;
  896. struct i915_ctx_hang_stats *hs;
  897. if (engine->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
  898. return ERR_PTR(-EINVAL);
  899. ctx = i915_gem_context_get(file->driver_priv, ctx_id);
  900. if (IS_ERR(ctx))
  901. return ctx;
  902. hs = &ctx->hang_stats;
  903. if (hs->banned) {
  904. DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
  905. return ERR_PTR(-EIO);
  906. }
  907. if (i915.enable_execlists && !ctx->engine[engine->id].state) {
  908. int ret = intel_lr_context_deferred_alloc(ctx, engine);
  909. if (ret) {
  910. DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
  911. return ERR_PTR(ret);
  912. }
  913. }
  914. return ctx;
  915. }
  916. void
  917. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  918. struct drm_i915_gem_request *req)
  919. {
  920. struct intel_engine_cs *engine = i915_gem_request_get_engine(req);
  921. struct i915_vma *vma;
  922. list_for_each_entry(vma, vmas, exec_list) {
  923. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  924. struct drm_i915_gem_object *obj = vma->obj;
  925. u32 old_read = obj->base.read_domains;
  926. u32 old_write = obj->base.write_domain;
  927. obj->dirty = 1; /* be paranoid */
  928. obj->base.write_domain = obj->base.pending_write_domain;
  929. if (obj->base.write_domain == 0)
  930. obj->base.pending_read_domains |= obj->base.read_domains;
  931. obj->base.read_domains = obj->base.pending_read_domains;
  932. i915_vma_move_to_active(vma, req);
  933. if (obj->base.write_domain) {
  934. i915_gem_request_assign(&obj->last_write_req, req);
  935. intel_fb_obj_invalidate(obj, ORIGIN_CS);
  936. /* update for the implicit flush after a batch */
  937. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  938. }
  939. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  940. i915_gem_request_assign(&obj->last_fenced_req, req);
  941. if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
  942. struct drm_i915_private *dev_priv = to_i915(engine->dev);
  943. list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
  944. &dev_priv->mm.fence_list);
  945. }
  946. }
  947. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  948. }
  949. }
  950. static void
  951. i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params *params)
  952. {
  953. /* Unconditionally force add_request to emit a full flush. */
  954. params->engine->gpu_caches_dirty = true;
  955. /* Add a breadcrumb for the completion of the batch buffer */
  956. __i915_add_request(params->request, params->batch_obj, true);
  957. }
  958. static int
  959. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  960. struct drm_i915_gem_request *req)
  961. {
  962. struct intel_engine_cs *engine = req->engine;
  963. struct drm_i915_private *dev_priv = dev->dev_private;
  964. int ret, i;
  965. if (!IS_GEN7(dev) || engine != &dev_priv->engine[RCS]) {
  966. DRM_DEBUG("sol reset is gen7/rcs only\n");
  967. return -EINVAL;
  968. }
  969. ret = intel_ring_begin(req, 4 * 3);
  970. if (ret)
  971. return ret;
  972. for (i = 0; i < 4; i++) {
  973. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  974. intel_ring_emit_reg(engine, GEN7_SO_WRITE_OFFSET(i));
  975. intel_ring_emit(engine, 0);
  976. }
  977. intel_ring_advance(engine);
  978. return 0;
  979. }
  980. static struct drm_i915_gem_object*
  981. i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
  982. struct drm_i915_gem_exec_object2 *shadow_exec_entry,
  983. struct eb_vmas *eb,
  984. struct drm_i915_gem_object *batch_obj,
  985. u32 batch_start_offset,
  986. u32 batch_len,
  987. bool is_master)
  988. {
  989. struct drm_i915_gem_object *shadow_batch_obj;
  990. struct i915_vma *vma;
  991. int ret;
  992. shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
  993. PAGE_ALIGN(batch_len));
  994. if (IS_ERR(shadow_batch_obj))
  995. return shadow_batch_obj;
  996. ret = i915_parse_cmds(engine,
  997. batch_obj,
  998. shadow_batch_obj,
  999. batch_start_offset,
  1000. batch_len,
  1001. is_master);
  1002. if (ret)
  1003. goto err;
  1004. ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
  1005. if (ret)
  1006. goto err;
  1007. i915_gem_object_unpin_pages(shadow_batch_obj);
  1008. memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
  1009. vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
  1010. vma->exec_entry = shadow_exec_entry;
  1011. vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
  1012. drm_gem_object_reference(&shadow_batch_obj->base);
  1013. list_add_tail(&vma->exec_list, &eb->vmas);
  1014. shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  1015. return shadow_batch_obj;
  1016. err:
  1017. i915_gem_object_unpin_pages(shadow_batch_obj);
  1018. if (ret == -EACCES) /* unhandled chained batch */
  1019. return batch_obj;
  1020. else
  1021. return ERR_PTR(ret);
  1022. }
  1023. int
  1024. i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
  1025. struct drm_i915_gem_execbuffer2 *args,
  1026. struct list_head *vmas)
  1027. {
  1028. struct drm_device *dev = params->dev;
  1029. struct intel_engine_cs *engine = params->engine;
  1030. struct drm_i915_private *dev_priv = dev->dev_private;
  1031. u64 exec_start, exec_len;
  1032. int instp_mode;
  1033. u32 instp_mask;
  1034. int ret;
  1035. ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
  1036. if (ret)
  1037. return ret;
  1038. ret = i915_switch_context(params->request);
  1039. if (ret)
  1040. return ret;
  1041. WARN(params->ctx->ppgtt && params->ctx->ppgtt->pd_dirty_rings & (1<<engine->id),
  1042. "%s didn't clear reload\n", engine->name);
  1043. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  1044. instp_mask = I915_EXEC_CONSTANTS_MASK;
  1045. switch (instp_mode) {
  1046. case I915_EXEC_CONSTANTS_REL_GENERAL:
  1047. case I915_EXEC_CONSTANTS_ABSOLUTE:
  1048. case I915_EXEC_CONSTANTS_REL_SURFACE:
  1049. if (instp_mode != 0 && engine != &dev_priv->engine[RCS]) {
  1050. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  1051. return -EINVAL;
  1052. }
  1053. if (instp_mode != dev_priv->relative_constants_mode) {
  1054. if (INTEL_INFO(dev)->gen < 4) {
  1055. DRM_DEBUG("no rel constants on pre-gen4\n");
  1056. return -EINVAL;
  1057. }
  1058. if (INTEL_INFO(dev)->gen > 5 &&
  1059. instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  1060. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  1061. return -EINVAL;
  1062. }
  1063. /* The HW changed the meaning on this bit on gen6 */
  1064. if (INTEL_INFO(dev)->gen >= 6)
  1065. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  1066. }
  1067. break;
  1068. default:
  1069. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  1070. return -EINVAL;
  1071. }
  1072. if (engine == &dev_priv->engine[RCS] &&
  1073. instp_mode != dev_priv->relative_constants_mode) {
  1074. ret = intel_ring_begin(params->request, 4);
  1075. if (ret)
  1076. return ret;
  1077. intel_ring_emit(engine, MI_NOOP);
  1078. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  1079. intel_ring_emit_reg(engine, INSTPM);
  1080. intel_ring_emit(engine, instp_mask << 16 | instp_mode);
  1081. intel_ring_advance(engine);
  1082. dev_priv->relative_constants_mode = instp_mode;
  1083. }
  1084. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1085. ret = i915_reset_gen7_sol_offsets(dev, params->request);
  1086. if (ret)
  1087. return ret;
  1088. }
  1089. exec_len = args->batch_len;
  1090. exec_start = params->batch_obj_vm_offset +
  1091. params->args_batch_start_offset;
  1092. if (exec_len == 0)
  1093. exec_len = params->batch_obj->base.size;
  1094. ret = engine->dispatch_execbuffer(params->request,
  1095. exec_start, exec_len,
  1096. params->dispatch_flags);
  1097. if (ret)
  1098. return ret;
  1099. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  1100. i915_gem_execbuffer_move_to_active(vmas, params->request);
  1101. return 0;
  1102. }
  1103. /**
  1104. * Find one BSD ring to dispatch the corresponding BSD command.
  1105. * The ring index is returned.
  1106. */
  1107. static unsigned int
  1108. gen8_dispatch_bsd_ring(struct drm_i915_private *dev_priv, struct drm_file *file)
  1109. {
  1110. struct drm_i915_file_private *file_priv = file->driver_priv;
  1111. /* Check whether the file_priv has already selected one ring. */
  1112. if ((int)file_priv->bsd_ring < 0) {
  1113. /* If not, use the ping-pong mechanism to select one. */
  1114. mutex_lock(&dev_priv->dev->struct_mutex);
  1115. file_priv->bsd_ring = dev_priv->mm.bsd_ring_dispatch_index;
  1116. dev_priv->mm.bsd_ring_dispatch_index ^= 1;
  1117. mutex_unlock(&dev_priv->dev->struct_mutex);
  1118. }
  1119. return file_priv->bsd_ring;
  1120. }
  1121. static struct drm_i915_gem_object *
  1122. eb_get_batch(struct eb_vmas *eb)
  1123. {
  1124. struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
  1125. /*
  1126. * SNA is doing fancy tricks with compressing batch buffers, which leads
  1127. * to negative relocation deltas. Usually that works out ok since the
  1128. * relocate address is still positive, except when the batch is placed
  1129. * very low in the GTT. Ensure this doesn't happen.
  1130. *
  1131. * Note that actual hangs have only been observed on gen7, but for
  1132. * paranoia do it everywhere.
  1133. */
  1134. if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
  1135. vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  1136. return vma->obj;
  1137. }
  1138. #define I915_USER_RINGS (4)
  1139. static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
  1140. [I915_EXEC_DEFAULT] = RCS,
  1141. [I915_EXEC_RENDER] = RCS,
  1142. [I915_EXEC_BLT] = BCS,
  1143. [I915_EXEC_BSD] = VCS,
  1144. [I915_EXEC_VEBOX] = VECS
  1145. };
  1146. static int
  1147. eb_select_ring(struct drm_i915_private *dev_priv,
  1148. struct drm_file *file,
  1149. struct drm_i915_gem_execbuffer2 *args,
  1150. struct intel_engine_cs **ring)
  1151. {
  1152. unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
  1153. if (user_ring_id > I915_USER_RINGS) {
  1154. DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
  1155. return -EINVAL;
  1156. }
  1157. if ((user_ring_id != I915_EXEC_BSD) &&
  1158. ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
  1159. DRM_DEBUG("execbuf with non bsd ring but with invalid "
  1160. "bsd dispatch flags: %d\n", (int)(args->flags));
  1161. return -EINVAL;
  1162. }
  1163. if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
  1164. unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
  1165. if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
  1166. bsd_idx = gen8_dispatch_bsd_ring(dev_priv, file);
  1167. } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
  1168. bsd_idx <= I915_EXEC_BSD_RING2) {
  1169. bsd_idx >>= I915_EXEC_BSD_SHIFT;
  1170. bsd_idx--;
  1171. } else {
  1172. DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
  1173. bsd_idx);
  1174. return -EINVAL;
  1175. }
  1176. *ring = &dev_priv->engine[_VCS(bsd_idx)];
  1177. } else {
  1178. *ring = &dev_priv->engine[user_ring_map[user_ring_id]];
  1179. }
  1180. if (!intel_engine_initialized(*ring)) {
  1181. DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
  1182. return -EINVAL;
  1183. }
  1184. return 0;
  1185. }
  1186. static int
  1187. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  1188. struct drm_file *file,
  1189. struct drm_i915_gem_execbuffer2 *args,
  1190. struct drm_i915_gem_exec_object2 *exec)
  1191. {
  1192. struct drm_i915_private *dev_priv = to_i915(dev);
  1193. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1194. struct drm_i915_gem_request *req = NULL;
  1195. struct eb_vmas *eb;
  1196. struct drm_i915_gem_object *batch_obj;
  1197. struct drm_i915_gem_exec_object2 shadow_exec_entry;
  1198. struct intel_engine_cs *engine;
  1199. struct intel_context *ctx;
  1200. struct i915_address_space *vm;
  1201. struct i915_execbuffer_params params_master; /* XXX: will be removed later */
  1202. struct i915_execbuffer_params *params = &params_master;
  1203. const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  1204. u32 dispatch_flags;
  1205. int ret;
  1206. bool need_relocs;
  1207. if (!i915_gem_check_execbuffer(args))
  1208. return -EINVAL;
  1209. ret = validate_exec_list(dev, exec, args->buffer_count);
  1210. if (ret)
  1211. return ret;
  1212. dispatch_flags = 0;
  1213. if (args->flags & I915_EXEC_SECURE) {
  1214. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  1215. return -EPERM;
  1216. dispatch_flags |= I915_DISPATCH_SECURE;
  1217. }
  1218. if (args->flags & I915_EXEC_IS_PINNED)
  1219. dispatch_flags |= I915_DISPATCH_PINNED;
  1220. ret = eb_select_ring(dev_priv, file, args, &engine);
  1221. if (ret)
  1222. return ret;
  1223. if (args->buffer_count < 1) {
  1224. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1225. return -EINVAL;
  1226. }
  1227. if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
  1228. if (!HAS_RESOURCE_STREAMER(dev)) {
  1229. DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
  1230. return -EINVAL;
  1231. }
  1232. if (engine->id != RCS) {
  1233. DRM_DEBUG("RS is not available on %s\n",
  1234. engine->name);
  1235. return -EINVAL;
  1236. }
  1237. dispatch_flags |= I915_DISPATCH_RS;
  1238. }
  1239. intel_runtime_pm_get(dev_priv);
  1240. ret = i915_mutex_lock_interruptible(dev);
  1241. if (ret)
  1242. goto pre_mutex_err;
  1243. ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
  1244. if (IS_ERR(ctx)) {
  1245. mutex_unlock(&dev->struct_mutex);
  1246. ret = PTR_ERR(ctx);
  1247. goto pre_mutex_err;
  1248. }
  1249. i915_gem_context_reference(ctx);
  1250. if (ctx->ppgtt)
  1251. vm = &ctx->ppgtt->base;
  1252. else
  1253. vm = &ggtt->base;
  1254. memset(&params_master, 0x00, sizeof(params_master));
  1255. eb = eb_create(args);
  1256. if (eb == NULL) {
  1257. i915_gem_context_unreference(ctx);
  1258. mutex_unlock(&dev->struct_mutex);
  1259. ret = -ENOMEM;
  1260. goto pre_mutex_err;
  1261. }
  1262. /* Look up object handles */
  1263. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  1264. if (ret)
  1265. goto err;
  1266. /* take note of the batch buffer before we might reorder the lists */
  1267. batch_obj = eb_get_batch(eb);
  1268. /* Move the objects en-masse into the GTT, evicting if necessary. */
  1269. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  1270. ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
  1271. &need_relocs);
  1272. if (ret)
  1273. goto err;
  1274. /* The objects are in their final locations, apply the relocations. */
  1275. if (need_relocs)
  1276. ret = i915_gem_execbuffer_relocate(eb);
  1277. if (ret) {
  1278. if (ret == -EFAULT) {
  1279. ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
  1280. engine,
  1281. eb, exec, ctx);
  1282. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1283. }
  1284. if (ret)
  1285. goto err;
  1286. }
  1287. /* Set the pending read domains for the batch buffer to COMMAND */
  1288. if (batch_obj->base.pending_write_domain) {
  1289. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1290. ret = -EINVAL;
  1291. goto err;
  1292. }
  1293. params->args_batch_start_offset = args->batch_start_offset;
  1294. if (i915_needs_cmd_parser(engine) && args->batch_len) {
  1295. struct drm_i915_gem_object *parsed_batch_obj;
  1296. parsed_batch_obj = i915_gem_execbuffer_parse(engine,
  1297. &shadow_exec_entry,
  1298. eb,
  1299. batch_obj,
  1300. args->batch_start_offset,
  1301. args->batch_len,
  1302. file->is_master);
  1303. if (IS_ERR(parsed_batch_obj)) {
  1304. ret = PTR_ERR(parsed_batch_obj);
  1305. goto err;
  1306. }
  1307. /*
  1308. * parsed_batch_obj == batch_obj means batch not fully parsed:
  1309. * Accept, but don't promote to secure.
  1310. */
  1311. if (parsed_batch_obj != batch_obj) {
  1312. /*
  1313. * Batch parsed and accepted:
  1314. *
  1315. * Set the DISPATCH_SECURE bit to remove the NON_SECURE
  1316. * bit from MI_BATCH_BUFFER_START commands issued in
  1317. * the dispatch_execbuffer implementations. We
  1318. * specifically don't want that set on batches the
  1319. * command parser has accepted.
  1320. */
  1321. dispatch_flags |= I915_DISPATCH_SECURE;
  1322. params->args_batch_start_offset = 0;
  1323. batch_obj = parsed_batch_obj;
  1324. }
  1325. }
  1326. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1327. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1328. * batch" bit. Hence we need to pin secure batches into the global gtt.
  1329. * hsw should have this fixed, but bdw mucks it up again. */
  1330. if (dispatch_flags & I915_DISPATCH_SECURE) {
  1331. /*
  1332. * So on first glance it looks freaky that we pin the batch here
  1333. * outside of the reservation loop. But:
  1334. * - The batch is already pinned into the relevant ppgtt, so we
  1335. * already have the backing storage fully allocated.
  1336. * - No other BO uses the global gtt (well contexts, but meh),
  1337. * so we don't really have issues with multiple objects not
  1338. * fitting due to fragmentation.
  1339. * So this is actually safe.
  1340. */
  1341. ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
  1342. if (ret)
  1343. goto err;
  1344. params->batch_obj_vm_offset = i915_gem_obj_ggtt_offset(batch_obj);
  1345. } else
  1346. params->batch_obj_vm_offset = i915_gem_obj_offset(batch_obj, vm);
  1347. /* Allocate a request for this batch buffer nice and early. */
  1348. req = i915_gem_request_alloc(engine, ctx);
  1349. if (IS_ERR(req)) {
  1350. ret = PTR_ERR(req);
  1351. goto err_batch_unpin;
  1352. }
  1353. ret = i915_gem_request_add_to_client(req, file);
  1354. if (ret)
  1355. goto err_request;
  1356. /*
  1357. * Save assorted stuff away to pass through to *_submission().
  1358. * NB: This data should be 'persistent' and not local as it will
  1359. * kept around beyond the duration of the IOCTL once the GPU
  1360. * scheduler arrives.
  1361. */
  1362. params->dev = dev;
  1363. params->file = file;
  1364. params->engine = engine;
  1365. params->dispatch_flags = dispatch_flags;
  1366. params->batch_obj = batch_obj;
  1367. params->ctx = ctx;
  1368. params->request = req;
  1369. ret = dev_priv->gt.execbuf_submit(params, args, &eb->vmas);
  1370. err_request:
  1371. i915_gem_execbuffer_retire_commands(params);
  1372. err_batch_unpin:
  1373. /*
  1374. * FIXME: We crucially rely upon the active tracking for the (ppgtt)
  1375. * batch vma for correctness. For less ugly and less fragility this
  1376. * needs to be adjusted to also track the ggtt batch vma properly as
  1377. * active.
  1378. */
  1379. if (dispatch_flags & I915_DISPATCH_SECURE)
  1380. i915_gem_object_ggtt_unpin(batch_obj);
  1381. err:
  1382. /* the request owns the ref now */
  1383. i915_gem_context_unreference(ctx);
  1384. eb_destroy(eb);
  1385. mutex_unlock(&dev->struct_mutex);
  1386. pre_mutex_err:
  1387. /* intel_gpu_busy should also get a ref, so it will free when the device
  1388. * is really idle. */
  1389. intel_runtime_pm_put(dev_priv);
  1390. return ret;
  1391. }
  1392. /*
  1393. * Legacy execbuffer just creates an exec2 list from the original exec object
  1394. * list array and passes it to the real function.
  1395. */
  1396. int
  1397. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1398. struct drm_file *file)
  1399. {
  1400. struct drm_i915_gem_execbuffer *args = data;
  1401. struct drm_i915_gem_execbuffer2 exec2;
  1402. struct drm_i915_gem_exec_object *exec_list = NULL;
  1403. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1404. int ret, i;
  1405. if (args->buffer_count < 1) {
  1406. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1407. return -EINVAL;
  1408. }
  1409. /* Copy in the exec list from userland */
  1410. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1411. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1412. if (exec_list == NULL || exec2_list == NULL) {
  1413. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1414. args->buffer_count);
  1415. drm_free_large(exec_list);
  1416. drm_free_large(exec2_list);
  1417. return -ENOMEM;
  1418. }
  1419. ret = copy_from_user(exec_list,
  1420. u64_to_user_ptr(args->buffers_ptr),
  1421. sizeof(*exec_list) * args->buffer_count);
  1422. if (ret != 0) {
  1423. DRM_DEBUG("copy %d exec entries failed %d\n",
  1424. args->buffer_count, ret);
  1425. drm_free_large(exec_list);
  1426. drm_free_large(exec2_list);
  1427. return -EFAULT;
  1428. }
  1429. for (i = 0; i < args->buffer_count; i++) {
  1430. exec2_list[i].handle = exec_list[i].handle;
  1431. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1432. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1433. exec2_list[i].alignment = exec_list[i].alignment;
  1434. exec2_list[i].offset = exec_list[i].offset;
  1435. if (INTEL_INFO(dev)->gen < 4)
  1436. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1437. else
  1438. exec2_list[i].flags = 0;
  1439. }
  1440. exec2.buffers_ptr = args->buffers_ptr;
  1441. exec2.buffer_count = args->buffer_count;
  1442. exec2.batch_start_offset = args->batch_start_offset;
  1443. exec2.batch_len = args->batch_len;
  1444. exec2.DR1 = args->DR1;
  1445. exec2.DR4 = args->DR4;
  1446. exec2.num_cliprects = args->num_cliprects;
  1447. exec2.cliprects_ptr = args->cliprects_ptr;
  1448. exec2.flags = I915_EXEC_RENDER;
  1449. i915_execbuffer2_set_context_id(exec2, 0);
  1450. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1451. if (!ret) {
  1452. struct drm_i915_gem_exec_object __user *user_exec_list =
  1453. u64_to_user_ptr(args->buffers_ptr);
  1454. /* Copy the new buffer offsets back to the user's exec list. */
  1455. for (i = 0; i < args->buffer_count; i++) {
  1456. exec2_list[i].offset =
  1457. gen8_canonical_addr(exec2_list[i].offset);
  1458. ret = __copy_to_user(&user_exec_list[i].offset,
  1459. &exec2_list[i].offset,
  1460. sizeof(user_exec_list[i].offset));
  1461. if (ret) {
  1462. ret = -EFAULT;
  1463. DRM_DEBUG("failed to copy %d exec entries "
  1464. "back to user (%d)\n",
  1465. args->buffer_count, ret);
  1466. break;
  1467. }
  1468. }
  1469. }
  1470. drm_free_large(exec_list);
  1471. drm_free_large(exec2_list);
  1472. return ret;
  1473. }
  1474. int
  1475. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1476. struct drm_file *file)
  1477. {
  1478. struct drm_i915_gem_execbuffer2 *args = data;
  1479. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1480. int ret;
  1481. if (args->buffer_count < 1 ||
  1482. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1483. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1484. return -EINVAL;
  1485. }
  1486. if (args->rsvd2 != 0) {
  1487. DRM_DEBUG("dirty rvsd2 field\n");
  1488. return -EINVAL;
  1489. }
  1490. exec2_list = drm_malloc_gfp(args->buffer_count,
  1491. sizeof(*exec2_list),
  1492. GFP_TEMPORARY);
  1493. if (exec2_list == NULL) {
  1494. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1495. args->buffer_count);
  1496. return -ENOMEM;
  1497. }
  1498. ret = copy_from_user(exec2_list,
  1499. u64_to_user_ptr(args->buffers_ptr),
  1500. sizeof(*exec2_list) * args->buffer_count);
  1501. if (ret != 0) {
  1502. DRM_DEBUG("copy %d exec entries failed %d\n",
  1503. args->buffer_count, ret);
  1504. drm_free_large(exec2_list);
  1505. return -EFAULT;
  1506. }
  1507. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1508. if (!ret) {
  1509. /* Copy the new buffer offsets back to the user's exec list. */
  1510. struct drm_i915_gem_exec_object2 __user *user_exec_list =
  1511. u64_to_user_ptr(args->buffers_ptr);
  1512. int i;
  1513. for (i = 0; i < args->buffer_count; i++) {
  1514. exec2_list[i].offset =
  1515. gen8_canonical_addr(exec2_list[i].offset);
  1516. ret = __copy_to_user(&user_exec_list[i].offset,
  1517. &exec2_list[i].offset,
  1518. sizeof(user_exec_list[i].offset));
  1519. if (ret) {
  1520. ret = -EFAULT;
  1521. DRM_DEBUG("failed to copy %d exec entries "
  1522. "back to user\n",
  1523. args->buffer_count);
  1524. break;
  1525. }
  1526. }
  1527. }
  1528. drm_free_large(exec2_list);
  1529. return ret;
  1530. }