i915_gem_execbuffer.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000
  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 <linux/dma_remapping.h>
  29. #include <linux/reservation.h>
  30. #include <linux/sync_file.h>
  31. #include <linux/uaccess.h>
  32. #include <drm/drmP.h>
  33. #include <drm/i915_drm.h>
  34. #include "i915_drv.h"
  35. #include "i915_gem_clflush.h"
  36. #include "i915_trace.h"
  37. #include "intel_drv.h"
  38. #include "intel_frontbuffer.h"
  39. #define DBG_USE_CPU_RELOC 0 /* -1 force GTT relocs; 1 force CPU relocs */
  40. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  41. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  42. #define __EXEC_OBJECT_NEEDS_MAP (1<<29)
  43. #define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
  44. #define __EXEC_OBJECT_INTERNAL_FLAGS (0xf<<28) /* all of the above */
  45. #define BATCH_OFFSET_BIAS (256*1024)
  46. struct i915_execbuffer_params {
  47. struct drm_device *dev;
  48. struct drm_file *file;
  49. struct i915_vma *batch;
  50. u32 dispatch_flags;
  51. u32 args_batch_start_offset;
  52. struct intel_engine_cs *engine;
  53. struct i915_gem_context *ctx;
  54. struct drm_i915_gem_request *request;
  55. };
  56. struct eb_vmas {
  57. struct drm_i915_private *i915;
  58. struct list_head vmas;
  59. int and;
  60. union {
  61. struct i915_vma *lut[0];
  62. struct hlist_head buckets[0];
  63. };
  64. };
  65. static struct eb_vmas *
  66. eb_create(struct drm_i915_private *i915,
  67. struct drm_i915_gem_execbuffer2 *args)
  68. {
  69. struct eb_vmas *eb = NULL;
  70. if (args->flags & I915_EXEC_HANDLE_LUT) {
  71. unsigned size = args->buffer_count;
  72. size *= sizeof(struct i915_vma *);
  73. size += sizeof(struct eb_vmas);
  74. eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  75. }
  76. if (eb == NULL) {
  77. unsigned size = args->buffer_count;
  78. unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  79. BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
  80. while (count > 2*size)
  81. count >>= 1;
  82. eb = kzalloc(count*sizeof(struct hlist_head) +
  83. sizeof(struct eb_vmas),
  84. GFP_TEMPORARY);
  85. if (eb == NULL)
  86. return eb;
  87. eb->and = count - 1;
  88. } else
  89. eb->and = -args->buffer_count;
  90. eb->i915 = i915;
  91. INIT_LIST_HEAD(&eb->vmas);
  92. return eb;
  93. }
  94. static void
  95. eb_reset(struct eb_vmas *eb)
  96. {
  97. if (eb->and >= 0)
  98. memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  99. }
  100. static struct i915_vma *
  101. eb_get_batch(struct eb_vmas *eb)
  102. {
  103. struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
  104. /*
  105. * SNA is doing fancy tricks with compressing batch buffers, which leads
  106. * to negative relocation deltas. Usually that works out ok since the
  107. * relocate address is still positive, except when the batch is placed
  108. * very low in the GTT. Ensure this doesn't happen.
  109. *
  110. * Note that actual hangs have only been observed on gen7, but for
  111. * paranoia do it everywhere.
  112. */
  113. if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
  114. vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  115. return vma;
  116. }
  117. static int
  118. eb_lookup_vmas(struct eb_vmas *eb,
  119. struct drm_i915_gem_exec_object2 *exec,
  120. const struct drm_i915_gem_execbuffer2 *args,
  121. struct i915_address_space *vm,
  122. struct drm_file *file)
  123. {
  124. struct drm_i915_gem_object *obj;
  125. struct list_head objects;
  126. int i, ret;
  127. INIT_LIST_HEAD(&objects);
  128. spin_lock(&file->table_lock);
  129. /* Grab a reference to the object and release the lock so we can lookup
  130. * or create the VMA without using GFP_ATOMIC */
  131. for (i = 0; i < args->buffer_count; i++) {
  132. obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
  133. if (obj == NULL) {
  134. spin_unlock(&file->table_lock);
  135. DRM_DEBUG("Invalid object handle %d at index %d\n",
  136. exec[i].handle, i);
  137. ret = -ENOENT;
  138. goto err;
  139. }
  140. if (!list_empty(&obj->obj_exec_link)) {
  141. spin_unlock(&file->table_lock);
  142. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  143. obj, exec[i].handle, i);
  144. ret = -EINVAL;
  145. goto err;
  146. }
  147. i915_gem_object_get(obj);
  148. list_add_tail(&obj->obj_exec_link, &objects);
  149. }
  150. spin_unlock(&file->table_lock);
  151. i = 0;
  152. while (!list_empty(&objects)) {
  153. struct i915_vma *vma;
  154. obj = list_first_entry(&objects,
  155. struct drm_i915_gem_object,
  156. obj_exec_link);
  157. /*
  158. * NOTE: We can leak any vmas created here when something fails
  159. * later on. But that's no issue since vma_unbind can deal with
  160. * vmas which are not actually bound. And since only
  161. * lookup_or_create exists as an interface to get at the vma
  162. * from the (obj, vm) we don't run the risk of creating
  163. * duplicated vmas for the same vm.
  164. */
  165. vma = i915_vma_instance(obj, vm, NULL);
  166. if (unlikely(IS_ERR(vma))) {
  167. DRM_DEBUG("Failed to lookup VMA\n");
  168. ret = PTR_ERR(vma);
  169. goto err;
  170. }
  171. /* Transfer ownership from the objects list to the vmas list. */
  172. list_add_tail(&vma->exec_list, &eb->vmas);
  173. list_del_init(&obj->obj_exec_link);
  174. vma->exec_entry = &exec[i];
  175. if (eb->and < 0) {
  176. eb->lut[i] = vma;
  177. } else {
  178. uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
  179. vma->exec_handle = handle;
  180. hlist_add_head(&vma->exec_node,
  181. &eb->buckets[handle & eb->and]);
  182. }
  183. ++i;
  184. }
  185. return 0;
  186. err:
  187. while (!list_empty(&objects)) {
  188. obj = list_first_entry(&objects,
  189. struct drm_i915_gem_object,
  190. obj_exec_link);
  191. list_del_init(&obj->obj_exec_link);
  192. i915_gem_object_put(obj);
  193. }
  194. /*
  195. * Objects already transfered to the vmas list will be unreferenced by
  196. * eb_destroy.
  197. */
  198. return ret;
  199. }
  200. static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
  201. {
  202. if (eb->and < 0) {
  203. if (handle >= -eb->and)
  204. return NULL;
  205. return eb->lut[handle];
  206. } else {
  207. struct hlist_head *head;
  208. struct i915_vma *vma;
  209. head = &eb->buckets[handle & eb->and];
  210. hlist_for_each_entry(vma, head, exec_node) {
  211. if (vma->exec_handle == handle)
  212. return vma;
  213. }
  214. return NULL;
  215. }
  216. }
  217. static void
  218. i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
  219. {
  220. struct drm_i915_gem_exec_object2 *entry;
  221. if (!drm_mm_node_allocated(&vma->node))
  222. return;
  223. entry = vma->exec_entry;
  224. if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
  225. i915_vma_unpin_fence(vma);
  226. if (entry->flags & __EXEC_OBJECT_HAS_PIN)
  227. __i915_vma_unpin(vma);
  228. entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  229. }
  230. static void eb_destroy(struct eb_vmas *eb)
  231. {
  232. while (!list_empty(&eb->vmas)) {
  233. struct i915_vma *vma;
  234. vma = list_first_entry(&eb->vmas,
  235. struct i915_vma,
  236. exec_list);
  237. list_del_init(&vma->exec_list);
  238. i915_gem_execbuffer_unreserve_vma(vma);
  239. vma->exec_entry = NULL;
  240. i915_vma_put(vma);
  241. }
  242. kfree(eb);
  243. }
  244. static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
  245. {
  246. if (!i915_gem_object_has_struct_page(obj))
  247. return false;
  248. if (DBG_USE_CPU_RELOC)
  249. return DBG_USE_CPU_RELOC > 0;
  250. return (HAS_LLC(to_i915(obj->base.dev)) ||
  251. obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
  252. obj->cache_level != I915_CACHE_NONE);
  253. }
  254. /* Used to convert any address to canonical form.
  255. * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
  256. * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
  257. * addresses to be in a canonical form:
  258. * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
  259. * canonical form [63:48] == [47]."
  260. */
  261. #define GEN8_HIGH_ADDRESS_BIT 47
  262. static inline uint64_t gen8_canonical_addr(uint64_t address)
  263. {
  264. return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
  265. }
  266. static inline uint64_t gen8_noncanonical_addr(uint64_t address)
  267. {
  268. return address & ((1ULL << (GEN8_HIGH_ADDRESS_BIT + 1)) - 1);
  269. }
  270. static inline uint64_t
  271. relocation_target(const struct drm_i915_gem_relocation_entry *reloc,
  272. uint64_t target_offset)
  273. {
  274. return gen8_canonical_addr((int)reloc->delta + target_offset);
  275. }
  276. struct reloc_cache {
  277. struct drm_i915_private *i915;
  278. struct drm_mm_node node;
  279. unsigned long vaddr;
  280. unsigned int page;
  281. bool use_64bit_reloc;
  282. };
  283. static void reloc_cache_init(struct reloc_cache *cache,
  284. struct drm_i915_private *i915)
  285. {
  286. cache->page = -1;
  287. cache->vaddr = 0;
  288. cache->i915 = i915;
  289. /* Must be a variable in the struct to allow GCC to unroll. */
  290. cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
  291. cache->node.allocated = false;
  292. }
  293. static inline void *unmask_page(unsigned long p)
  294. {
  295. return (void *)(uintptr_t)(p & PAGE_MASK);
  296. }
  297. static inline unsigned int unmask_flags(unsigned long p)
  298. {
  299. return p & ~PAGE_MASK;
  300. }
  301. #define KMAP 0x4 /* after CLFLUSH_FLAGS */
  302. static void reloc_cache_fini(struct reloc_cache *cache)
  303. {
  304. void *vaddr;
  305. if (!cache->vaddr)
  306. return;
  307. vaddr = unmask_page(cache->vaddr);
  308. if (cache->vaddr & KMAP) {
  309. if (cache->vaddr & CLFLUSH_AFTER)
  310. mb();
  311. kunmap_atomic(vaddr);
  312. i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
  313. } else {
  314. wmb();
  315. io_mapping_unmap_atomic((void __iomem *)vaddr);
  316. if (cache->node.allocated) {
  317. struct i915_ggtt *ggtt = &cache->i915->ggtt;
  318. ggtt->base.clear_range(&ggtt->base,
  319. cache->node.start,
  320. cache->node.size);
  321. drm_mm_remove_node(&cache->node);
  322. } else {
  323. i915_vma_unpin((struct i915_vma *)cache->node.mm);
  324. }
  325. }
  326. }
  327. static void *reloc_kmap(struct drm_i915_gem_object *obj,
  328. struct reloc_cache *cache,
  329. int page)
  330. {
  331. void *vaddr;
  332. if (cache->vaddr) {
  333. kunmap_atomic(unmask_page(cache->vaddr));
  334. } else {
  335. unsigned int flushes;
  336. int ret;
  337. ret = i915_gem_obj_prepare_shmem_write(obj, &flushes);
  338. if (ret)
  339. return ERR_PTR(ret);
  340. BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS);
  341. BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK);
  342. cache->vaddr = flushes | KMAP;
  343. cache->node.mm = (void *)obj;
  344. if (flushes)
  345. mb();
  346. }
  347. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj, page));
  348. cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr;
  349. cache->page = page;
  350. return vaddr;
  351. }
  352. static void *reloc_iomap(struct drm_i915_gem_object *obj,
  353. struct reloc_cache *cache,
  354. int page)
  355. {
  356. struct i915_ggtt *ggtt = &cache->i915->ggtt;
  357. unsigned long offset;
  358. void *vaddr;
  359. if (cache->vaddr) {
  360. io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
  361. } else {
  362. struct i915_vma *vma;
  363. int ret;
  364. if (use_cpu_reloc(obj))
  365. return NULL;
  366. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  367. if (ret)
  368. return ERR_PTR(ret);
  369. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  370. PIN_MAPPABLE | PIN_NONBLOCK);
  371. if (IS_ERR(vma)) {
  372. memset(&cache->node, 0, sizeof(cache->node));
  373. ret = drm_mm_insert_node_in_range
  374. (&ggtt->base.mm, &cache->node,
  375. PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
  376. 0, ggtt->mappable_end,
  377. DRM_MM_INSERT_LOW);
  378. if (ret) /* no inactive aperture space, use cpu reloc */
  379. return NULL;
  380. } else {
  381. ret = i915_vma_put_fence(vma);
  382. if (ret) {
  383. i915_vma_unpin(vma);
  384. return ERR_PTR(ret);
  385. }
  386. cache->node.start = vma->node.start;
  387. cache->node.mm = (void *)vma;
  388. }
  389. }
  390. offset = cache->node.start;
  391. if (cache->node.allocated) {
  392. wmb();
  393. ggtt->base.insert_page(&ggtt->base,
  394. i915_gem_object_get_dma_address(obj, page),
  395. offset, I915_CACHE_NONE, 0);
  396. } else {
  397. offset += page << PAGE_SHIFT;
  398. }
  399. vaddr = (void __force *) io_mapping_map_atomic_wc(&cache->i915->ggtt.mappable, offset);
  400. cache->page = page;
  401. cache->vaddr = (unsigned long)vaddr;
  402. return vaddr;
  403. }
  404. static void *reloc_vaddr(struct drm_i915_gem_object *obj,
  405. struct reloc_cache *cache,
  406. int page)
  407. {
  408. void *vaddr;
  409. if (cache->page == page) {
  410. vaddr = unmask_page(cache->vaddr);
  411. } else {
  412. vaddr = NULL;
  413. if ((cache->vaddr & KMAP) == 0)
  414. vaddr = reloc_iomap(obj, cache, page);
  415. if (!vaddr)
  416. vaddr = reloc_kmap(obj, cache, page);
  417. }
  418. return vaddr;
  419. }
  420. static void clflush_write32(u32 *addr, u32 value, unsigned int flushes)
  421. {
  422. if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) {
  423. if (flushes & CLFLUSH_BEFORE) {
  424. clflushopt(addr);
  425. mb();
  426. }
  427. *addr = value;
  428. /* Writes to the same cacheline are serialised by the CPU
  429. * (including clflush). On the write path, we only require
  430. * that it hits memory in an orderly fashion and place
  431. * mb barriers at the start and end of the relocation phase
  432. * to ensure ordering of clflush wrt to the system.
  433. */
  434. if (flushes & CLFLUSH_AFTER)
  435. clflushopt(addr);
  436. } else
  437. *addr = value;
  438. }
  439. static int
  440. relocate_entry(struct drm_i915_gem_object *obj,
  441. const struct drm_i915_gem_relocation_entry *reloc,
  442. struct reloc_cache *cache,
  443. u64 target_offset)
  444. {
  445. u64 offset = reloc->offset;
  446. bool wide = cache->use_64bit_reloc;
  447. void *vaddr;
  448. target_offset = relocation_target(reloc, target_offset);
  449. repeat:
  450. vaddr = reloc_vaddr(obj, cache, offset >> PAGE_SHIFT);
  451. if (IS_ERR(vaddr))
  452. return PTR_ERR(vaddr);
  453. clflush_write32(vaddr + offset_in_page(offset),
  454. lower_32_bits(target_offset),
  455. cache->vaddr);
  456. if (wide) {
  457. offset += sizeof(u32);
  458. target_offset >>= 32;
  459. wide = false;
  460. goto repeat;
  461. }
  462. return 0;
  463. }
  464. static int
  465. i915_gem_execbuffer_relocate_entry(struct i915_vma *vma,
  466. struct eb_vmas *eb,
  467. struct drm_i915_gem_relocation_entry *reloc,
  468. struct reloc_cache *cache)
  469. {
  470. struct drm_i915_gem_object *obj = vma->obj;
  471. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  472. struct drm_gem_object *target_obj;
  473. struct drm_i915_gem_object *target_i915_obj;
  474. struct i915_vma *target_vma;
  475. uint64_t target_offset;
  476. int ret;
  477. /* we've already hold a reference to all valid objects */
  478. target_vma = eb_get_vma(eb, reloc->target_handle);
  479. if (unlikely(target_vma == NULL))
  480. return -ENOENT;
  481. target_i915_obj = target_vma->obj;
  482. target_obj = &target_vma->obj->base;
  483. target_offset = gen8_canonical_addr(target_vma->node.start);
  484. /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
  485. * pipe_control writes because the gpu doesn't properly redirect them
  486. * through the ppgtt for non_secure batchbuffers. */
  487. if (unlikely(IS_GEN6(dev_priv) &&
  488. reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
  489. ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
  490. PIN_GLOBAL);
  491. if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
  492. return ret;
  493. }
  494. /* Validate that the target is in a valid r/w GPU domain */
  495. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  496. DRM_DEBUG("reloc with multiple write domains: "
  497. "obj %p target %d offset %d "
  498. "read %08x write %08x",
  499. obj, reloc->target_handle,
  500. (int) reloc->offset,
  501. reloc->read_domains,
  502. reloc->write_domain);
  503. return -EINVAL;
  504. }
  505. if (unlikely((reloc->write_domain | reloc->read_domains)
  506. & ~I915_GEM_GPU_DOMAINS)) {
  507. DRM_DEBUG("reloc with read/write non-GPU domains: "
  508. "obj %p target %d offset %d "
  509. "read %08x write %08x",
  510. obj, reloc->target_handle,
  511. (int) reloc->offset,
  512. reloc->read_domains,
  513. reloc->write_domain);
  514. return -EINVAL;
  515. }
  516. target_obj->pending_read_domains |= reloc->read_domains;
  517. target_obj->pending_write_domain |= reloc->write_domain;
  518. /* If the relocation already has the right value in it, no
  519. * more work needs to be done.
  520. */
  521. if (target_offset == reloc->presumed_offset)
  522. return 0;
  523. /* Check that the relocation address is valid... */
  524. if (unlikely(reloc->offset >
  525. obj->base.size - (cache->use_64bit_reloc ? 8 : 4))) {
  526. DRM_DEBUG("Relocation beyond object bounds: "
  527. "obj %p target %d offset %d size %d.\n",
  528. obj, reloc->target_handle,
  529. (int) reloc->offset,
  530. (int) obj->base.size);
  531. return -EINVAL;
  532. }
  533. if (unlikely(reloc->offset & 3)) {
  534. DRM_DEBUG("Relocation not 4-byte aligned: "
  535. "obj %p target %d offset %d.\n",
  536. obj, reloc->target_handle,
  537. (int) reloc->offset);
  538. return -EINVAL;
  539. }
  540. /*
  541. * If we write into the object, we need to force the synchronisation
  542. * barrier, either with an asynchronous clflush or if we executed the
  543. * patching using the GPU (though that should be serialised by the
  544. * timeline). To be completely sure, and since we are required to
  545. * do relocations we are already stalling, disable the user's opt
  546. * of our synchronisation.
  547. */
  548. vma->exec_entry->flags &= ~EXEC_OBJECT_ASYNC;
  549. ret = relocate_entry(obj, reloc, cache, target_offset);
  550. if (ret)
  551. return ret;
  552. /* and update the user's relocation entry */
  553. reloc->presumed_offset = target_offset;
  554. return 0;
  555. }
  556. static int
  557. i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
  558. struct eb_vmas *eb)
  559. {
  560. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  561. struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
  562. struct drm_i915_gem_relocation_entry __user *user_relocs;
  563. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  564. struct reloc_cache cache;
  565. int remain, ret = 0;
  566. user_relocs = u64_to_user_ptr(entry->relocs_ptr);
  567. reloc_cache_init(&cache, eb->i915);
  568. remain = entry->relocation_count;
  569. while (remain) {
  570. struct drm_i915_gem_relocation_entry *r = stack_reloc;
  571. unsigned long unwritten;
  572. unsigned int count;
  573. count = min_t(unsigned int, remain, ARRAY_SIZE(stack_reloc));
  574. remain -= count;
  575. /* This is the fast path and we cannot handle a pagefault
  576. * whilst holding the struct mutex lest the user pass in the
  577. * relocations contained within a mmaped bo. For in such a case
  578. * we, the page fault handler would call i915_gem_fault() and
  579. * we would try to acquire the struct mutex again. Obviously
  580. * this is bad and so lockdep complains vehemently.
  581. */
  582. pagefault_disable();
  583. unwritten = __copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0]));
  584. pagefault_enable();
  585. if (unlikely(unwritten)) {
  586. ret = -EFAULT;
  587. goto out;
  588. }
  589. do {
  590. u64 offset = r->presumed_offset;
  591. ret = i915_gem_execbuffer_relocate_entry(vma, eb, r, &cache);
  592. if (ret)
  593. goto out;
  594. if (r->presumed_offset != offset) {
  595. pagefault_disable();
  596. unwritten = __put_user(r->presumed_offset,
  597. &user_relocs->presumed_offset);
  598. pagefault_enable();
  599. if (unlikely(unwritten)) {
  600. /* Note that reporting an error now
  601. * leaves everything in an inconsistent
  602. * state as we have *already* changed
  603. * the relocation value inside the
  604. * object. As we have not changed the
  605. * reloc.presumed_offset or will not
  606. * change the execobject.offset, on the
  607. * call we may not rewrite the value
  608. * inside the object, leaving it
  609. * dangling and causing a GPU hang.
  610. */
  611. ret = -EFAULT;
  612. goto out;
  613. }
  614. }
  615. user_relocs++;
  616. r++;
  617. } while (--count);
  618. }
  619. out:
  620. reloc_cache_fini(&cache);
  621. return ret;
  622. #undef N_RELOC
  623. }
  624. static int
  625. i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
  626. struct eb_vmas *eb,
  627. struct drm_i915_gem_relocation_entry *relocs)
  628. {
  629. const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  630. struct reloc_cache cache;
  631. int i, ret = 0;
  632. reloc_cache_init(&cache, eb->i915);
  633. for (i = 0; i < entry->relocation_count; i++) {
  634. ret = i915_gem_execbuffer_relocate_entry(vma, eb, &relocs[i], &cache);
  635. if (ret)
  636. break;
  637. }
  638. reloc_cache_fini(&cache);
  639. return ret;
  640. }
  641. static int
  642. i915_gem_execbuffer_relocate(struct eb_vmas *eb)
  643. {
  644. struct i915_vma *vma;
  645. int ret = 0;
  646. list_for_each_entry(vma, &eb->vmas, exec_list) {
  647. ret = i915_gem_execbuffer_relocate_vma(vma, eb);
  648. if (ret)
  649. break;
  650. }
  651. return ret;
  652. }
  653. static bool only_mappable_for_reloc(unsigned int flags)
  654. {
  655. return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
  656. __EXEC_OBJECT_NEEDS_MAP;
  657. }
  658. static int
  659. i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
  660. struct intel_engine_cs *engine,
  661. bool *need_reloc)
  662. {
  663. struct drm_i915_gem_object *obj = vma->obj;
  664. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  665. uint64_t flags;
  666. int ret;
  667. flags = PIN_USER;
  668. if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
  669. flags |= PIN_GLOBAL;
  670. if (!drm_mm_node_allocated(&vma->node)) {
  671. /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
  672. * limit address to the first 4GBs for unflagged objects.
  673. */
  674. if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
  675. flags |= PIN_ZONE_4G;
  676. if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
  677. flags |= PIN_GLOBAL | PIN_MAPPABLE;
  678. if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
  679. flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
  680. if (entry->flags & EXEC_OBJECT_PINNED)
  681. flags |= entry->offset | PIN_OFFSET_FIXED;
  682. if ((flags & PIN_MAPPABLE) == 0)
  683. flags |= PIN_HIGH;
  684. }
  685. ret = i915_vma_pin(vma,
  686. entry->pad_to_size,
  687. entry->alignment,
  688. flags);
  689. if ((ret == -ENOSPC || ret == -E2BIG) &&
  690. only_mappable_for_reloc(entry->flags))
  691. ret = i915_vma_pin(vma,
  692. entry->pad_to_size,
  693. entry->alignment,
  694. flags & ~PIN_MAPPABLE);
  695. if (ret)
  696. return ret;
  697. entry->flags |= __EXEC_OBJECT_HAS_PIN;
  698. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  699. ret = i915_vma_get_fence(vma);
  700. if (ret)
  701. return ret;
  702. if (i915_vma_pin_fence(vma))
  703. entry->flags |= __EXEC_OBJECT_HAS_FENCE;
  704. }
  705. if (entry->offset != vma->node.start) {
  706. entry->offset = vma->node.start;
  707. *need_reloc = true;
  708. }
  709. if (entry->flags & EXEC_OBJECT_WRITE) {
  710. obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
  711. obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
  712. }
  713. return 0;
  714. }
  715. static bool
  716. need_reloc_mappable(struct i915_vma *vma)
  717. {
  718. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  719. if (entry->relocation_count == 0)
  720. return false;
  721. if (!i915_vma_is_ggtt(vma))
  722. return false;
  723. /* See also use_cpu_reloc() */
  724. if (HAS_LLC(to_i915(vma->obj->base.dev)))
  725. return false;
  726. if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  727. return false;
  728. return true;
  729. }
  730. static bool
  731. eb_vma_misplaced(struct i915_vma *vma)
  732. {
  733. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  734. WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
  735. !i915_vma_is_ggtt(vma));
  736. if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment))
  737. return true;
  738. if (vma->node.size < entry->pad_to_size)
  739. return true;
  740. if (entry->flags & EXEC_OBJECT_PINNED &&
  741. vma->node.start != entry->offset)
  742. return true;
  743. if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
  744. vma->node.start < BATCH_OFFSET_BIAS)
  745. return true;
  746. /* avoid costly ping-pong once a batch bo ended up non-mappable */
  747. if (entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
  748. !i915_vma_is_map_and_fenceable(vma))
  749. return !only_mappable_for_reloc(entry->flags);
  750. if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
  751. (vma->node.start + vma->node.size - 1) >> 32)
  752. return true;
  753. return false;
  754. }
  755. static int
  756. i915_gem_execbuffer_reserve(struct intel_engine_cs *engine,
  757. struct list_head *vmas,
  758. struct i915_gem_context *ctx,
  759. bool *need_relocs)
  760. {
  761. struct drm_i915_gem_object *obj;
  762. struct i915_vma *vma;
  763. struct i915_address_space *vm;
  764. struct list_head ordered_vmas;
  765. struct list_head pinned_vmas;
  766. bool has_fenced_gpu_access = INTEL_GEN(engine->i915) < 4;
  767. bool needs_unfenced_map = INTEL_INFO(engine->i915)->unfenced_needs_alignment;
  768. int retry;
  769. vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
  770. INIT_LIST_HEAD(&ordered_vmas);
  771. INIT_LIST_HEAD(&pinned_vmas);
  772. while (!list_empty(vmas)) {
  773. struct drm_i915_gem_exec_object2 *entry;
  774. bool need_fence, need_mappable;
  775. vma = list_first_entry(vmas, struct i915_vma, exec_list);
  776. obj = vma->obj;
  777. entry = vma->exec_entry;
  778. if (ctx->flags & CONTEXT_NO_ZEROMAP)
  779. entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  780. if (!has_fenced_gpu_access)
  781. entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
  782. need_fence =
  783. (entry->flags & EXEC_OBJECT_NEEDS_FENCE ||
  784. needs_unfenced_map) &&
  785. i915_gem_object_is_tiled(obj);
  786. need_mappable = need_fence || need_reloc_mappable(vma);
  787. if (entry->flags & EXEC_OBJECT_PINNED)
  788. list_move_tail(&vma->exec_list, &pinned_vmas);
  789. else if (need_mappable) {
  790. entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
  791. list_move(&vma->exec_list, &ordered_vmas);
  792. } else
  793. list_move_tail(&vma->exec_list, &ordered_vmas);
  794. obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  795. obj->base.pending_write_domain = 0;
  796. }
  797. list_splice(&ordered_vmas, vmas);
  798. list_splice(&pinned_vmas, vmas);
  799. /* Attempt to pin all of the buffers into the GTT.
  800. * This is done in 3 phases:
  801. *
  802. * 1a. Unbind all objects that do not match the GTT constraints for
  803. * the execbuffer (fenceable, mappable, alignment etc).
  804. * 1b. Increment pin count for already bound objects.
  805. * 2. Bind new objects.
  806. * 3. Decrement pin count.
  807. *
  808. * This avoid unnecessary unbinding of later objects in order to make
  809. * room for the earlier objects *unless* we need to defragment.
  810. */
  811. retry = 0;
  812. do {
  813. int ret = 0;
  814. /* Unbind any ill-fitting objects or pin. */
  815. list_for_each_entry(vma, vmas, exec_list) {
  816. if (!drm_mm_node_allocated(&vma->node))
  817. continue;
  818. if (eb_vma_misplaced(vma))
  819. ret = i915_vma_unbind(vma);
  820. else
  821. ret = i915_gem_execbuffer_reserve_vma(vma,
  822. engine,
  823. need_relocs);
  824. if (ret)
  825. goto err;
  826. }
  827. /* Bind fresh objects */
  828. list_for_each_entry(vma, vmas, exec_list) {
  829. if (drm_mm_node_allocated(&vma->node))
  830. continue;
  831. ret = i915_gem_execbuffer_reserve_vma(vma, engine,
  832. need_relocs);
  833. if (ret)
  834. goto err;
  835. }
  836. err:
  837. if (ret != -ENOSPC || retry++)
  838. return ret;
  839. /* Decrement pin count for bound objects */
  840. list_for_each_entry(vma, vmas, exec_list)
  841. i915_gem_execbuffer_unreserve_vma(vma);
  842. ret = i915_gem_evict_vm(vm, true);
  843. if (ret)
  844. return ret;
  845. } while (1);
  846. }
  847. static int
  848. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  849. struct drm_i915_gem_execbuffer2 *args,
  850. struct drm_file *file,
  851. struct intel_engine_cs *engine,
  852. struct eb_vmas *eb,
  853. struct drm_i915_gem_exec_object2 *exec,
  854. struct i915_gem_context *ctx)
  855. {
  856. struct drm_i915_gem_relocation_entry *reloc;
  857. struct i915_address_space *vm;
  858. struct i915_vma *vma;
  859. bool need_relocs;
  860. int *reloc_offset;
  861. int i, total, ret;
  862. unsigned count = args->buffer_count;
  863. vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
  864. /* We may process another execbuffer during the unlock... */
  865. while (!list_empty(&eb->vmas)) {
  866. vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
  867. list_del_init(&vma->exec_list);
  868. i915_gem_execbuffer_unreserve_vma(vma);
  869. i915_vma_put(vma);
  870. }
  871. mutex_unlock(&dev->struct_mutex);
  872. total = 0;
  873. for (i = 0; i < count; i++)
  874. total += exec[i].relocation_count;
  875. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  876. reloc = drm_malloc_ab(total, sizeof(*reloc));
  877. if (reloc == NULL || reloc_offset == NULL) {
  878. drm_free_large(reloc);
  879. drm_free_large(reloc_offset);
  880. mutex_lock(&dev->struct_mutex);
  881. return -ENOMEM;
  882. }
  883. total = 0;
  884. for (i = 0; i < count; i++) {
  885. struct drm_i915_gem_relocation_entry __user *user_relocs;
  886. u64 invalid_offset = (u64)-1;
  887. int j;
  888. user_relocs = u64_to_user_ptr(exec[i].relocs_ptr);
  889. if (copy_from_user(reloc+total, user_relocs,
  890. exec[i].relocation_count * sizeof(*reloc))) {
  891. ret = -EFAULT;
  892. mutex_lock(&dev->struct_mutex);
  893. goto err;
  894. }
  895. /* As we do not update the known relocation offsets after
  896. * relocating (due to the complexities in lock handling),
  897. * we need to mark them as invalid now so that we force the
  898. * relocation processing next time. Just in case the target
  899. * object is evicted and then rebound into its old
  900. * presumed_offset before the next execbuffer - if that
  901. * happened we would make the mistake of assuming that the
  902. * relocations were valid.
  903. */
  904. for (j = 0; j < exec[i].relocation_count; j++) {
  905. if (__copy_to_user(&user_relocs[j].presumed_offset,
  906. &invalid_offset,
  907. sizeof(invalid_offset))) {
  908. ret = -EFAULT;
  909. mutex_lock(&dev->struct_mutex);
  910. goto err;
  911. }
  912. }
  913. reloc_offset[i] = total;
  914. total += exec[i].relocation_count;
  915. }
  916. ret = i915_mutex_lock_interruptible(dev);
  917. if (ret) {
  918. mutex_lock(&dev->struct_mutex);
  919. goto err;
  920. }
  921. /* reacquire the objects */
  922. eb_reset(eb);
  923. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  924. if (ret)
  925. goto err;
  926. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  927. ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
  928. &need_relocs);
  929. if (ret)
  930. goto err;
  931. list_for_each_entry(vma, &eb->vmas, exec_list) {
  932. int offset = vma->exec_entry - exec;
  933. ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
  934. reloc + reloc_offset[offset]);
  935. if (ret)
  936. goto err;
  937. }
  938. /* Leave the user relocations as are, this is the painfully slow path,
  939. * and we want to avoid the complication of dropping the lock whilst
  940. * having buffers reserved in the aperture and so causing spurious
  941. * ENOSPC for random operations.
  942. */
  943. err:
  944. drm_free_large(reloc);
  945. drm_free_large(reloc_offset);
  946. return ret;
  947. }
  948. static int
  949. i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
  950. struct list_head *vmas)
  951. {
  952. struct i915_vma *vma;
  953. int ret;
  954. list_for_each_entry(vma, vmas, exec_list) {
  955. struct drm_i915_gem_object *obj = vma->obj;
  956. if (vma->exec_entry->flags & EXEC_OBJECT_ASYNC)
  957. continue;
  958. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU) {
  959. i915_gem_clflush_object(obj, 0);
  960. obj->base.write_domain = 0;
  961. }
  962. ret = i915_gem_request_await_object
  963. (req, obj, obj->base.pending_write_domain);
  964. if (ret)
  965. return ret;
  966. }
  967. /* Unconditionally flush any chipset caches (for streaming writes). */
  968. i915_gem_chipset_flush(req->engine->i915);
  969. /* Unconditionally invalidate GPU caches and TLBs. */
  970. return req->engine->emit_flush(req, EMIT_INVALIDATE);
  971. }
  972. static bool
  973. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  974. {
  975. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  976. return false;
  977. /* Kernel clipping was a DRI1 misfeature */
  978. if (exec->num_cliprects || exec->cliprects_ptr)
  979. return false;
  980. if (exec->DR4 == 0xffffffff) {
  981. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  982. exec->DR4 = 0;
  983. }
  984. if (exec->DR1 || exec->DR4)
  985. return false;
  986. if ((exec->batch_start_offset | exec->batch_len) & 0x7)
  987. return false;
  988. return true;
  989. }
  990. static int
  991. validate_exec_list(struct drm_device *dev,
  992. struct drm_i915_gem_exec_object2 *exec,
  993. int count)
  994. {
  995. unsigned relocs_total = 0;
  996. unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  997. unsigned invalid_flags;
  998. int i;
  999. /* INTERNAL flags must not overlap with external ones */
  1000. BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
  1001. invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  1002. if (USES_FULL_PPGTT(dev))
  1003. invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  1004. for (i = 0; i < count; i++) {
  1005. char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
  1006. int length; /* limited by fault_in_pages_readable() */
  1007. if (exec[i].flags & invalid_flags)
  1008. return -EINVAL;
  1009. /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
  1010. * any non-page-aligned or non-canonical addresses.
  1011. */
  1012. if (exec[i].flags & EXEC_OBJECT_PINNED) {
  1013. if (exec[i].offset !=
  1014. gen8_canonical_addr(exec[i].offset & PAGE_MASK))
  1015. return -EINVAL;
  1016. }
  1017. /* From drm_mm perspective address space is continuous,
  1018. * so from this point we're always using non-canonical
  1019. * form internally.
  1020. */
  1021. exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
  1022. if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
  1023. return -EINVAL;
  1024. /* pad_to_size was once a reserved field, so sanitize it */
  1025. if (exec[i].flags & EXEC_OBJECT_PAD_TO_SIZE) {
  1026. if (offset_in_page(exec[i].pad_to_size))
  1027. return -EINVAL;
  1028. } else {
  1029. exec[i].pad_to_size = 0;
  1030. }
  1031. /* First check for malicious input causing overflow in
  1032. * the worst case where we need to allocate the entire
  1033. * relocation tree as a single array.
  1034. */
  1035. if (exec[i].relocation_count > relocs_max - relocs_total)
  1036. return -EINVAL;
  1037. relocs_total += exec[i].relocation_count;
  1038. length = exec[i].relocation_count *
  1039. sizeof(struct drm_i915_gem_relocation_entry);
  1040. /*
  1041. * We must check that the entire relocation array is safe
  1042. * to read, but since we may need to update the presumed
  1043. * offsets during execution, check for full write access.
  1044. */
  1045. if (!access_ok(VERIFY_WRITE, ptr, length))
  1046. return -EFAULT;
  1047. if (likely(!i915.prefault_disable)) {
  1048. if (fault_in_pages_readable(ptr, length))
  1049. return -EFAULT;
  1050. }
  1051. }
  1052. return 0;
  1053. }
  1054. static struct i915_gem_context *
  1055. i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
  1056. struct intel_engine_cs *engine, const u32 ctx_id)
  1057. {
  1058. struct i915_gem_context *ctx;
  1059. ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
  1060. if (IS_ERR(ctx))
  1061. return ctx;
  1062. if (i915_gem_context_is_banned(ctx)) {
  1063. DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
  1064. return ERR_PTR(-EIO);
  1065. }
  1066. return ctx;
  1067. }
  1068. static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  1069. {
  1070. return !(obj->cache_level == I915_CACHE_NONE ||
  1071. obj->cache_level == I915_CACHE_WT);
  1072. }
  1073. void i915_vma_move_to_active(struct i915_vma *vma,
  1074. struct drm_i915_gem_request *req,
  1075. unsigned int flags)
  1076. {
  1077. struct drm_i915_gem_object *obj = vma->obj;
  1078. const unsigned int idx = req->engine->id;
  1079. lockdep_assert_held(&req->i915->drm.struct_mutex);
  1080. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  1081. /* Add a reference if we're newly entering the active list.
  1082. * The order in which we add operations to the retirement queue is
  1083. * vital here: mark_active adds to the start of the callback list,
  1084. * such that subsequent callbacks are called first. Therefore we
  1085. * add the active reference first and queue for it to be dropped
  1086. * *last*.
  1087. */
  1088. if (!i915_vma_is_active(vma))
  1089. obj->active_count++;
  1090. i915_vma_set_active(vma, idx);
  1091. i915_gem_active_set(&vma->last_read[idx], req);
  1092. list_move_tail(&vma->vm_link, &vma->vm->active_list);
  1093. if (flags & EXEC_OBJECT_WRITE) {
  1094. if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
  1095. i915_gem_active_set(&obj->frontbuffer_write, req);
  1096. /* update for the implicit flush after a batch */
  1097. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  1098. if (!obj->cache_dirty && gpu_write_needs_clflush(obj))
  1099. obj->cache_dirty = true;
  1100. }
  1101. if (flags & EXEC_OBJECT_NEEDS_FENCE)
  1102. i915_gem_active_set(&vma->last_fence, req);
  1103. }
  1104. static void eb_export_fence(struct drm_i915_gem_object *obj,
  1105. struct drm_i915_gem_request *req,
  1106. unsigned int flags)
  1107. {
  1108. struct reservation_object *resv = obj->resv;
  1109. /* Ignore errors from failing to allocate the new fence, we can't
  1110. * handle an error right now. Worst case should be missed
  1111. * synchronisation leading to rendering corruption.
  1112. */
  1113. reservation_object_lock(resv, NULL);
  1114. if (flags & EXEC_OBJECT_WRITE)
  1115. reservation_object_add_excl_fence(resv, &req->fence);
  1116. else if (reservation_object_reserve_shared(resv) == 0)
  1117. reservation_object_add_shared_fence(resv, &req->fence);
  1118. reservation_object_unlock(resv);
  1119. }
  1120. static void
  1121. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  1122. struct drm_i915_gem_request *req)
  1123. {
  1124. struct i915_vma *vma;
  1125. list_for_each_entry(vma, vmas, exec_list) {
  1126. struct drm_i915_gem_object *obj = vma->obj;
  1127. obj->base.write_domain = obj->base.pending_write_domain;
  1128. if (obj->base.write_domain)
  1129. vma->exec_entry->flags |= EXEC_OBJECT_WRITE;
  1130. else
  1131. obj->base.pending_read_domains |= obj->base.read_domains;
  1132. obj->base.read_domains = obj->base.pending_read_domains;
  1133. i915_vma_move_to_active(vma, req, vma->exec_entry->flags);
  1134. eb_export_fence(obj, req, vma->exec_entry->flags);
  1135. }
  1136. }
  1137. static int
  1138. i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
  1139. {
  1140. u32 *cs;
  1141. int i;
  1142. if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
  1143. DRM_DEBUG("sol reset is gen7/rcs only\n");
  1144. return -EINVAL;
  1145. }
  1146. cs = intel_ring_begin(req, 4 * 3);
  1147. if (IS_ERR(cs))
  1148. return PTR_ERR(cs);
  1149. for (i = 0; i < 4; i++) {
  1150. *cs++ = MI_LOAD_REGISTER_IMM(1);
  1151. *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i));
  1152. *cs++ = 0;
  1153. }
  1154. intel_ring_advance(req, cs);
  1155. return 0;
  1156. }
  1157. static struct i915_vma *
  1158. i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
  1159. struct drm_i915_gem_exec_object2 *shadow_exec_entry,
  1160. struct drm_i915_gem_object *batch_obj,
  1161. struct eb_vmas *eb,
  1162. u32 batch_start_offset,
  1163. u32 batch_len,
  1164. bool is_master)
  1165. {
  1166. struct drm_i915_gem_object *shadow_batch_obj;
  1167. struct i915_vma *vma;
  1168. int ret;
  1169. shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
  1170. PAGE_ALIGN(batch_len));
  1171. if (IS_ERR(shadow_batch_obj))
  1172. return ERR_CAST(shadow_batch_obj);
  1173. ret = intel_engine_cmd_parser(engine,
  1174. batch_obj,
  1175. shadow_batch_obj,
  1176. batch_start_offset,
  1177. batch_len,
  1178. is_master);
  1179. if (ret) {
  1180. if (ret == -EACCES) /* unhandled chained batch */
  1181. vma = NULL;
  1182. else
  1183. vma = ERR_PTR(ret);
  1184. goto out;
  1185. }
  1186. vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
  1187. if (IS_ERR(vma))
  1188. goto out;
  1189. memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
  1190. vma->exec_entry = shadow_exec_entry;
  1191. vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
  1192. i915_gem_object_get(shadow_batch_obj);
  1193. list_add_tail(&vma->exec_list, &eb->vmas);
  1194. out:
  1195. i915_gem_object_unpin_pages(shadow_batch_obj);
  1196. return vma;
  1197. }
  1198. static void
  1199. add_to_client(struct drm_i915_gem_request *req,
  1200. struct drm_file *file)
  1201. {
  1202. req->file_priv = file->driver_priv;
  1203. list_add_tail(&req->client_link, &req->file_priv->mm.request_list);
  1204. }
  1205. static int
  1206. execbuf_submit(struct i915_execbuffer_params *params,
  1207. struct drm_i915_gem_execbuffer2 *args,
  1208. struct list_head *vmas)
  1209. {
  1210. u64 exec_start, exec_len;
  1211. int ret;
  1212. ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
  1213. if (ret)
  1214. return ret;
  1215. ret = i915_switch_context(params->request);
  1216. if (ret)
  1217. return ret;
  1218. if (args->flags & I915_EXEC_CONSTANTS_MASK) {
  1219. DRM_DEBUG("I915_EXEC_CONSTANTS_* unsupported\n");
  1220. return -EINVAL;
  1221. }
  1222. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1223. ret = i915_reset_gen7_sol_offsets(params->request);
  1224. if (ret)
  1225. return ret;
  1226. }
  1227. exec_len = args->batch_len;
  1228. exec_start = params->batch->node.start +
  1229. params->args_batch_start_offset;
  1230. if (exec_len == 0)
  1231. exec_len = params->batch->size - params->args_batch_start_offset;
  1232. ret = params->engine->emit_bb_start(params->request,
  1233. exec_start, exec_len,
  1234. params->dispatch_flags);
  1235. if (ret)
  1236. return ret;
  1237. i915_gem_execbuffer_move_to_active(vmas, params->request);
  1238. return 0;
  1239. }
  1240. /**
  1241. * Find one BSD ring to dispatch the corresponding BSD command.
  1242. * The engine index is returned.
  1243. */
  1244. static unsigned int
  1245. gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv,
  1246. struct drm_file *file)
  1247. {
  1248. struct drm_i915_file_private *file_priv = file->driver_priv;
  1249. /* Check whether the file_priv has already selected one ring. */
  1250. if ((int)file_priv->bsd_engine < 0)
  1251. file_priv->bsd_engine = atomic_fetch_xor(1,
  1252. &dev_priv->mm.bsd_engine_dispatch_index);
  1253. return file_priv->bsd_engine;
  1254. }
  1255. #define I915_USER_RINGS (4)
  1256. static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
  1257. [I915_EXEC_DEFAULT] = RCS,
  1258. [I915_EXEC_RENDER] = RCS,
  1259. [I915_EXEC_BLT] = BCS,
  1260. [I915_EXEC_BSD] = VCS,
  1261. [I915_EXEC_VEBOX] = VECS
  1262. };
  1263. static struct intel_engine_cs *
  1264. eb_select_engine(struct drm_i915_private *dev_priv,
  1265. struct drm_file *file,
  1266. struct drm_i915_gem_execbuffer2 *args)
  1267. {
  1268. unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
  1269. struct intel_engine_cs *engine;
  1270. if (user_ring_id > I915_USER_RINGS) {
  1271. DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
  1272. return NULL;
  1273. }
  1274. if ((user_ring_id != I915_EXEC_BSD) &&
  1275. ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
  1276. DRM_DEBUG("execbuf with non bsd ring but with invalid "
  1277. "bsd dispatch flags: %d\n", (int)(args->flags));
  1278. return NULL;
  1279. }
  1280. if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
  1281. unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
  1282. if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
  1283. bsd_idx = gen8_dispatch_bsd_engine(dev_priv, file);
  1284. } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
  1285. bsd_idx <= I915_EXEC_BSD_RING2) {
  1286. bsd_idx >>= I915_EXEC_BSD_SHIFT;
  1287. bsd_idx--;
  1288. } else {
  1289. DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
  1290. bsd_idx);
  1291. return NULL;
  1292. }
  1293. engine = dev_priv->engine[_VCS(bsd_idx)];
  1294. } else {
  1295. engine = dev_priv->engine[user_ring_map[user_ring_id]];
  1296. }
  1297. if (!engine) {
  1298. DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
  1299. return NULL;
  1300. }
  1301. return engine;
  1302. }
  1303. static int
  1304. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  1305. struct drm_file *file,
  1306. struct drm_i915_gem_execbuffer2 *args,
  1307. struct drm_i915_gem_exec_object2 *exec)
  1308. {
  1309. struct drm_i915_private *dev_priv = to_i915(dev);
  1310. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1311. struct eb_vmas *eb;
  1312. struct drm_i915_gem_exec_object2 shadow_exec_entry;
  1313. struct intel_engine_cs *engine;
  1314. struct i915_gem_context *ctx;
  1315. struct i915_address_space *vm;
  1316. struct i915_execbuffer_params params_master; /* XXX: will be removed later */
  1317. struct i915_execbuffer_params *params = &params_master;
  1318. const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  1319. u32 dispatch_flags;
  1320. struct dma_fence *in_fence = NULL;
  1321. struct sync_file *out_fence = NULL;
  1322. int out_fence_fd = -1;
  1323. int ret;
  1324. bool need_relocs;
  1325. if (!i915_gem_check_execbuffer(args))
  1326. return -EINVAL;
  1327. ret = validate_exec_list(dev, exec, args->buffer_count);
  1328. if (ret)
  1329. return ret;
  1330. dispatch_flags = 0;
  1331. if (args->flags & I915_EXEC_SECURE) {
  1332. if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
  1333. return -EPERM;
  1334. dispatch_flags |= I915_DISPATCH_SECURE;
  1335. }
  1336. if (args->flags & I915_EXEC_IS_PINNED)
  1337. dispatch_flags |= I915_DISPATCH_PINNED;
  1338. engine = eb_select_engine(dev_priv, file, args);
  1339. if (!engine)
  1340. return -EINVAL;
  1341. if (args->buffer_count < 1) {
  1342. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1343. return -EINVAL;
  1344. }
  1345. if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
  1346. if (!HAS_RESOURCE_STREAMER(dev_priv)) {
  1347. DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
  1348. return -EINVAL;
  1349. }
  1350. if (engine->id != RCS) {
  1351. DRM_DEBUG("RS is not available on %s\n",
  1352. engine->name);
  1353. return -EINVAL;
  1354. }
  1355. dispatch_flags |= I915_DISPATCH_RS;
  1356. }
  1357. if (args->flags & I915_EXEC_FENCE_IN) {
  1358. in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2));
  1359. if (!in_fence)
  1360. return -EINVAL;
  1361. }
  1362. if (args->flags & I915_EXEC_FENCE_OUT) {
  1363. out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
  1364. if (out_fence_fd < 0) {
  1365. ret = out_fence_fd;
  1366. goto err_in_fence;
  1367. }
  1368. }
  1369. /* Take a local wakeref for preparing to dispatch the execbuf as
  1370. * we expect to access the hardware fairly frequently in the
  1371. * process. Upon first dispatch, we acquire another prolonged
  1372. * wakeref that we hold until the GPU has been idle for at least
  1373. * 100ms.
  1374. */
  1375. intel_runtime_pm_get(dev_priv);
  1376. ret = i915_mutex_lock_interruptible(dev);
  1377. if (ret)
  1378. goto pre_mutex_err;
  1379. ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
  1380. if (IS_ERR(ctx)) {
  1381. mutex_unlock(&dev->struct_mutex);
  1382. ret = PTR_ERR(ctx);
  1383. goto pre_mutex_err;
  1384. }
  1385. i915_gem_context_get(ctx);
  1386. if (ctx->ppgtt)
  1387. vm = &ctx->ppgtt->base;
  1388. else
  1389. vm = &ggtt->base;
  1390. memset(&params_master, 0x00, sizeof(params_master));
  1391. eb = eb_create(dev_priv, args);
  1392. if (eb == NULL) {
  1393. i915_gem_context_put(ctx);
  1394. mutex_unlock(&dev->struct_mutex);
  1395. ret = -ENOMEM;
  1396. goto pre_mutex_err;
  1397. }
  1398. /* Look up object handles */
  1399. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  1400. if (ret)
  1401. goto err;
  1402. /* take note of the batch buffer before we might reorder the lists */
  1403. params->batch = eb_get_batch(eb);
  1404. /* Move the objects en-masse into the GTT, evicting if necessary. */
  1405. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  1406. ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
  1407. &need_relocs);
  1408. if (ret)
  1409. goto err;
  1410. /* The objects are in their final locations, apply the relocations. */
  1411. if (need_relocs)
  1412. ret = i915_gem_execbuffer_relocate(eb);
  1413. if (ret) {
  1414. if (ret == -EFAULT) {
  1415. ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
  1416. engine,
  1417. eb, exec, ctx);
  1418. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1419. }
  1420. if (ret)
  1421. goto err;
  1422. }
  1423. /* Set the pending read domains for the batch buffer to COMMAND */
  1424. if (params->batch->obj->base.pending_write_domain) {
  1425. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1426. ret = -EINVAL;
  1427. goto err;
  1428. }
  1429. if (args->batch_start_offset > params->batch->size ||
  1430. args->batch_len > params->batch->size - args->batch_start_offset) {
  1431. DRM_DEBUG("Attempting to use out-of-bounds batch\n");
  1432. ret = -EINVAL;
  1433. goto err;
  1434. }
  1435. params->args_batch_start_offset = args->batch_start_offset;
  1436. if (engine->needs_cmd_parser && args->batch_len) {
  1437. struct i915_vma *vma;
  1438. vma = i915_gem_execbuffer_parse(engine, &shadow_exec_entry,
  1439. params->batch->obj,
  1440. eb,
  1441. args->batch_start_offset,
  1442. args->batch_len,
  1443. drm_is_current_master(file));
  1444. if (IS_ERR(vma)) {
  1445. ret = PTR_ERR(vma);
  1446. goto err;
  1447. }
  1448. if (vma) {
  1449. /*
  1450. * Batch parsed and accepted:
  1451. *
  1452. * Set the DISPATCH_SECURE bit to remove the NON_SECURE
  1453. * bit from MI_BATCH_BUFFER_START commands issued in
  1454. * the dispatch_execbuffer implementations. We
  1455. * specifically don't want that set on batches the
  1456. * command parser has accepted.
  1457. */
  1458. dispatch_flags |= I915_DISPATCH_SECURE;
  1459. params->args_batch_start_offset = 0;
  1460. params->batch = vma;
  1461. }
  1462. }
  1463. params->batch->obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1464. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1465. * batch" bit. Hence we need to pin secure batches into the global gtt.
  1466. * hsw should have this fixed, but bdw mucks it up again. */
  1467. if (dispatch_flags & I915_DISPATCH_SECURE) {
  1468. struct drm_i915_gem_object *obj = params->batch->obj;
  1469. struct i915_vma *vma;
  1470. /*
  1471. * So on first glance it looks freaky that we pin the batch here
  1472. * outside of the reservation loop. But:
  1473. * - The batch is already pinned into the relevant ppgtt, so we
  1474. * already have the backing storage fully allocated.
  1475. * - No other BO uses the global gtt (well contexts, but meh),
  1476. * so we don't really have issues with multiple objects not
  1477. * fitting due to fragmentation.
  1478. * So this is actually safe.
  1479. */
  1480. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
  1481. if (IS_ERR(vma)) {
  1482. ret = PTR_ERR(vma);
  1483. goto err;
  1484. }
  1485. params->batch = vma;
  1486. }
  1487. /* Allocate a request for this batch buffer nice and early. */
  1488. params->request = i915_gem_request_alloc(engine, ctx);
  1489. if (IS_ERR(params->request)) {
  1490. ret = PTR_ERR(params->request);
  1491. goto err_batch_unpin;
  1492. }
  1493. if (in_fence) {
  1494. ret = i915_gem_request_await_dma_fence(params->request,
  1495. in_fence);
  1496. if (ret < 0)
  1497. goto err_request;
  1498. }
  1499. if (out_fence_fd != -1) {
  1500. out_fence = sync_file_create(&params->request->fence);
  1501. if (!out_fence) {
  1502. ret = -ENOMEM;
  1503. goto err_request;
  1504. }
  1505. }
  1506. /* Whilst this request exists, batch_obj will be on the
  1507. * active_list, and so will hold the active reference. Only when this
  1508. * request is retired will the the batch_obj be moved onto the
  1509. * inactive_list and lose its active reference. Hence we do not need
  1510. * to explicitly hold another reference here.
  1511. */
  1512. params->request->batch = params->batch;
  1513. /*
  1514. * Save assorted stuff away to pass through to *_submission().
  1515. * NB: This data should be 'persistent' and not local as it will
  1516. * kept around beyond the duration of the IOCTL once the GPU
  1517. * scheduler arrives.
  1518. */
  1519. params->dev = dev;
  1520. params->file = file;
  1521. params->engine = engine;
  1522. params->dispatch_flags = dispatch_flags;
  1523. params->ctx = ctx;
  1524. trace_i915_gem_request_queue(params->request, dispatch_flags);
  1525. ret = execbuf_submit(params, args, &eb->vmas);
  1526. err_request:
  1527. __i915_add_request(params->request, ret == 0);
  1528. add_to_client(params->request, file);
  1529. if (out_fence) {
  1530. if (ret == 0) {
  1531. fd_install(out_fence_fd, out_fence->file);
  1532. args->rsvd2 &= GENMASK_ULL(0, 31); /* keep in-fence */
  1533. args->rsvd2 |= (u64)out_fence_fd << 32;
  1534. out_fence_fd = -1;
  1535. } else {
  1536. fput(out_fence->file);
  1537. }
  1538. }
  1539. err_batch_unpin:
  1540. /*
  1541. * FIXME: We crucially rely upon the active tracking for the (ppgtt)
  1542. * batch vma for correctness. For less ugly and less fragility this
  1543. * needs to be adjusted to also track the ggtt batch vma properly as
  1544. * active.
  1545. */
  1546. if (dispatch_flags & I915_DISPATCH_SECURE)
  1547. i915_vma_unpin(params->batch);
  1548. err:
  1549. /* the request owns the ref now */
  1550. i915_gem_context_put(ctx);
  1551. eb_destroy(eb);
  1552. mutex_unlock(&dev->struct_mutex);
  1553. pre_mutex_err:
  1554. /* intel_gpu_busy should also get a ref, so it will free when the device
  1555. * is really idle. */
  1556. intel_runtime_pm_put(dev_priv);
  1557. if (out_fence_fd != -1)
  1558. put_unused_fd(out_fence_fd);
  1559. err_in_fence:
  1560. dma_fence_put(in_fence);
  1561. return ret;
  1562. }
  1563. /*
  1564. * Legacy execbuffer just creates an exec2 list from the original exec object
  1565. * list array and passes it to the real function.
  1566. */
  1567. int
  1568. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1569. struct drm_file *file)
  1570. {
  1571. struct drm_i915_gem_execbuffer *args = data;
  1572. struct drm_i915_gem_execbuffer2 exec2;
  1573. struct drm_i915_gem_exec_object *exec_list = NULL;
  1574. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1575. int ret, i;
  1576. if (args->buffer_count < 1) {
  1577. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1578. return -EINVAL;
  1579. }
  1580. /* Copy in the exec list from userland */
  1581. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1582. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1583. if (exec_list == NULL || exec2_list == NULL) {
  1584. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1585. args->buffer_count);
  1586. drm_free_large(exec_list);
  1587. drm_free_large(exec2_list);
  1588. return -ENOMEM;
  1589. }
  1590. ret = copy_from_user(exec_list,
  1591. u64_to_user_ptr(args->buffers_ptr),
  1592. sizeof(*exec_list) * args->buffer_count);
  1593. if (ret != 0) {
  1594. DRM_DEBUG("copy %d exec entries failed %d\n",
  1595. args->buffer_count, ret);
  1596. drm_free_large(exec_list);
  1597. drm_free_large(exec2_list);
  1598. return -EFAULT;
  1599. }
  1600. for (i = 0; i < args->buffer_count; i++) {
  1601. exec2_list[i].handle = exec_list[i].handle;
  1602. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1603. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1604. exec2_list[i].alignment = exec_list[i].alignment;
  1605. exec2_list[i].offset = exec_list[i].offset;
  1606. if (INTEL_GEN(to_i915(dev)) < 4)
  1607. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1608. else
  1609. exec2_list[i].flags = 0;
  1610. }
  1611. exec2.buffers_ptr = args->buffers_ptr;
  1612. exec2.buffer_count = args->buffer_count;
  1613. exec2.batch_start_offset = args->batch_start_offset;
  1614. exec2.batch_len = args->batch_len;
  1615. exec2.DR1 = args->DR1;
  1616. exec2.DR4 = args->DR4;
  1617. exec2.num_cliprects = args->num_cliprects;
  1618. exec2.cliprects_ptr = args->cliprects_ptr;
  1619. exec2.flags = I915_EXEC_RENDER;
  1620. i915_execbuffer2_set_context_id(exec2, 0);
  1621. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1622. if (!ret) {
  1623. struct drm_i915_gem_exec_object __user *user_exec_list =
  1624. u64_to_user_ptr(args->buffers_ptr);
  1625. /* Copy the new buffer offsets back to the user's exec list. */
  1626. for (i = 0; i < args->buffer_count; i++) {
  1627. exec2_list[i].offset =
  1628. gen8_canonical_addr(exec2_list[i].offset);
  1629. ret = __copy_to_user(&user_exec_list[i].offset,
  1630. &exec2_list[i].offset,
  1631. sizeof(user_exec_list[i].offset));
  1632. if (ret) {
  1633. ret = -EFAULT;
  1634. DRM_DEBUG("failed to copy %d exec entries "
  1635. "back to user (%d)\n",
  1636. args->buffer_count, ret);
  1637. break;
  1638. }
  1639. }
  1640. }
  1641. drm_free_large(exec_list);
  1642. drm_free_large(exec2_list);
  1643. return ret;
  1644. }
  1645. int
  1646. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1647. struct drm_file *file)
  1648. {
  1649. struct drm_i915_gem_execbuffer2 *args = data;
  1650. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1651. int ret;
  1652. if (args->buffer_count < 1 ||
  1653. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1654. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1655. return -EINVAL;
  1656. }
  1657. exec2_list = drm_malloc_gfp(args->buffer_count,
  1658. sizeof(*exec2_list),
  1659. GFP_TEMPORARY);
  1660. if (exec2_list == NULL) {
  1661. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1662. args->buffer_count);
  1663. return -ENOMEM;
  1664. }
  1665. ret = copy_from_user(exec2_list,
  1666. u64_to_user_ptr(args->buffers_ptr),
  1667. sizeof(*exec2_list) * args->buffer_count);
  1668. if (ret != 0) {
  1669. DRM_DEBUG("copy %d exec entries failed %d\n",
  1670. args->buffer_count, ret);
  1671. drm_free_large(exec2_list);
  1672. return -EFAULT;
  1673. }
  1674. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1675. if (!ret) {
  1676. /* Copy the new buffer offsets back to the user's exec list. */
  1677. struct drm_i915_gem_exec_object2 __user *user_exec_list =
  1678. u64_to_user_ptr(args->buffers_ptr);
  1679. int i;
  1680. for (i = 0; i < args->buffer_count; i++) {
  1681. exec2_list[i].offset =
  1682. gen8_canonical_addr(exec2_list[i].offset);
  1683. ret = __copy_to_user(&user_exec_list[i].offset,
  1684. &exec2_list[i].offset,
  1685. sizeof(user_exec_list[i].offset));
  1686. if (ret) {
  1687. ret = -EFAULT;
  1688. DRM_DEBUG("failed to copy %d exec entries "
  1689. "back to user\n",
  1690. args->buffer_count);
  1691. break;
  1692. }
  1693. }
  1694. }
  1695. drm_free_large(exec2_list);
  1696. return ret;
  1697. }