i915_gem_execbuffer.c 53 KB

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