i915_gem_execbuffer.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  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_trace.h"
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #define DBG_USE_CPU_RELOC 0 /* -1 force GTT relocs; 1 force CPU relocs */
  38. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  39. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  40. #define __EXEC_OBJECT_NEEDS_MAP (1<<29)
  41. #define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
  42. #define __EXEC_OBJECT_INTERNAL_FLAGS (0xf<<28) /* all of the above */
  43. #define BATCH_OFFSET_BIAS (256*1024)
  44. struct i915_execbuffer_params {
  45. struct drm_device *dev;
  46. struct drm_file *file;
  47. struct i915_vma *batch;
  48. u32 dispatch_flags;
  49. u32 args_batch_start_offset;
  50. struct intel_engine_cs *engine;
  51. struct i915_gem_context *ctx;
  52. struct drm_i915_gem_request *request;
  53. };
  54. struct eb_vmas {
  55. struct drm_i915_private *i915;
  56. struct list_head vmas;
  57. int and;
  58. union {
  59. struct i915_vma *lut[0];
  60. struct hlist_head buckets[0];
  61. };
  62. };
  63. static struct eb_vmas *
  64. eb_create(struct drm_i915_private *i915,
  65. struct drm_i915_gem_execbuffer2 *args)
  66. {
  67. struct eb_vmas *eb = NULL;
  68. if (args->flags & I915_EXEC_HANDLE_LUT) {
  69. unsigned size = args->buffer_count;
  70. size *= sizeof(struct i915_vma *);
  71. size += sizeof(struct eb_vmas);
  72. eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  73. }
  74. if (eb == NULL) {
  75. unsigned size = args->buffer_count;
  76. unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  77. BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
  78. while (count > 2*size)
  79. count >>= 1;
  80. eb = kzalloc(count*sizeof(struct hlist_head) +
  81. sizeof(struct eb_vmas),
  82. GFP_TEMPORARY);
  83. if (eb == NULL)
  84. return eb;
  85. eb->and = count - 1;
  86. } else
  87. eb->and = -args->buffer_count;
  88. eb->i915 = i915;
  89. INIT_LIST_HEAD(&eb->vmas);
  90. return eb;
  91. }
  92. static void
  93. eb_reset(struct eb_vmas *eb)
  94. {
  95. if (eb->and >= 0)
  96. memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  97. }
  98. static struct i915_vma *
  99. eb_get_batch(struct eb_vmas *eb)
  100. {
  101. struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
  102. /*
  103. * SNA is doing fancy tricks with compressing batch buffers, which leads
  104. * to negative relocation deltas. Usually that works out ok since the
  105. * relocate address is still positive, except when the batch is placed
  106. * very low in the GTT. Ensure this doesn't happen.
  107. *
  108. * Note that actual hangs have only been observed on gen7, but for
  109. * paranoia do it everywhere.
  110. */
  111. if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
  112. vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  113. return vma;
  114. }
  115. static int
  116. eb_lookup_vmas(struct eb_vmas *eb,
  117. struct drm_i915_gem_exec_object2 *exec,
  118. const struct drm_i915_gem_execbuffer2 *args,
  119. struct i915_address_space *vm,
  120. struct drm_file *file)
  121. {
  122. struct drm_i915_gem_object *obj;
  123. struct list_head objects;
  124. int i, ret;
  125. INIT_LIST_HEAD(&objects);
  126. spin_lock(&file->table_lock);
  127. /* Grab a reference to the object and release the lock so we can lookup
  128. * or create the VMA without using GFP_ATOMIC */
  129. for (i = 0; i < args->buffer_count; i++) {
  130. obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
  131. if (obj == NULL) {
  132. spin_unlock(&file->table_lock);
  133. DRM_DEBUG("Invalid object handle %d at index %d\n",
  134. exec[i].handle, i);
  135. ret = -ENOENT;
  136. goto err;
  137. }
  138. if (!list_empty(&obj->obj_exec_link)) {
  139. spin_unlock(&file->table_lock);
  140. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  141. obj, exec[i].handle, i);
  142. ret = -EINVAL;
  143. goto err;
  144. }
  145. i915_gem_object_get(obj);
  146. list_add_tail(&obj->obj_exec_link, &objects);
  147. }
  148. spin_unlock(&file->table_lock);
  149. i = 0;
  150. while (!list_empty(&objects)) {
  151. struct i915_vma *vma;
  152. obj = list_first_entry(&objects,
  153. struct drm_i915_gem_object,
  154. obj_exec_link);
  155. /*
  156. * NOTE: We can leak any vmas created here when something fails
  157. * later on. But that's no issue since vma_unbind can deal with
  158. * vmas which are not actually bound. And since only
  159. * lookup_or_create exists as an interface to get at the vma
  160. * from the (obj, vm) we don't run the risk of creating
  161. * duplicated vmas for the same vm.
  162. */
  163. vma = i915_vma_instance(obj, vm, NULL);
  164. if (unlikely(IS_ERR(vma))) {
  165. DRM_DEBUG("Failed to lookup VMA\n");
  166. ret = PTR_ERR(vma);
  167. goto err;
  168. }
  169. /* Transfer ownership from the objects list to the vmas list. */
  170. list_add_tail(&vma->exec_list, &eb->vmas);
  171. list_del_init(&obj->obj_exec_link);
  172. vma->exec_entry = &exec[i];
  173. if (eb->and < 0) {
  174. eb->lut[i] = vma;
  175. } else {
  176. uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
  177. vma->exec_handle = handle;
  178. hlist_add_head(&vma->exec_node,
  179. &eb->buckets[handle & eb->and]);
  180. }
  181. ++i;
  182. }
  183. return 0;
  184. err:
  185. while (!list_empty(&objects)) {
  186. obj = list_first_entry(&objects,
  187. struct drm_i915_gem_object,
  188. obj_exec_link);
  189. list_del_init(&obj->obj_exec_link);
  190. i915_gem_object_put(obj);
  191. }
  192. /*
  193. * Objects already transfered to the vmas list will be unreferenced by
  194. * eb_destroy.
  195. */
  196. return ret;
  197. }
  198. static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
  199. {
  200. if (eb->and < 0) {
  201. if (handle >= -eb->and)
  202. return NULL;
  203. return eb->lut[handle];
  204. } else {
  205. struct hlist_head *head;
  206. struct i915_vma *vma;
  207. head = &eb->buckets[handle & eb->and];
  208. hlist_for_each_entry(vma, head, exec_node) {
  209. if (vma->exec_handle == handle)
  210. return vma;
  211. }
  212. return NULL;
  213. }
  214. }
  215. static void
  216. i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
  217. {
  218. struct drm_i915_gem_exec_object2 *entry;
  219. if (!drm_mm_node_allocated(&vma->node))
  220. return;
  221. entry = vma->exec_entry;
  222. if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
  223. i915_vma_unpin_fence(vma);
  224. if (entry->flags & __EXEC_OBJECT_HAS_PIN)
  225. __i915_vma_unpin(vma);
  226. entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  227. }
  228. static void eb_destroy(struct eb_vmas *eb)
  229. {
  230. while (!list_empty(&eb->vmas)) {
  231. struct i915_vma *vma;
  232. vma = list_first_entry(&eb->vmas,
  233. struct i915_vma,
  234. exec_list);
  235. list_del_init(&vma->exec_list);
  236. i915_gem_execbuffer_unreserve_vma(vma);
  237. vma->exec_entry = NULL;
  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(to_i915(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. /* Must be a variable in the struct to allow GCC to unroll. */
  288. cache->use_64bit_reloc = HAS_64BIT_RELOC(i915);
  289. cache->node.allocated = false;
  290. }
  291. static inline void *unmask_page(unsigned long p)
  292. {
  293. return (void *)(uintptr_t)(p & PAGE_MASK);
  294. }
  295. static inline unsigned int unmask_flags(unsigned long p)
  296. {
  297. return p & ~PAGE_MASK;
  298. }
  299. #define KMAP 0x4 /* after CLFLUSH_FLAGS */
  300. static void reloc_cache_fini(struct reloc_cache *cache)
  301. {
  302. void *vaddr;
  303. if (!cache->vaddr)
  304. return;
  305. vaddr = unmask_page(cache->vaddr);
  306. if (cache->vaddr & KMAP) {
  307. if (cache->vaddr & CLFLUSH_AFTER)
  308. mb();
  309. kunmap_atomic(vaddr);
  310. i915_gem_obj_finish_shmem_access((struct drm_i915_gem_object *)cache->node.mm);
  311. } else {
  312. wmb();
  313. io_mapping_unmap_atomic((void __iomem *)vaddr);
  314. if (cache->node.allocated) {
  315. struct i915_ggtt *ggtt = &cache->i915->ggtt;
  316. ggtt->base.clear_range(&ggtt->base,
  317. cache->node.start,
  318. cache->node.size);
  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->vaddr) {
  358. io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr));
  359. } else {
  360. struct i915_vma *vma;
  361. int ret;
  362. if (use_cpu_reloc(obj))
  363. return NULL;
  364. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  365. if (ret)
  366. return ERR_PTR(ret);
  367. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  368. PIN_MAPPABLE | PIN_NONBLOCK);
  369. if (IS_ERR(vma)) {
  370. memset(&cache->node, 0, sizeof(cache->node));
  371. ret = drm_mm_insert_node_in_range_generic
  372. (&ggtt->base.mm, &cache->node,
  373. PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
  374. 0, ggtt->mappable_end,
  375. DRM_MM_SEARCH_DEFAULT,
  376. DRM_MM_CREATE_DEFAULT);
  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. ret = i915_gem_request_await_object
  944. (req, obj, obj->base.pending_write_domain);
  945. if (ret)
  946. return ret;
  947. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  948. i915_gem_clflush_object(obj, false);
  949. }
  950. /* Unconditionally flush any chipset caches (for streaming writes). */
  951. i915_gem_chipset_flush(req->engine->i915);
  952. /* Unconditionally invalidate GPU caches and TLBs. */
  953. return req->engine->emit_flush(req, EMIT_INVALIDATE);
  954. }
  955. static bool
  956. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  957. {
  958. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  959. return false;
  960. /* Kernel clipping was a DRI1 misfeature */
  961. if (exec->num_cliprects || exec->cliprects_ptr)
  962. return false;
  963. if (exec->DR4 == 0xffffffff) {
  964. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  965. exec->DR4 = 0;
  966. }
  967. if (exec->DR1 || exec->DR4)
  968. return false;
  969. if ((exec->batch_start_offset | exec->batch_len) & 0x7)
  970. return false;
  971. return true;
  972. }
  973. static int
  974. validate_exec_list(struct drm_device *dev,
  975. struct drm_i915_gem_exec_object2 *exec,
  976. int count)
  977. {
  978. unsigned relocs_total = 0;
  979. unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  980. unsigned invalid_flags;
  981. int i;
  982. /* INTERNAL flags must not overlap with external ones */
  983. BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & ~__EXEC_OBJECT_UNKNOWN_FLAGS);
  984. invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  985. if (USES_FULL_PPGTT(dev))
  986. invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  987. for (i = 0; i < count; i++) {
  988. char __user *ptr = u64_to_user_ptr(exec[i].relocs_ptr);
  989. int length; /* limited by fault_in_pages_readable() */
  990. if (exec[i].flags & invalid_flags)
  991. return -EINVAL;
  992. /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
  993. * any non-page-aligned or non-canonical addresses.
  994. */
  995. if (exec[i].flags & EXEC_OBJECT_PINNED) {
  996. if (exec[i].offset !=
  997. gen8_canonical_addr(exec[i].offset & PAGE_MASK))
  998. return -EINVAL;
  999. /* From drm_mm perspective address space is continuous,
  1000. * so from this point we're always using non-canonical
  1001. * form internally.
  1002. */
  1003. exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
  1004. }
  1005. if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
  1006. return -EINVAL;
  1007. /* pad_to_size was once a reserved field, so sanitize it */
  1008. if (exec[i].flags & EXEC_OBJECT_PAD_TO_SIZE) {
  1009. if (offset_in_page(exec[i].pad_to_size))
  1010. return -EINVAL;
  1011. } else {
  1012. exec[i].pad_to_size = 0;
  1013. }
  1014. /* First check for malicious input causing overflow in
  1015. * the worst case where we need to allocate the entire
  1016. * relocation tree as a single array.
  1017. */
  1018. if (exec[i].relocation_count > relocs_max - relocs_total)
  1019. return -EINVAL;
  1020. relocs_total += exec[i].relocation_count;
  1021. length = exec[i].relocation_count *
  1022. sizeof(struct drm_i915_gem_relocation_entry);
  1023. /*
  1024. * We must check that the entire relocation array is safe
  1025. * to read, but since we may need to update the presumed
  1026. * offsets during execution, check for full write access.
  1027. */
  1028. if (!access_ok(VERIFY_WRITE, ptr, length))
  1029. return -EFAULT;
  1030. if (likely(!i915.prefault_disable)) {
  1031. if (fault_in_pages_readable(ptr, length))
  1032. return -EFAULT;
  1033. }
  1034. }
  1035. return 0;
  1036. }
  1037. static struct i915_gem_context *
  1038. i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
  1039. struct intel_engine_cs *engine, const u32 ctx_id)
  1040. {
  1041. struct i915_gem_context *ctx;
  1042. ctx = i915_gem_context_lookup(file->driver_priv, ctx_id);
  1043. if (IS_ERR(ctx))
  1044. return ctx;
  1045. if (i915_gem_context_is_banned(ctx)) {
  1046. DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
  1047. return ERR_PTR(-EIO);
  1048. }
  1049. return ctx;
  1050. }
  1051. static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  1052. {
  1053. return !(obj->cache_level == I915_CACHE_NONE ||
  1054. obj->cache_level == I915_CACHE_WT);
  1055. }
  1056. void i915_vma_move_to_active(struct i915_vma *vma,
  1057. struct drm_i915_gem_request *req,
  1058. unsigned int flags)
  1059. {
  1060. struct drm_i915_gem_object *obj = vma->obj;
  1061. const unsigned int idx = req->engine->id;
  1062. lockdep_assert_held(&req->i915->drm.struct_mutex);
  1063. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  1064. /* Add a reference if we're newly entering the active list.
  1065. * The order in which we add operations to the retirement queue is
  1066. * vital here: mark_active adds to the start of the callback list,
  1067. * such that subsequent callbacks are called first. Therefore we
  1068. * add the active reference first and queue for it to be dropped
  1069. * *last*.
  1070. */
  1071. if (!i915_vma_is_active(vma))
  1072. obj->active_count++;
  1073. i915_vma_set_active(vma, idx);
  1074. i915_gem_active_set(&vma->last_read[idx], req);
  1075. list_move_tail(&vma->vm_link, &vma->vm->active_list);
  1076. if (flags & EXEC_OBJECT_WRITE) {
  1077. if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
  1078. i915_gem_active_set(&obj->frontbuffer_write, req);
  1079. /* update for the implicit flush after a batch */
  1080. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  1081. if (!obj->cache_dirty && gpu_write_needs_clflush(obj))
  1082. obj->cache_dirty = true;
  1083. }
  1084. if (flags & EXEC_OBJECT_NEEDS_FENCE)
  1085. i915_gem_active_set(&vma->last_fence, req);
  1086. }
  1087. static void eb_export_fence(struct drm_i915_gem_object *obj,
  1088. struct drm_i915_gem_request *req,
  1089. unsigned int flags)
  1090. {
  1091. struct reservation_object *resv = obj->resv;
  1092. /* Ignore errors from failing to allocate the new fence, we can't
  1093. * handle an error right now. Worst case should be missed
  1094. * synchronisation leading to rendering corruption.
  1095. */
  1096. ww_mutex_lock(&resv->lock, NULL);
  1097. if (flags & EXEC_OBJECT_WRITE)
  1098. reservation_object_add_excl_fence(resv, &req->fence);
  1099. else if (reservation_object_reserve_shared(resv) == 0)
  1100. reservation_object_add_shared_fence(resv, &req->fence);
  1101. ww_mutex_unlock(&resv->lock);
  1102. }
  1103. static void
  1104. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  1105. struct drm_i915_gem_request *req)
  1106. {
  1107. struct i915_vma *vma;
  1108. list_for_each_entry(vma, vmas, exec_list) {
  1109. struct drm_i915_gem_object *obj = vma->obj;
  1110. u32 old_read = obj->base.read_domains;
  1111. u32 old_write = obj->base.write_domain;
  1112. obj->base.write_domain = obj->base.pending_write_domain;
  1113. if (obj->base.write_domain)
  1114. vma->exec_entry->flags |= EXEC_OBJECT_WRITE;
  1115. else
  1116. obj->base.pending_read_domains |= obj->base.read_domains;
  1117. obj->base.read_domains = obj->base.pending_read_domains;
  1118. i915_vma_move_to_active(vma, req, vma->exec_entry->flags);
  1119. eb_export_fence(obj, req, vma->exec_entry->flags);
  1120. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  1121. }
  1122. }
  1123. static int
  1124. i915_reset_gen7_sol_offsets(struct drm_i915_gem_request *req)
  1125. {
  1126. struct intel_ring *ring = req->ring;
  1127. int ret, i;
  1128. if (!IS_GEN7(req->i915) || req->engine->id != RCS) {
  1129. DRM_DEBUG("sol reset is gen7/rcs only\n");
  1130. return -EINVAL;
  1131. }
  1132. ret = intel_ring_begin(req, 4 * 3);
  1133. if (ret)
  1134. return ret;
  1135. for (i = 0; i < 4; i++) {
  1136. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1137. intel_ring_emit_reg(ring, GEN7_SO_WRITE_OFFSET(i));
  1138. intel_ring_emit(ring, 0);
  1139. }
  1140. intel_ring_advance(ring);
  1141. return 0;
  1142. }
  1143. static struct i915_vma *
  1144. i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
  1145. struct drm_i915_gem_exec_object2 *shadow_exec_entry,
  1146. struct drm_i915_gem_object *batch_obj,
  1147. struct eb_vmas *eb,
  1148. u32 batch_start_offset,
  1149. u32 batch_len,
  1150. bool is_master)
  1151. {
  1152. struct drm_i915_gem_object *shadow_batch_obj;
  1153. struct i915_vma *vma;
  1154. int ret;
  1155. shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
  1156. PAGE_ALIGN(batch_len));
  1157. if (IS_ERR(shadow_batch_obj))
  1158. return ERR_CAST(shadow_batch_obj);
  1159. ret = intel_engine_cmd_parser(engine,
  1160. batch_obj,
  1161. shadow_batch_obj,
  1162. batch_start_offset,
  1163. batch_len,
  1164. is_master);
  1165. if (ret) {
  1166. if (ret == -EACCES) /* unhandled chained batch */
  1167. vma = NULL;
  1168. else
  1169. vma = ERR_PTR(ret);
  1170. goto out;
  1171. }
  1172. vma = i915_gem_object_ggtt_pin(shadow_batch_obj, NULL, 0, 0, 0);
  1173. if (IS_ERR(vma))
  1174. goto out;
  1175. memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
  1176. vma->exec_entry = shadow_exec_entry;
  1177. vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
  1178. i915_gem_object_get(shadow_batch_obj);
  1179. list_add_tail(&vma->exec_list, &eb->vmas);
  1180. out:
  1181. i915_gem_object_unpin_pages(shadow_batch_obj);
  1182. return vma;
  1183. }
  1184. static int
  1185. execbuf_submit(struct i915_execbuffer_params *params,
  1186. struct drm_i915_gem_execbuffer2 *args,
  1187. struct list_head *vmas)
  1188. {
  1189. struct drm_i915_private *dev_priv = params->request->i915;
  1190. u64 exec_start, exec_len;
  1191. int instp_mode;
  1192. u32 instp_mask;
  1193. int ret;
  1194. ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
  1195. if (ret)
  1196. return ret;
  1197. ret = i915_switch_context(params->request);
  1198. if (ret)
  1199. return ret;
  1200. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  1201. instp_mask = I915_EXEC_CONSTANTS_MASK;
  1202. switch (instp_mode) {
  1203. case I915_EXEC_CONSTANTS_REL_GENERAL:
  1204. case I915_EXEC_CONSTANTS_ABSOLUTE:
  1205. case I915_EXEC_CONSTANTS_REL_SURFACE:
  1206. if (instp_mode != 0 && params->engine->id != RCS) {
  1207. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  1208. return -EINVAL;
  1209. }
  1210. if (instp_mode != dev_priv->relative_constants_mode) {
  1211. if (INTEL_INFO(dev_priv)->gen < 4) {
  1212. DRM_DEBUG("no rel constants on pre-gen4\n");
  1213. return -EINVAL;
  1214. }
  1215. if (INTEL_INFO(dev_priv)->gen > 5 &&
  1216. instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  1217. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  1218. return -EINVAL;
  1219. }
  1220. /* The HW changed the meaning on this bit on gen6 */
  1221. if (INTEL_INFO(dev_priv)->gen >= 6)
  1222. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  1223. }
  1224. break;
  1225. default:
  1226. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  1227. return -EINVAL;
  1228. }
  1229. if (params->engine->id == RCS &&
  1230. instp_mode != dev_priv->relative_constants_mode) {
  1231. struct intel_ring *ring = params->request->ring;
  1232. ret = intel_ring_begin(params->request, 4);
  1233. if (ret)
  1234. return ret;
  1235. intel_ring_emit(ring, MI_NOOP);
  1236. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1237. intel_ring_emit_reg(ring, INSTPM);
  1238. intel_ring_emit(ring, instp_mask << 16 | instp_mode);
  1239. intel_ring_advance(ring);
  1240. dev_priv->relative_constants_mode = instp_mode;
  1241. }
  1242. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1243. ret = i915_reset_gen7_sol_offsets(params->request);
  1244. if (ret)
  1245. return ret;
  1246. }
  1247. exec_len = args->batch_len;
  1248. exec_start = params->batch->node.start +
  1249. params->args_batch_start_offset;
  1250. if (exec_len == 0)
  1251. exec_len = params->batch->size - params->args_batch_start_offset;
  1252. ret = params->engine->emit_bb_start(params->request,
  1253. exec_start, exec_len,
  1254. params->dispatch_flags);
  1255. if (ret)
  1256. return ret;
  1257. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  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. int ret;
  1342. bool need_relocs;
  1343. if (!i915_gem_check_execbuffer(args))
  1344. return -EINVAL;
  1345. ret = validate_exec_list(dev, exec, args->buffer_count);
  1346. if (ret)
  1347. return ret;
  1348. dispatch_flags = 0;
  1349. if (args->flags & I915_EXEC_SECURE) {
  1350. if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN))
  1351. return -EPERM;
  1352. dispatch_flags |= I915_DISPATCH_SECURE;
  1353. }
  1354. if (args->flags & I915_EXEC_IS_PINNED)
  1355. dispatch_flags |= I915_DISPATCH_PINNED;
  1356. engine = eb_select_engine(dev_priv, file, args);
  1357. if (!engine)
  1358. return -EINVAL;
  1359. if (args->buffer_count < 1) {
  1360. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1361. return -EINVAL;
  1362. }
  1363. if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
  1364. if (!HAS_RESOURCE_STREAMER(dev_priv)) {
  1365. DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
  1366. return -EINVAL;
  1367. }
  1368. if (engine->id != RCS) {
  1369. DRM_DEBUG("RS is not available on %s\n",
  1370. engine->name);
  1371. return -EINVAL;
  1372. }
  1373. dispatch_flags |= I915_DISPATCH_RS;
  1374. }
  1375. /* Take a local wakeref for preparing to dispatch the execbuf as
  1376. * we expect to access the hardware fairly frequently in the
  1377. * process. Upon first dispatch, we acquire another prolonged
  1378. * wakeref that we hold until the GPU has been idle for at least
  1379. * 100ms.
  1380. */
  1381. intel_runtime_pm_get(dev_priv);
  1382. ret = i915_mutex_lock_interruptible(dev);
  1383. if (ret)
  1384. goto pre_mutex_err;
  1385. ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
  1386. if (IS_ERR(ctx)) {
  1387. mutex_unlock(&dev->struct_mutex);
  1388. ret = PTR_ERR(ctx);
  1389. goto pre_mutex_err;
  1390. }
  1391. i915_gem_context_get(ctx);
  1392. if (ctx->ppgtt)
  1393. vm = &ctx->ppgtt->base;
  1394. else
  1395. vm = &ggtt->base;
  1396. memset(&params_master, 0x00, sizeof(params_master));
  1397. eb = eb_create(dev_priv, args);
  1398. if (eb == NULL) {
  1399. i915_gem_context_put(ctx);
  1400. mutex_unlock(&dev->struct_mutex);
  1401. ret = -ENOMEM;
  1402. goto pre_mutex_err;
  1403. }
  1404. /* Look up object handles */
  1405. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  1406. if (ret)
  1407. goto err;
  1408. /* take note of the batch buffer before we might reorder the lists */
  1409. params->batch = eb_get_batch(eb);
  1410. /* Move the objects en-masse into the GTT, evicting if necessary. */
  1411. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  1412. ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
  1413. &need_relocs);
  1414. if (ret)
  1415. goto err;
  1416. /* The objects are in their final locations, apply the relocations. */
  1417. if (need_relocs)
  1418. ret = i915_gem_execbuffer_relocate(eb);
  1419. if (ret) {
  1420. if (ret == -EFAULT) {
  1421. ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
  1422. engine,
  1423. eb, exec, ctx);
  1424. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1425. }
  1426. if (ret)
  1427. goto err;
  1428. }
  1429. /* Set the pending read domains for the batch buffer to COMMAND */
  1430. if (params->batch->obj->base.pending_write_domain) {
  1431. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1432. ret = -EINVAL;
  1433. goto err;
  1434. }
  1435. if (args->batch_start_offset > params->batch->size ||
  1436. args->batch_len > params->batch->size - args->batch_start_offset) {
  1437. DRM_DEBUG("Attempting to use out-of-bounds batch\n");
  1438. ret = -EINVAL;
  1439. goto err;
  1440. }
  1441. params->args_batch_start_offset = args->batch_start_offset;
  1442. if (engine->needs_cmd_parser && args->batch_len) {
  1443. struct i915_vma *vma;
  1444. vma = i915_gem_execbuffer_parse(engine, &shadow_exec_entry,
  1445. params->batch->obj,
  1446. eb,
  1447. args->batch_start_offset,
  1448. args->batch_len,
  1449. drm_is_current_master(file));
  1450. if (IS_ERR(vma)) {
  1451. ret = PTR_ERR(vma);
  1452. goto err;
  1453. }
  1454. if (vma) {
  1455. /*
  1456. * Batch parsed and accepted:
  1457. *
  1458. * Set the DISPATCH_SECURE bit to remove the NON_SECURE
  1459. * bit from MI_BATCH_BUFFER_START commands issued in
  1460. * the dispatch_execbuffer implementations. We
  1461. * specifically don't want that set on batches the
  1462. * command parser has accepted.
  1463. */
  1464. dispatch_flags |= I915_DISPATCH_SECURE;
  1465. params->args_batch_start_offset = 0;
  1466. params->batch = vma;
  1467. }
  1468. }
  1469. params->batch->obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1470. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1471. * batch" bit. Hence we need to pin secure batches into the global gtt.
  1472. * hsw should have this fixed, but bdw mucks it up again. */
  1473. if (dispatch_flags & I915_DISPATCH_SECURE) {
  1474. struct drm_i915_gem_object *obj = params->batch->obj;
  1475. struct i915_vma *vma;
  1476. /*
  1477. * So on first glance it looks freaky that we pin the batch here
  1478. * outside of the reservation loop. But:
  1479. * - The batch is already pinned into the relevant ppgtt, so we
  1480. * already have the backing storage fully allocated.
  1481. * - No other BO uses the global gtt (well contexts, but meh),
  1482. * so we don't really have issues with multiple objects not
  1483. * fitting due to fragmentation.
  1484. * So this is actually safe.
  1485. */
  1486. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
  1487. if (IS_ERR(vma)) {
  1488. ret = PTR_ERR(vma);
  1489. goto err;
  1490. }
  1491. params->batch = vma;
  1492. }
  1493. /* Allocate a request for this batch buffer nice and early. */
  1494. params->request = i915_gem_request_alloc(engine, ctx);
  1495. if (IS_ERR(params->request)) {
  1496. ret = PTR_ERR(params->request);
  1497. goto err_batch_unpin;
  1498. }
  1499. /* Whilst this request exists, batch_obj will be on the
  1500. * active_list, and so will hold the active reference. Only when this
  1501. * request is retired will the the batch_obj be moved onto the
  1502. * inactive_list and lose its active reference. Hence we do not need
  1503. * to explicitly hold another reference here.
  1504. */
  1505. params->request->batch = params->batch;
  1506. ret = i915_gem_request_add_to_client(params->request, file);
  1507. if (ret)
  1508. goto err_request;
  1509. /*
  1510. * Save assorted stuff away to pass through to *_submission().
  1511. * NB: This data should be 'persistent' and not local as it will
  1512. * kept around beyond the duration of the IOCTL once the GPU
  1513. * scheduler arrives.
  1514. */
  1515. params->dev = dev;
  1516. params->file = file;
  1517. params->engine = engine;
  1518. params->dispatch_flags = dispatch_flags;
  1519. params->ctx = ctx;
  1520. ret = execbuf_submit(params, args, &eb->vmas);
  1521. err_request:
  1522. __i915_add_request(params->request, ret == 0);
  1523. err_batch_unpin:
  1524. /*
  1525. * FIXME: We crucially rely upon the active tracking for the (ppgtt)
  1526. * batch vma for correctness. For less ugly and less fragility this
  1527. * needs to be adjusted to also track the ggtt batch vma properly as
  1528. * active.
  1529. */
  1530. if (dispatch_flags & I915_DISPATCH_SECURE)
  1531. i915_vma_unpin(params->batch);
  1532. err:
  1533. /* the request owns the ref now */
  1534. i915_gem_context_put(ctx);
  1535. eb_destroy(eb);
  1536. mutex_unlock(&dev->struct_mutex);
  1537. pre_mutex_err:
  1538. /* intel_gpu_busy should also get a ref, so it will free when the device
  1539. * is really idle. */
  1540. intel_runtime_pm_put(dev_priv);
  1541. return ret;
  1542. }
  1543. /*
  1544. * Legacy execbuffer just creates an exec2 list from the original exec object
  1545. * list array and passes it to the real function.
  1546. */
  1547. int
  1548. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1549. struct drm_file *file)
  1550. {
  1551. struct drm_i915_gem_execbuffer *args = data;
  1552. struct drm_i915_gem_execbuffer2 exec2;
  1553. struct drm_i915_gem_exec_object *exec_list = NULL;
  1554. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1555. int ret, i;
  1556. if (args->buffer_count < 1) {
  1557. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1558. return -EINVAL;
  1559. }
  1560. /* Copy in the exec list from userland */
  1561. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1562. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1563. if (exec_list == NULL || exec2_list == NULL) {
  1564. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1565. args->buffer_count);
  1566. drm_free_large(exec_list);
  1567. drm_free_large(exec2_list);
  1568. return -ENOMEM;
  1569. }
  1570. ret = copy_from_user(exec_list,
  1571. u64_to_user_ptr(args->buffers_ptr),
  1572. sizeof(*exec_list) * args->buffer_count);
  1573. if (ret != 0) {
  1574. DRM_DEBUG("copy %d exec entries failed %d\n",
  1575. args->buffer_count, ret);
  1576. drm_free_large(exec_list);
  1577. drm_free_large(exec2_list);
  1578. return -EFAULT;
  1579. }
  1580. for (i = 0; i < args->buffer_count; i++) {
  1581. exec2_list[i].handle = exec_list[i].handle;
  1582. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1583. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1584. exec2_list[i].alignment = exec_list[i].alignment;
  1585. exec2_list[i].offset = exec_list[i].offset;
  1586. if (INTEL_GEN(to_i915(dev)) < 4)
  1587. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1588. else
  1589. exec2_list[i].flags = 0;
  1590. }
  1591. exec2.buffers_ptr = args->buffers_ptr;
  1592. exec2.buffer_count = args->buffer_count;
  1593. exec2.batch_start_offset = args->batch_start_offset;
  1594. exec2.batch_len = args->batch_len;
  1595. exec2.DR1 = args->DR1;
  1596. exec2.DR4 = args->DR4;
  1597. exec2.num_cliprects = args->num_cliprects;
  1598. exec2.cliprects_ptr = args->cliprects_ptr;
  1599. exec2.flags = I915_EXEC_RENDER;
  1600. i915_execbuffer2_set_context_id(exec2, 0);
  1601. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1602. if (!ret) {
  1603. struct drm_i915_gem_exec_object __user *user_exec_list =
  1604. u64_to_user_ptr(args->buffers_ptr);
  1605. /* Copy the new buffer offsets back to the user's exec list. */
  1606. for (i = 0; i < args->buffer_count; i++) {
  1607. exec2_list[i].offset =
  1608. gen8_canonical_addr(exec2_list[i].offset);
  1609. ret = __copy_to_user(&user_exec_list[i].offset,
  1610. &exec2_list[i].offset,
  1611. sizeof(user_exec_list[i].offset));
  1612. if (ret) {
  1613. ret = -EFAULT;
  1614. DRM_DEBUG("failed to copy %d exec entries "
  1615. "back to user (%d)\n",
  1616. args->buffer_count, ret);
  1617. break;
  1618. }
  1619. }
  1620. }
  1621. drm_free_large(exec_list);
  1622. drm_free_large(exec2_list);
  1623. return ret;
  1624. }
  1625. int
  1626. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1627. struct drm_file *file)
  1628. {
  1629. struct drm_i915_gem_execbuffer2 *args = data;
  1630. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1631. int ret;
  1632. if (args->buffer_count < 1 ||
  1633. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1634. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1635. return -EINVAL;
  1636. }
  1637. if (args->rsvd2 != 0) {
  1638. DRM_DEBUG("dirty rvsd2 field\n");
  1639. return -EINVAL;
  1640. }
  1641. exec2_list = drm_malloc_gfp(args->buffer_count,
  1642. sizeof(*exec2_list),
  1643. GFP_TEMPORARY);
  1644. if (exec2_list == NULL) {
  1645. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1646. args->buffer_count);
  1647. return -ENOMEM;
  1648. }
  1649. ret = copy_from_user(exec2_list,
  1650. u64_to_user_ptr(args->buffers_ptr),
  1651. sizeof(*exec2_list) * args->buffer_count);
  1652. if (ret != 0) {
  1653. DRM_DEBUG("copy %d exec entries failed %d\n",
  1654. args->buffer_count, ret);
  1655. drm_free_large(exec2_list);
  1656. return -EFAULT;
  1657. }
  1658. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1659. if (!ret) {
  1660. /* Copy the new buffer offsets back to the user's exec list. */
  1661. struct drm_i915_gem_exec_object2 __user *user_exec_list =
  1662. u64_to_user_ptr(args->buffers_ptr);
  1663. int i;
  1664. for (i = 0; i < args->buffer_count; i++) {
  1665. exec2_list[i].offset =
  1666. gen8_canonical_addr(exec2_list[i].offset);
  1667. ret = __copy_to_user(&user_exec_list[i].offset,
  1668. &exec2_list[i].offset,
  1669. sizeof(user_exec_list[i].offset));
  1670. if (ret) {
  1671. ret = -EFAULT;
  1672. DRM_DEBUG("failed to copy %d exec entries "
  1673. "back to user\n",
  1674. args->buffer_count);
  1675. break;
  1676. }
  1677. }
  1678. }
  1679. drm_free_large(exec2_list);
  1680. return ret;
  1681. }