i915_gem_execbuffer.c 54 KB

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