i915_gem_execbuffer.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823
  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 (!vma->is_ggtt)
  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 && !vma->is_ggtt);
  581. if (entry->alignment &&
  582. vma->node.start & (entry->alignment - 1))
  583. return true;
  584. if (entry->flags & EXEC_OBJECT_PINNED &&
  585. vma->node.start != entry->offset)
  586. return true;
  587. if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
  588. vma->node.start < BATCH_OFFSET_BIAS)
  589. return true;
  590. /* avoid costly ping-pong once a batch bo ended up non-mappable */
  591. if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
  592. return !only_mappable_for_reloc(entry->flags);
  593. if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
  594. (vma->node.start + vma->node.size - 1) >> 32)
  595. return true;
  596. return false;
  597. }
  598. static int
  599. i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
  600. struct list_head *vmas,
  601. struct intel_context *ctx,
  602. bool *need_relocs)
  603. {
  604. struct drm_i915_gem_object *obj;
  605. struct i915_vma *vma;
  606. struct i915_address_space *vm;
  607. struct list_head ordered_vmas;
  608. struct list_head pinned_vmas;
  609. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  610. int retry;
  611. i915_gem_retire_requests_ring(ring);
  612. vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
  613. INIT_LIST_HEAD(&ordered_vmas);
  614. INIT_LIST_HEAD(&pinned_vmas);
  615. while (!list_empty(vmas)) {
  616. struct drm_i915_gem_exec_object2 *entry;
  617. bool need_fence, need_mappable;
  618. vma = list_first_entry(vmas, struct i915_vma, exec_list);
  619. obj = vma->obj;
  620. entry = vma->exec_entry;
  621. if (ctx->flags & CONTEXT_NO_ZEROMAP)
  622. entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  623. if (!has_fenced_gpu_access)
  624. entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
  625. need_fence =
  626. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  627. obj->tiling_mode != I915_TILING_NONE;
  628. need_mappable = need_fence || need_reloc_mappable(vma);
  629. if (entry->flags & EXEC_OBJECT_PINNED)
  630. list_move_tail(&vma->exec_list, &pinned_vmas);
  631. else if (need_mappable) {
  632. entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
  633. list_move(&vma->exec_list, &ordered_vmas);
  634. } else
  635. list_move_tail(&vma->exec_list, &ordered_vmas);
  636. obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  637. obj->base.pending_write_domain = 0;
  638. }
  639. list_splice(&ordered_vmas, vmas);
  640. list_splice(&pinned_vmas, vmas);
  641. /* Attempt to pin all of the buffers into the GTT.
  642. * This is done in 3 phases:
  643. *
  644. * 1a. Unbind all objects that do not match the GTT constraints for
  645. * the execbuffer (fenceable, mappable, alignment etc).
  646. * 1b. Increment pin count for already bound objects.
  647. * 2. Bind new objects.
  648. * 3. Decrement pin count.
  649. *
  650. * This avoid unnecessary unbinding of later objects in order to make
  651. * room for the earlier objects *unless* we need to defragment.
  652. */
  653. retry = 0;
  654. do {
  655. int ret = 0;
  656. /* Unbind any ill-fitting objects or pin. */
  657. list_for_each_entry(vma, vmas, exec_list) {
  658. if (!drm_mm_node_allocated(&vma->node))
  659. continue;
  660. if (eb_vma_misplaced(vma))
  661. ret = i915_vma_unbind(vma);
  662. else
  663. ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  664. if (ret)
  665. goto err;
  666. }
  667. /* Bind fresh objects */
  668. list_for_each_entry(vma, vmas, exec_list) {
  669. if (drm_mm_node_allocated(&vma->node))
  670. continue;
  671. ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  672. if (ret)
  673. goto err;
  674. }
  675. err:
  676. if (ret != -ENOSPC || retry++)
  677. return ret;
  678. /* Decrement pin count for bound objects */
  679. list_for_each_entry(vma, vmas, exec_list)
  680. i915_gem_execbuffer_unreserve_vma(vma);
  681. ret = i915_gem_evict_vm(vm, true);
  682. if (ret)
  683. return ret;
  684. } while (1);
  685. }
  686. static int
  687. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  688. struct drm_i915_gem_execbuffer2 *args,
  689. struct drm_file *file,
  690. struct intel_engine_cs *ring,
  691. struct eb_vmas *eb,
  692. struct drm_i915_gem_exec_object2 *exec,
  693. struct intel_context *ctx)
  694. {
  695. struct drm_i915_gem_relocation_entry *reloc;
  696. struct i915_address_space *vm;
  697. struct i915_vma *vma;
  698. bool need_relocs;
  699. int *reloc_offset;
  700. int i, total, ret;
  701. unsigned count = args->buffer_count;
  702. vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
  703. /* We may process another execbuffer during the unlock... */
  704. while (!list_empty(&eb->vmas)) {
  705. vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
  706. list_del_init(&vma->exec_list);
  707. i915_gem_execbuffer_unreserve_vma(vma);
  708. drm_gem_object_unreference(&vma->obj->base);
  709. }
  710. mutex_unlock(&dev->struct_mutex);
  711. total = 0;
  712. for (i = 0; i < count; i++)
  713. total += exec[i].relocation_count;
  714. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  715. reloc = drm_malloc_ab(total, sizeof(*reloc));
  716. if (reloc == NULL || reloc_offset == NULL) {
  717. drm_free_large(reloc);
  718. drm_free_large(reloc_offset);
  719. mutex_lock(&dev->struct_mutex);
  720. return -ENOMEM;
  721. }
  722. total = 0;
  723. for (i = 0; i < count; i++) {
  724. struct drm_i915_gem_relocation_entry __user *user_relocs;
  725. u64 invalid_offset = (u64)-1;
  726. int j;
  727. user_relocs = to_user_ptr(exec[i].relocs_ptr);
  728. if (copy_from_user(reloc+total, user_relocs,
  729. exec[i].relocation_count * sizeof(*reloc))) {
  730. ret = -EFAULT;
  731. mutex_lock(&dev->struct_mutex);
  732. goto err;
  733. }
  734. /* As we do not update the known relocation offsets after
  735. * relocating (due to the complexities in lock handling),
  736. * we need to mark them as invalid now so that we force the
  737. * relocation processing next time. Just in case the target
  738. * object is evicted and then rebound into its old
  739. * presumed_offset before the next execbuffer - if that
  740. * happened we would make the mistake of assuming that the
  741. * relocations were valid.
  742. */
  743. for (j = 0; j < exec[i].relocation_count; j++) {
  744. if (__copy_to_user(&user_relocs[j].presumed_offset,
  745. &invalid_offset,
  746. sizeof(invalid_offset))) {
  747. ret = -EFAULT;
  748. mutex_lock(&dev->struct_mutex);
  749. goto err;
  750. }
  751. }
  752. reloc_offset[i] = total;
  753. total += exec[i].relocation_count;
  754. }
  755. ret = i915_mutex_lock_interruptible(dev);
  756. if (ret) {
  757. mutex_lock(&dev->struct_mutex);
  758. goto err;
  759. }
  760. /* reacquire the objects */
  761. eb_reset(eb);
  762. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  763. if (ret)
  764. goto err;
  765. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  766. ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, ctx, &need_relocs);
  767. if (ret)
  768. goto err;
  769. list_for_each_entry(vma, &eb->vmas, exec_list) {
  770. int offset = vma->exec_entry - exec;
  771. ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
  772. reloc + reloc_offset[offset]);
  773. if (ret)
  774. goto err;
  775. }
  776. /* Leave the user relocations as are, this is the painfully slow path,
  777. * and we want to avoid the complication of dropping the lock whilst
  778. * having buffers reserved in the aperture and so causing spurious
  779. * ENOSPC for random operations.
  780. */
  781. err:
  782. drm_free_large(reloc);
  783. drm_free_large(reloc_offset);
  784. return ret;
  785. }
  786. static int
  787. i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
  788. struct list_head *vmas)
  789. {
  790. const unsigned other_rings = ~intel_ring_flag(req->ring);
  791. struct i915_vma *vma;
  792. uint32_t flush_domains = 0;
  793. bool flush_chipset = false;
  794. int ret;
  795. list_for_each_entry(vma, vmas, exec_list) {
  796. struct drm_i915_gem_object *obj = vma->obj;
  797. if (obj->active & other_rings) {
  798. ret = i915_gem_object_sync(obj, req->ring, &req);
  799. if (ret)
  800. return ret;
  801. }
  802. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  803. flush_chipset |= i915_gem_clflush_object(obj, false);
  804. flush_domains |= obj->base.write_domain;
  805. }
  806. if (flush_chipset)
  807. i915_gem_chipset_flush(req->ring->dev);
  808. if (flush_domains & I915_GEM_DOMAIN_GTT)
  809. wmb();
  810. /* Unconditionally invalidate gpu caches and ensure that we do flush
  811. * any residual writes from the previous batch.
  812. */
  813. return intel_ring_invalidate_all_caches(req);
  814. }
  815. static bool
  816. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  817. {
  818. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  819. return false;
  820. /* Kernel clipping was a DRI1 misfeature */
  821. if (exec->num_cliprects || exec->cliprects_ptr)
  822. return false;
  823. if (exec->DR4 == 0xffffffff) {
  824. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  825. exec->DR4 = 0;
  826. }
  827. if (exec->DR1 || exec->DR4)
  828. return false;
  829. if ((exec->batch_start_offset | exec->batch_len) & 0x7)
  830. return false;
  831. return true;
  832. }
  833. static int
  834. validate_exec_list(struct drm_device *dev,
  835. struct drm_i915_gem_exec_object2 *exec,
  836. int count)
  837. {
  838. unsigned relocs_total = 0;
  839. unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  840. unsigned invalid_flags;
  841. int i;
  842. invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  843. if (USES_FULL_PPGTT(dev))
  844. invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  845. for (i = 0; i < count; i++) {
  846. char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
  847. int length; /* limited by fault_in_pages_readable() */
  848. if (exec[i].flags & invalid_flags)
  849. return -EINVAL;
  850. /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
  851. * any non-page-aligned or non-canonical addresses.
  852. */
  853. if (exec[i].flags & EXEC_OBJECT_PINNED) {
  854. if (exec[i].offset !=
  855. gen8_canonical_addr(exec[i].offset & PAGE_MASK))
  856. return -EINVAL;
  857. /* From drm_mm perspective address space is continuous,
  858. * so from this point we're always using non-canonical
  859. * form internally.
  860. */
  861. exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
  862. }
  863. if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
  864. return -EINVAL;
  865. /* First check for malicious input causing overflow in
  866. * the worst case where we need to allocate the entire
  867. * relocation tree as a single array.
  868. */
  869. if (exec[i].relocation_count > relocs_max - relocs_total)
  870. return -EINVAL;
  871. relocs_total += exec[i].relocation_count;
  872. length = exec[i].relocation_count *
  873. sizeof(struct drm_i915_gem_relocation_entry);
  874. /*
  875. * We must check that the entire relocation array is safe
  876. * to read, but since we may need to update the presumed
  877. * offsets during execution, check for full write access.
  878. */
  879. if (!access_ok(VERIFY_WRITE, ptr, length))
  880. return -EFAULT;
  881. if (likely(!i915.prefault_disable)) {
  882. if (fault_in_multipages_readable(ptr, length))
  883. return -EFAULT;
  884. }
  885. }
  886. return 0;
  887. }
  888. static struct intel_context *
  889. i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
  890. struct intel_engine_cs *ring, const u32 ctx_id)
  891. {
  892. struct intel_context *ctx = NULL;
  893. struct i915_ctx_hang_stats *hs;
  894. if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
  895. return ERR_PTR(-EINVAL);
  896. ctx = i915_gem_context_get(file->driver_priv, ctx_id);
  897. if (IS_ERR(ctx))
  898. return ctx;
  899. hs = &ctx->hang_stats;
  900. if (hs->banned) {
  901. DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
  902. return ERR_PTR(-EIO);
  903. }
  904. if (i915.enable_execlists && !ctx->engine[ring->id].state) {
  905. int ret = intel_lr_context_deferred_alloc(ctx, ring);
  906. if (ret) {
  907. DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
  908. return ERR_PTR(ret);
  909. }
  910. }
  911. return ctx;
  912. }
  913. void
  914. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  915. struct drm_i915_gem_request *req)
  916. {
  917. struct intel_engine_cs *ring = i915_gem_request_get_ring(req);
  918. struct i915_vma *vma;
  919. list_for_each_entry(vma, vmas, exec_list) {
  920. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  921. struct drm_i915_gem_object *obj = vma->obj;
  922. u32 old_read = obj->base.read_domains;
  923. u32 old_write = obj->base.write_domain;
  924. obj->dirty = 1; /* be paranoid */
  925. obj->base.write_domain = obj->base.pending_write_domain;
  926. if (obj->base.write_domain == 0)
  927. obj->base.pending_read_domains |= obj->base.read_domains;
  928. obj->base.read_domains = obj->base.pending_read_domains;
  929. i915_vma_move_to_active(vma, req);
  930. if (obj->base.write_domain) {
  931. i915_gem_request_assign(&obj->last_write_req, req);
  932. intel_fb_obj_invalidate(obj, ORIGIN_CS);
  933. /* update for the implicit flush after a batch */
  934. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  935. }
  936. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  937. i915_gem_request_assign(&obj->last_fenced_req, req);
  938. if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
  939. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  940. list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
  941. &dev_priv->mm.fence_list);
  942. }
  943. }
  944. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  945. }
  946. }
  947. void
  948. i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params *params)
  949. {
  950. /* Unconditionally force add_request to emit a full flush. */
  951. params->ring->gpu_caches_dirty = true;
  952. /* Add a breadcrumb for the completion of the batch buffer */
  953. __i915_add_request(params->request, params->batch_obj, true);
  954. }
  955. static int
  956. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  957. struct drm_i915_gem_request *req)
  958. {
  959. struct intel_engine_cs *ring = req->ring;
  960. struct drm_i915_private *dev_priv = dev->dev_private;
  961. int ret, i;
  962. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
  963. DRM_DEBUG("sol reset is gen7/rcs only\n");
  964. return -EINVAL;
  965. }
  966. ret = intel_ring_begin(req, 4 * 3);
  967. if (ret)
  968. return ret;
  969. for (i = 0; i < 4; i++) {
  970. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  971. intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
  972. intel_ring_emit(ring, 0);
  973. }
  974. intel_ring_advance(ring);
  975. return 0;
  976. }
  977. static struct drm_i915_gem_object*
  978. i915_gem_execbuffer_parse(struct intel_engine_cs *ring,
  979. struct drm_i915_gem_exec_object2 *shadow_exec_entry,
  980. struct eb_vmas *eb,
  981. struct drm_i915_gem_object *batch_obj,
  982. u32 batch_start_offset,
  983. u32 batch_len,
  984. bool is_master)
  985. {
  986. struct drm_i915_gem_object *shadow_batch_obj;
  987. struct i915_vma *vma;
  988. int ret;
  989. shadow_batch_obj = i915_gem_batch_pool_get(&ring->batch_pool,
  990. PAGE_ALIGN(batch_len));
  991. if (IS_ERR(shadow_batch_obj))
  992. return shadow_batch_obj;
  993. ret = i915_parse_cmds(ring,
  994. batch_obj,
  995. shadow_batch_obj,
  996. batch_start_offset,
  997. batch_len,
  998. is_master);
  999. if (ret)
  1000. goto err;
  1001. ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
  1002. if (ret)
  1003. goto err;
  1004. i915_gem_object_unpin_pages(shadow_batch_obj);
  1005. memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
  1006. vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
  1007. vma->exec_entry = shadow_exec_entry;
  1008. vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
  1009. drm_gem_object_reference(&shadow_batch_obj->base);
  1010. list_add_tail(&vma->exec_list, &eb->vmas);
  1011. shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  1012. return shadow_batch_obj;
  1013. err:
  1014. i915_gem_object_unpin_pages(shadow_batch_obj);
  1015. if (ret == -EACCES) /* unhandled chained batch */
  1016. return batch_obj;
  1017. else
  1018. return ERR_PTR(ret);
  1019. }
  1020. int
  1021. i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
  1022. struct drm_i915_gem_execbuffer2 *args,
  1023. struct list_head *vmas)
  1024. {
  1025. struct drm_device *dev = params->dev;
  1026. struct intel_engine_cs *ring = params->ring;
  1027. struct drm_i915_private *dev_priv = dev->dev_private;
  1028. u64 exec_start, exec_len;
  1029. int instp_mode;
  1030. u32 instp_mask;
  1031. int ret;
  1032. ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
  1033. if (ret)
  1034. return ret;
  1035. ret = i915_switch_context(params->request);
  1036. if (ret)
  1037. return ret;
  1038. WARN(params->ctx->ppgtt && params->ctx->ppgtt->pd_dirty_rings & (1<<ring->id),
  1039. "%s didn't clear reload\n", ring->name);
  1040. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  1041. instp_mask = I915_EXEC_CONSTANTS_MASK;
  1042. switch (instp_mode) {
  1043. case I915_EXEC_CONSTANTS_REL_GENERAL:
  1044. case I915_EXEC_CONSTANTS_ABSOLUTE:
  1045. case I915_EXEC_CONSTANTS_REL_SURFACE:
  1046. if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
  1047. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  1048. return -EINVAL;
  1049. }
  1050. if (instp_mode != dev_priv->relative_constants_mode) {
  1051. if (INTEL_INFO(dev)->gen < 4) {
  1052. DRM_DEBUG("no rel constants on pre-gen4\n");
  1053. return -EINVAL;
  1054. }
  1055. if (INTEL_INFO(dev)->gen > 5 &&
  1056. instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  1057. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  1058. return -EINVAL;
  1059. }
  1060. /* The HW changed the meaning on this bit on gen6 */
  1061. if (INTEL_INFO(dev)->gen >= 6)
  1062. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  1063. }
  1064. break;
  1065. default:
  1066. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  1067. return -EINVAL;
  1068. }
  1069. if (ring == &dev_priv->ring[RCS] &&
  1070. instp_mode != dev_priv->relative_constants_mode) {
  1071. ret = intel_ring_begin(params->request, 4);
  1072. if (ret)
  1073. return ret;
  1074. intel_ring_emit(ring, MI_NOOP);
  1075. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1076. intel_ring_emit_reg(ring, INSTPM);
  1077. intel_ring_emit(ring, instp_mask << 16 | instp_mode);
  1078. intel_ring_advance(ring);
  1079. dev_priv->relative_constants_mode = instp_mode;
  1080. }
  1081. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1082. ret = i915_reset_gen7_sol_offsets(dev, params->request);
  1083. if (ret)
  1084. return ret;
  1085. }
  1086. exec_len = args->batch_len;
  1087. exec_start = params->batch_obj_vm_offset +
  1088. params->args_batch_start_offset;
  1089. if (exec_len == 0)
  1090. exec_len = params->batch_obj->base.size;
  1091. ret = ring->dispatch_execbuffer(params->request,
  1092. exec_start, exec_len,
  1093. params->dispatch_flags);
  1094. if (ret)
  1095. return ret;
  1096. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  1097. i915_gem_execbuffer_move_to_active(vmas, params->request);
  1098. i915_gem_execbuffer_retire_commands(params);
  1099. return 0;
  1100. }
  1101. /**
  1102. * Find one BSD ring to dispatch the corresponding BSD command.
  1103. * The ring index is returned.
  1104. */
  1105. static unsigned int
  1106. gen8_dispatch_bsd_ring(struct drm_i915_private *dev_priv, struct drm_file *file)
  1107. {
  1108. struct drm_i915_file_private *file_priv = file->driver_priv;
  1109. /* Check whether the file_priv has already selected one ring. */
  1110. if ((int)file_priv->bsd_ring < 0) {
  1111. /* If not, use the ping-pong mechanism to select one. */
  1112. mutex_lock(&dev_priv->dev->struct_mutex);
  1113. file_priv->bsd_ring = dev_priv->mm.bsd_ring_dispatch_index;
  1114. dev_priv->mm.bsd_ring_dispatch_index ^= 1;
  1115. mutex_unlock(&dev_priv->dev->struct_mutex);
  1116. }
  1117. return file_priv->bsd_ring;
  1118. }
  1119. static struct drm_i915_gem_object *
  1120. eb_get_batch(struct eb_vmas *eb)
  1121. {
  1122. struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
  1123. /*
  1124. * SNA is doing fancy tricks with compressing batch buffers, which leads
  1125. * to negative relocation deltas. Usually that works out ok since the
  1126. * relocate address is still positive, except when the batch is placed
  1127. * very low in the GTT. Ensure this doesn't happen.
  1128. *
  1129. * Note that actual hangs have only been observed on gen7, but for
  1130. * paranoia do it everywhere.
  1131. */
  1132. if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
  1133. vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  1134. return vma->obj;
  1135. }
  1136. #define I915_USER_RINGS (4)
  1137. static const enum intel_ring_id user_ring_map[I915_USER_RINGS + 1] = {
  1138. [I915_EXEC_DEFAULT] = RCS,
  1139. [I915_EXEC_RENDER] = RCS,
  1140. [I915_EXEC_BLT] = BCS,
  1141. [I915_EXEC_BSD] = VCS,
  1142. [I915_EXEC_VEBOX] = VECS
  1143. };
  1144. static int
  1145. eb_select_ring(struct drm_i915_private *dev_priv,
  1146. struct drm_file *file,
  1147. struct drm_i915_gem_execbuffer2 *args,
  1148. struct intel_engine_cs **ring)
  1149. {
  1150. unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
  1151. if (user_ring_id > I915_USER_RINGS) {
  1152. DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
  1153. return -EINVAL;
  1154. }
  1155. if ((user_ring_id != I915_EXEC_BSD) &&
  1156. ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
  1157. DRM_DEBUG("execbuf with non bsd ring but with invalid "
  1158. "bsd dispatch flags: %d\n", (int)(args->flags));
  1159. return -EINVAL;
  1160. }
  1161. if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
  1162. unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
  1163. if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
  1164. bsd_idx = gen8_dispatch_bsd_ring(dev_priv, file);
  1165. } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
  1166. bsd_idx <= I915_EXEC_BSD_RING2) {
  1167. bsd_idx >>= I915_EXEC_BSD_SHIFT;
  1168. bsd_idx--;
  1169. } else {
  1170. DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
  1171. bsd_idx);
  1172. return -EINVAL;
  1173. }
  1174. *ring = &dev_priv->ring[_VCS(bsd_idx)];
  1175. } else {
  1176. *ring = &dev_priv->ring[user_ring_map[user_ring_id]];
  1177. }
  1178. if (!intel_ring_initialized(*ring)) {
  1179. DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
  1180. return -EINVAL;
  1181. }
  1182. return 0;
  1183. }
  1184. static int
  1185. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  1186. struct drm_file *file,
  1187. struct drm_i915_gem_execbuffer2 *args,
  1188. struct drm_i915_gem_exec_object2 *exec)
  1189. {
  1190. struct drm_i915_private *dev_priv = dev->dev_private;
  1191. struct drm_i915_gem_request *req = NULL;
  1192. struct eb_vmas *eb;
  1193. struct drm_i915_gem_object *batch_obj;
  1194. struct drm_i915_gem_exec_object2 shadow_exec_entry;
  1195. struct intel_engine_cs *ring;
  1196. struct intel_context *ctx;
  1197. struct i915_address_space *vm;
  1198. struct i915_execbuffer_params params_master; /* XXX: will be removed later */
  1199. struct i915_execbuffer_params *params = &params_master;
  1200. const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  1201. u32 dispatch_flags;
  1202. int ret;
  1203. bool need_relocs;
  1204. if (!i915_gem_check_execbuffer(args))
  1205. return -EINVAL;
  1206. ret = validate_exec_list(dev, exec, args->buffer_count);
  1207. if (ret)
  1208. return ret;
  1209. dispatch_flags = 0;
  1210. if (args->flags & I915_EXEC_SECURE) {
  1211. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  1212. return -EPERM;
  1213. dispatch_flags |= I915_DISPATCH_SECURE;
  1214. }
  1215. if (args->flags & I915_EXEC_IS_PINNED)
  1216. dispatch_flags |= I915_DISPATCH_PINNED;
  1217. ret = eb_select_ring(dev_priv, file, args, &ring);
  1218. if (ret)
  1219. return ret;
  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. req = i915_gem_request_alloc(ring, ctx);
  1344. if (IS_ERR(req)) {
  1345. ret = PTR_ERR(req);
  1346. goto err_batch_unpin;
  1347. }
  1348. ret = i915_gem_request_add_to_client(req, file);
  1349. if (ret)
  1350. goto err_batch_unpin;
  1351. /*
  1352. * Save assorted stuff away to pass through to *_submission().
  1353. * NB: This data should be 'persistent' and not local as it will
  1354. * kept around beyond the duration of the IOCTL once the GPU
  1355. * scheduler arrives.
  1356. */
  1357. params->dev = dev;
  1358. params->file = file;
  1359. params->ring = ring;
  1360. params->dispatch_flags = dispatch_flags;
  1361. params->batch_obj = batch_obj;
  1362. params->ctx = ctx;
  1363. params->request = req;
  1364. ret = dev_priv->gt.execbuf_submit(params, args, &eb->vmas);
  1365. err_batch_unpin:
  1366. /*
  1367. * FIXME: We crucially rely upon the active tracking for the (ppgtt)
  1368. * batch vma for correctness. For less ugly and less fragility this
  1369. * needs to be adjusted to also track the ggtt batch vma properly as
  1370. * active.
  1371. */
  1372. if (dispatch_flags & I915_DISPATCH_SECURE)
  1373. i915_gem_object_ggtt_unpin(batch_obj);
  1374. err:
  1375. /* the request owns the ref now */
  1376. i915_gem_context_unreference(ctx);
  1377. eb_destroy(eb);
  1378. /*
  1379. * If the request was created but not successfully submitted then it
  1380. * must be freed again. If it was submitted then it is being tracked
  1381. * on the active request list and no clean up is required here.
  1382. */
  1383. if (ret && !IS_ERR_OR_NULL(req))
  1384. i915_gem_request_cancel(req);
  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. 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. 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 = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1491. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  1492. if (exec2_list == NULL)
  1493. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1494. args->buffer_count);
  1495. if (exec2_list == NULL) {
  1496. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1497. args->buffer_count);
  1498. return -ENOMEM;
  1499. }
  1500. ret = copy_from_user(exec2_list,
  1501. to_user_ptr(args->buffers_ptr),
  1502. sizeof(*exec2_list) * args->buffer_count);
  1503. if (ret != 0) {
  1504. DRM_DEBUG("copy %d exec entries failed %d\n",
  1505. args->buffer_count, ret);
  1506. drm_free_large(exec2_list);
  1507. return -EFAULT;
  1508. }
  1509. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1510. if (!ret) {
  1511. /* Copy the new buffer offsets back to the user's exec list. */
  1512. struct drm_i915_gem_exec_object2 __user *user_exec_list =
  1513. to_user_ptr(args->buffers_ptr);
  1514. int i;
  1515. for (i = 0; i < args->buffer_count; i++) {
  1516. exec2_list[i].offset =
  1517. gen8_canonical_addr(exec2_list[i].offset);
  1518. ret = __copy_to_user(&user_exec_list[i].offset,
  1519. &exec2_list[i].offset,
  1520. sizeof(user_exec_list[i].offset));
  1521. if (ret) {
  1522. ret = -EFAULT;
  1523. DRM_DEBUG("failed to copy %d exec entries "
  1524. "back to user\n",
  1525. args->buffer_count);
  1526. break;
  1527. }
  1528. }
  1529. }
  1530. drm_free_large(exec2_list);
  1531. return ret;
  1532. }