i915_gem_execbuffer.c 49 KB

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