i915_gem_execbuffer.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. /*
  2. * Copyright © 2008,2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Chris Wilson <chris@chris-wilson.co.uk>
  26. *
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/dma_remapping.h>
  34. #include <linux/uaccess.h>
  35. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  36. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  37. #define __EXEC_OBJECT_NEEDS_MAP (1<<29)
  38. #define __EXEC_OBJECT_NEEDS_BIAS (1<<28)
  39. #define BATCH_OFFSET_BIAS (256*1024)
  40. struct eb_vmas {
  41. struct list_head vmas;
  42. int and;
  43. union {
  44. struct i915_vma *lut[0];
  45. struct hlist_head buckets[0];
  46. };
  47. };
  48. static struct eb_vmas *
  49. eb_create(struct drm_i915_gem_execbuffer2 *args)
  50. {
  51. struct eb_vmas *eb = NULL;
  52. if (args->flags & I915_EXEC_HANDLE_LUT) {
  53. unsigned size = args->buffer_count;
  54. size *= sizeof(struct i915_vma *);
  55. size += sizeof(struct eb_vmas);
  56. eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  57. }
  58. if (eb == NULL) {
  59. unsigned size = args->buffer_count;
  60. unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  61. BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
  62. while (count > 2*size)
  63. count >>= 1;
  64. eb = kzalloc(count*sizeof(struct hlist_head) +
  65. sizeof(struct eb_vmas),
  66. GFP_TEMPORARY);
  67. if (eb == NULL)
  68. return eb;
  69. eb->and = count - 1;
  70. } else
  71. eb->and = -args->buffer_count;
  72. INIT_LIST_HEAD(&eb->vmas);
  73. return eb;
  74. }
  75. static void
  76. eb_reset(struct eb_vmas *eb)
  77. {
  78. if (eb->and >= 0)
  79. memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  80. }
  81. static int
  82. eb_lookup_vmas(struct eb_vmas *eb,
  83. struct drm_i915_gem_exec_object2 *exec,
  84. const struct drm_i915_gem_execbuffer2 *args,
  85. struct i915_address_space *vm,
  86. struct drm_file *file)
  87. {
  88. struct drm_i915_gem_object *obj;
  89. struct list_head objects;
  90. int i, ret;
  91. INIT_LIST_HEAD(&objects);
  92. spin_lock(&file->table_lock);
  93. /* Grab a reference to the object and release the lock so we can lookup
  94. * or create the VMA without using GFP_ATOMIC */
  95. for (i = 0; i < args->buffer_count; i++) {
  96. obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
  97. if (obj == NULL) {
  98. spin_unlock(&file->table_lock);
  99. DRM_DEBUG("Invalid object handle %d at index %d\n",
  100. exec[i].handle, i);
  101. ret = -ENOENT;
  102. goto err;
  103. }
  104. if (!list_empty(&obj->obj_exec_link)) {
  105. spin_unlock(&file->table_lock);
  106. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  107. obj, exec[i].handle, i);
  108. ret = -EINVAL;
  109. goto err;
  110. }
  111. drm_gem_object_reference(&obj->base);
  112. list_add_tail(&obj->obj_exec_link, &objects);
  113. }
  114. spin_unlock(&file->table_lock);
  115. i = 0;
  116. while (!list_empty(&objects)) {
  117. struct i915_vma *vma;
  118. obj = list_first_entry(&objects,
  119. struct drm_i915_gem_object,
  120. obj_exec_link);
  121. /*
  122. * NOTE: We can leak any vmas created here when something fails
  123. * later on. But that's no issue since vma_unbind can deal with
  124. * vmas which are not actually bound. And since only
  125. * lookup_or_create exists as an interface to get at the vma
  126. * from the (obj, vm) we don't run the risk of creating
  127. * duplicated vmas for the same vm.
  128. */
  129. vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
  130. if (IS_ERR(vma)) {
  131. DRM_DEBUG("Failed to lookup VMA\n");
  132. ret = PTR_ERR(vma);
  133. goto err;
  134. }
  135. /* Transfer ownership from the objects list to the vmas list. */
  136. list_add_tail(&vma->exec_list, &eb->vmas);
  137. list_del_init(&obj->obj_exec_link);
  138. vma->exec_entry = &exec[i];
  139. if (eb->and < 0) {
  140. eb->lut[i] = vma;
  141. } else {
  142. uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
  143. vma->exec_handle = handle;
  144. hlist_add_head(&vma->exec_node,
  145. &eb->buckets[handle & eb->and]);
  146. }
  147. ++i;
  148. }
  149. return 0;
  150. err:
  151. while (!list_empty(&objects)) {
  152. obj = list_first_entry(&objects,
  153. struct drm_i915_gem_object,
  154. obj_exec_link);
  155. list_del_init(&obj->obj_exec_link);
  156. drm_gem_object_unreference(&obj->base);
  157. }
  158. /*
  159. * Objects already transfered to the vmas list will be unreferenced by
  160. * eb_destroy.
  161. */
  162. return ret;
  163. }
  164. static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
  165. {
  166. if (eb->and < 0) {
  167. if (handle >= -eb->and)
  168. return NULL;
  169. return eb->lut[handle];
  170. } else {
  171. struct hlist_head *head;
  172. struct i915_vma *vma;
  173. head = &eb->buckets[handle & eb->and];
  174. hlist_for_each_entry(vma, head, exec_node) {
  175. if (vma->exec_handle == handle)
  176. return vma;
  177. }
  178. return NULL;
  179. }
  180. }
  181. static void
  182. i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
  183. {
  184. struct drm_i915_gem_exec_object2 *entry;
  185. struct drm_i915_gem_object *obj = vma->obj;
  186. if (!drm_mm_node_allocated(&vma->node))
  187. return;
  188. entry = vma->exec_entry;
  189. if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
  190. i915_gem_object_unpin_fence(obj);
  191. if (entry->flags & __EXEC_OBJECT_HAS_PIN)
  192. vma->pin_count--;
  193. entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  194. }
  195. static void eb_destroy(struct eb_vmas *eb)
  196. {
  197. while (!list_empty(&eb->vmas)) {
  198. struct i915_vma *vma;
  199. vma = list_first_entry(&eb->vmas,
  200. struct i915_vma,
  201. exec_list);
  202. list_del_init(&vma->exec_list);
  203. i915_gem_execbuffer_unreserve_vma(vma);
  204. drm_gem_object_unreference(&vma->obj->base);
  205. }
  206. kfree(eb);
  207. }
  208. static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
  209. {
  210. return (HAS_LLC(obj->base.dev) ||
  211. obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
  212. obj->cache_level != I915_CACHE_NONE);
  213. }
  214. /* Used to convert any address to canonical form.
  215. * Starting from gen8, some commands (e.g. STATE_BASE_ADDRESS,
  216. * MI_LOAD_REGISTER_MEM and others, see Broadwell PRM Vol2a) require the
  217. * addresses to be in a canonical form:
  218. * "GraphicsAddress[63:48] are ignored by the HW and assumed to be in correct
  219. * canonical form [63:48] == [47]."
  220. */
  221. #define GEN8_HIGH_ADDRESS_BIT 47
  222. static inline uint64_t gen8_canonical_addr(uint64_t address)
  223. {
  224. return sign_extend64(address, GEN8_HIGH_ADDRESS_BIT);
  225. }
  226. static inline uint64_t gen8_noncanonical_addr(uint64_t address)
  227. {
  228. return address & ((1ULL << (GEN8_HIGH_ADDRESS_BIT + 1)) - 1);
  229. }
  230. static inline uint64_t
  231. relocation_target(struct drm_i915_gem_relocation_entry *reloc,
  232. uint64_t target_offset)
  233. {
  234. return gen8_canonical_addr((int)reloc->delta + target_offset);
  235. }
  236. static int
  237. relocate_entry_cpu(struct drm_i915_gem_object *obj,
  238. struct drm_i915_gem_relocation_entry *reloc,
  239. uint64_t target_offset)
  240. {
  241. struct drm_device *dev = obj->base.dev;
  242. uint32_t page_offset = offset_in_page(reloc->offset);
  243. uint64_t delta = relocation_target(reloc, target_offset);
  244. char *vaddr;
  245. int ret;
  246. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  247. if (ret)
  248. return ret;
  249. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
  250. reloc->offset >> PAGE_SHIFT));
  251. *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
  252. if (INTEL_INFO(dev)->gen >= 8) {
  253. page_offset = offset_in_page(page_offset + sizeof(uint32_t));
  254. if (page_offset == 0) {
  255. kunmap_atomic(vaddr);
  256. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
  257. (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
  258. }
  259. *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
  260. }
  261. kunmap_atomic(vaddr);
  262. return 0;
  263. }
  264. static int
  265. relocate_entry_gtt(struct drm_i915_gem_object *obj,
  266. struct drm_i915_gem_relocation_entry *reloc,
  267. uint64_t target_offset)
  268. {
  269. struct drm_device *dev = obj->base.dev;
  270. struct drm_i915_private *dev_priv = dev->dev_private;
  271. uint64_t delta = relocation_target(reloc, target_offset);
  272. uint64_t offset;
  273. void __iomem *reloc_page;
  274. int ret;
  275. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  276. if (ret)
  277. return ret;
  278. ret = i915_gem_object_put_fence(obj);
  279. if (ret)
  280. return ret;
  281. /* Map the page containing the relocation we're going to perform. */
  282. offset = i915_gem_obj_ggtt_offset(obj);
  283. offset += reloc->offset;
  284. reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
  285. offset & PAGE_MASK);
  286. iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
  287. if (INTEL_INFO(dev)->gen >= 8) {
  288. offset += sizeof(uint32_t);
  289. if (offset_in_page(offset) == 0) {
  290. io_mapping_unmap_atomic(reloc_page);
  291. reloc_page =
  292. io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
  293. offset);
  294. }
  295. iowrite32(upper_32_bits(delta),
  296. reloc_page + offset_in_page(offset));
  297. }
  298. io_mapping_unmap_atomic(reloc_page);
  299. return 0;
  300. }
  301. static void
  302. clflush_write32(void *addr, uint32_t value)
  303. {
  304. /* This is not a fast path, so KISS. */
  305. drm_clflush_virt_range(addr, sizeof(uint32_t));
  306. *(uint32_t *)addr = value;
  307. drm_clflush_virt_range(addr, sizeof(uint32_t));
  308. }
  309. static int
  310. relocate_entry_clflush(struct drm_i915_gem_object *obj,
  311. struct drm_i915_gem_relocation_entry *reloc,
  312. uint64_t target_offset)
  313. {
  314. struct drm_device *dev = obj->base.dev;
  315. uint32_t page_offset = offset_in_page(reloc->offset);
  316. uint64_t delta = relocation_target(reloc, target_offset);
  317. char *vaddr;
  318. int ret;
  319. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  320. if (ret)
  321. return ret;
  322. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
  323. reloc->offset >> PAGE_SHIFT));
  324. clflush_write32(vaddr + page_offset, lower_32_bits(delta));
  325. if (INTEL_INFO(dev)->gen >= 8) {
  326. page_offset = offset_in_page(page_offset + sizeof(uint32_t));
  327. if (page_offset == 0) {
  328. kunmap_atomic(vaddr);
  329. vaddr = kmap_atomic(i915_gem_object_get_dirty_page(obj,
  330. (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
  331. }
  332. clflush_write32(vaddr + page_offset, upper_32_bits(delta));
  333. }
  334. kunmap_atomic(vaddr);
  335. return 0;
  336. }
  337. static int
  338. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  339. struct eb_vmas *eb,
  340. struct drm_i915_gem_relocation_entry *reloc)
  341. {
  342. struct drm_device *dev = obj->base.dev;
  343. struct drm_gem_object *target_obj;
  344. struct drm_i915_gem_object *target_i915_obj;
  345. struct i915_vma *target_vma;
  346. uint64_t target_offset;
  347. int ret;
  348. /* we've already hold a reference to all valid objects */
  349. target_vma = eb_get_vma(eb, reloc->target_handle);
  350. if (unlikely(target_vma == NULL))
  351. return -ENOENT;
  352. target_i915_obj = target_vma->obj;
  353. target_obj = &target_vma->obj->base;
  354. target_offset = gen8_canonical_addr(target_vma->node.start);
  355. /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
  356. * pipe_control writes because the gpu doesn't properly redirect them
  357. * through the ppgtt for non_secure batchbuffers. */
  358. if (unlikely(IS_GEN6(dev) &&
  359. reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
  360. ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
  361. PIN_GLOBAL);
  362. if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
  363. return ret;
  364. }
  365. /* Validate that the target is in a valid r/w GPU domain */
  366. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  367. DRM_DEBUG("reloc with multiple write domains: "
  368. "obj %p target %d offset %d "
  369. "read %08x write %08x",
  370. obj, reloc->target_handle,
  371. (int) reloc->offset,
  372. reloc->read_domains,
  373. reloc->write_domain);
  374. return -EINVAL;
  375. }
  376. if (unlikely((reloc->write_domain | reloc->read_domains)
  377. & ~I915_GEM_GPU_DOMAINS)) {
  378. DRM_DEBUG("reloc with read/write non-GPU domains: "
  379. "obj %p target %d offset %d "
  380. "read %08x write %08x",
  381. obj, reloc->target_handle,
  382. (int) reloc->offset,
  383. reloc->read_domains,
  384. reloc->write_domain);
  385. return -EINVAL;
  386. }
  387. target_obj->pending_read_domains |= reloc->read_domains;
  388. target_obj->pending_write_domain |= reloc->write_domain;
  389. /* If the relocation already has the right value in it, no
  390. * more work needs to be done.
  391. */
  392. if (target_offset == reloc->presumed_offset)
  393. return 0;
  394. /* Check that the relocation address is valid... */
  395. if (unlikely(reloc->offset >
  396. obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
  397. DRM_DEBUG("Relocation beyond object bounds: "
  398. "obj %p target %d offset %d size %d.\n",
  399. obj, reloc->target_handle,
  400. (int) reloc->offset,
  401. (int) obj->base.size);
  402. return -EINVAL;
  403. }
  404. if (unlikely(reloc->offset & 3)) {
  405. DRM_DEBUG("Relocation not 4-byte aligned: "
  406. "obj %p target %d offset %d.\n",
  407. obj, reloc->target_handle,
  408. (int) reloc->offset);
  409. return -EINVAL;
  410. }
  411. /* We can't wait for rendering with pagefaults disabled */
  412. if (obj->active && pagefault_disabled())
  413. return -EFAULT;
  414. if (use_cpu_reloc(obj))
  415. ret = relocate_entry_cpu(obj, reloc, target_offset);
  416. else if (obj->map_and_fenceable)
  417. ret = relocate_entry_gtt(obj, reloc, target_offset);
  418. else if (cpu_has_clflush)
  419. ret = relocate_entry_clflush(obj, reloc, target_offset);
  420. else {
  421. WARN_ONCE(1, "Impossible case in relocation handling\n");
  422. ret = -ENODEV;
  423. }
  424. if (ret)
  425. return ret;
  426. /* and update the user's relocation entry */
  427. reloc->presumed_offset = target_offset;
  428. return 0;
  429. }
  430. static int
  431. i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
  432. struct eb_vmas *eb)
  433. {
  434. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  435. struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
  436. struct drm_i915_gem_relocation_entry __user *user_relocs;
  437. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  438. int remain, ret;
  439. user_relocs = to_user_ptr(entry->relocs_ptr);
  440. remain = entry->relocation_count;
  441. while (remain) {
  442. struct drm_i915_gem_relocation_entry *r = stack_reloc;
  443. int count = remain;
  444. if (count > ARRAY_SIZE(stack_reloc))
  445. count = ARRAY_SIZE(stack_reloc);
  446. remain -= count;
  447. if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
  448. return -EFAULT;
  449. do {
  450. u64 offset = r->presumed_offset;
  451. ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
  452. if (ret)
  453. return ret;
  454. if (r->presumed_offset != offset &&
  455. __copy_to_user_inatomic(&user_relocs->presumed_offset,
  456. &r->presumed_offset,
  457. sizeof(r->presumed_offset))) {
  458. return -EFAULT;
  459. }
  460. user_relocs++;
  461. r++;
  462. } while (--count);
  463. }
  464. return 0;
  465. #undef N_RELOC
  466. }
  467. static int
  468. i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
  469. struct eb_vmas *eb,
  470. struct drm_i915_gem_relocation_entry *relocs)
  471. {
  472. const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  473. int i, ret;
  474. for (i = 0; i < entry->relocation_count; i++) {
  475. ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
  476. if (ret)
  477. return ret;
  478. }
  479. return 0;
  480. }
  481. static int
  482. i915_gem_execbuffer_relocate(struct eb_vmas *eb)
  483. {
  484. struct i915_vma *vma;
  485. int ret = 0;
  486. /* This is the fast path and we cannot handle a pagefault whilst
  487. * holding the struct mutex lest the user pass in the relocations
  488. * contained within a mmaped bo. For in such a case we, the page
  489. * fault handler would call i915_gem_fault() and we would try to
  490. * acquire the struct mutex again. Obviously this is bad and so
  491. * lockdep complains vehemently.
  492. */
  493. pagefault_disable();
  494. list_for_each_entry(vma, &eb->vmas, exec_list) {
  495. ret = i915_gem_execbuffer_relocate_vma(vma, eb);
  496. if (ret)
  497. break;
  498. }
  499. pagefault_enable();
  500. return ret;
  501. }
  502. static bool only_mappable_for_reloc(unsigned int flags)
  503. {
  504. return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
  505. __EXEC_OBJECT_NEEDS_MAP;
  506. }
  507. static int
  508. i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
  509. struct intel_engine_cs *engine,
  510. bool *need_reloc)
  511. {
  512. struct drm_i915_gem_object *obj = vma->obj;
  513. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  514. uint64_t flags;
  515. int ret;
  516. flags = PIN_USER;
  517. if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
  518. flags |= PIN_GLOBAL;
  519. if (!drm_mm_node_allocated(&vma->node)) {
  520. /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
  521. * limit address to the first 4GBs for unflagged objects.
  522. */
  523. if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
  524. flags |= PIN_ZONE_4G;
  525. if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
  526. flags |= PIN_GLOBAL | PIN_MAPPABLE;
  527. if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
  528. flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
  529. if (entry->flags & EXEC_OBJECT_PINNED)
  530. flags |= entry->offset | PIN_OFFSET_FIXED;
  531. if ((flags & PIN_MAPPABLE) == 0)
  532. flags |= PIN_HIGH;
  533. }
  534. ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
  535. if ((ret == -ENOSPC || ret == -E2BIG) &&
  536. only_mappable_for_reloc(entry->flags))
  537. ret = i915_gem_object_pin(obj, vma->vm,
  538. entry->alignment,
  539. flags & ~PIN_MAPPABLE);
  540. if (ret)
  541. return ret;
  542. entry->flags |= __EXEC_OBJECT_HAS_PIN;
  543. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  544. ret = i915_gem_object_get_fence(obj);
  545. if (ret)
  546. return ret;
  547. if (i915_gem_object_pin_fence(obj))
  548. entry->flags |= __EXEC_OBJECT_HAS_FENCE;
  549. }
  550. if (entry->offset != vma->node.start) {
  551. entry->offset = vma->node.start;
  552. *need_reloc = true;
  553. }
  554. if (entry->flags & EXEC_OBJECT_WRITE) {
  555. obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
  556. obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
  557. }
  558. return 0;
  559. }
  560. static bool
  561. need_reloc_mappable(struct i915_vma *vma)
  562. {
  563. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  564. if (entry->relocation_count == 0)
  565. return false;
  566. if (!vma->is_ggtt)
  567. return false;
  568. /* See also use_cpu_reloc() */
  569. if (HAS_LLC(vma->obj->base.dev))
  570. return false;
  571. if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  572. return false;
  573. return true;
  574. }
  575. static bool
  576. eb_vma_misplaced(struct i915_vma *vma)
  577. {
  578. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  579. struct drm_i915_gem_object *obj = vma->obj;
  580. WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP && !vma->is_ggtt);
  581. if (entry->alignment &&
  582. vma->node.start & (entry->alignment - 1))
  583. return true;
  584. if (entry->flags & EXEC_OBJECT_PINNED &&
  585. vma->node.start != entry->offset)
  586. return true;
  587. if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
  588. vma->node.start < BATCH_OFFSET_BIAS)
  589. return true;
  590. /* avoid costly ping-pong once a batch bo ended up non-mappable */
  591. if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
  592. return !only_mappable_for_reloc(entry->flags);
  593. if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
  594. (vma->node.start + vma->node.size - 1) >> 32)
  595. return true;
  596. return false;
  597. }
  598. static int
  599. i915_gem_execbuffer_reserve(struct intel_engine_cs *engine,
  600. struct list_head *vmas,
  601. struct intel_context *ctx,
  602. bool *need_relocs)
  603. {
  604. struct drm_i915_gem_object *obj;
  605. struct i915_vma *vma;
  606. struct i915_address_space *vm;
  607. struct list_head ordered_vmas;
  608. struct list_head pinned_vmas;
  609. bool has_fenced_gpu_access = INTEL_INFO(engine->dev)->gen < 4;
  610. int retry;
  611. i915_gem_retire_requests_ring(engine);
  612. vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
  613. INIT_LIST_HEAD(&ordered_vmas);
  614. INIT_LIST_HEAD(&pinned_vmas);
  615. while (!list_empty(vmas)) {
  616. struct drm_i915_gem_exec_object2 *entry;
  617. bool need_fence, need_mappable;
  618. vma = list_first_entry(vmas, struct i915_vma, exec_list);
  619. obj = vma->obj;
  620. entry = vma->exec_entry;
  621. if (ctx->flags & CONTEXT_NO_ZEROMAP)
  622. entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  623. if (!has_fenced_gpu_access)
  624. entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
  625. need_fence =
  626. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  627. obj->tiling_mode != I915_TILING_NONE;
  628. need_mappable = need_fence || need_reloc_mappable(vma);
  629. if (entry->flags & EXEC_OBJECT_PINNED)
  630. list_move_tail(&vma->exec_list, &pinned_vmas);
  631. else if (need_mappable) {
  632. entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
  633. list_move(&vma->exec_list, &ordered_vmas);
  634. } else
  635. list_move_tail(&vma->exec_list, &ordered_vmas);
  636. obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  637. obj->base.pending_write_domain = 0;
  638. }
  639. list_splice(&ordered_vmas, vmas);
  640. list_splice(&pinned_vmas, vmas);
  641. /* Attempt to pin all of the buffers into the GTT.
  642. * This is done in 3 phases:
  643. *
  644. * 1a. Unbind all objects that do not match the GTT constraints for
  645. * the execbuffer (fenceable, mappable, alignment etc).
  646. * 1b. Increment pin count for already bound objects.
  647. * 2. Bind new objects.
  648. * 3. Decrement pin count.
  649. *
  650. * This avoid unnecessary unbinding of later objects in order to make
  651. * room for the earlier objects *unless* we need to defragment.
  652. */
  653. retry = 0;
  654. do {
  655. int ret = 0;
  656. /* Unbind any ill-fitting objects or pin. */
  657. list_for_each_entry(vma, vmas, exec_list) {
  658. if (!drm_mm_node_allocated(&vma->node))
  659. continue;
  660. if (eb_vma_misplaced(vma))
  661. ret = i915_vma_unbind(vma);
  662. else
  663. ret = i915_gem_execbuffer_reserve_vma(vma,
  664. engine,
  665. need_relocs);
  666. if (ret)
  667. goto err;
  668. }
  669. /* Bind fresh objects */
  670. list_for_each_entry(vma, vmas, exec_list) {
  671. if (drm_mm_node_allocated(&vma->node))
  672. continue;
  673. ret = i915_gem_execbuffer_reserve_vma(vma, engine,
  674. need_relocs);
  675. if (ret)
  676. goto err;
  677. }
  678. err:
  679. if (ret != -ENOSPC || retry++)
  680. return ret;
  681. /* Decrement pin count for bound objects */
  682. list_for_each_entry(vma, vmas, exec_list)
  683. i915_gem_execbuffer_unreserve_vma(vma);
  684. ret = i915_gem_evict_vm(vm, true);
  685. if (ret)
  686. return ret;
  687. } while (1);
  688. }
  689. static int
  690. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  691. struct drm_i915_gem_execbuffer2 *args,
  692. struct drm_file *file,
  693. struct intel_engine_cs *engine,
  694. struct eb_vmas *eb,
  695. struct drm_i915_gem_exec_object2 *exec,
  696. struct intel_context *ctx)
  697. {
  698. struct drm_i915_gem_relocation_entry *reloc;
  699. struct i915_address_space *vm;
  700. struct i915_vma *vma;
  701. bool need_relocs;
  702. int *reloc_offset;
  703. int i, total, ret;
  704. unsigned count = args->buffer_count;
  705. vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
  706. /* We may process another execbuffer during the unlock... */
  707. while (!list_empty(&eb->vmas)) {
  708. vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
  709. list_del_init(&vma->exec_list);
  710. i915_gem_execbuffer_unreserve_vma(vma);
  711. drm_gem_object_unreference(&vma->obj->base);
  712. }
  713. mutex_unlock(&dev->struct_mutex);
  714. total = 0;
  715. for (i = 0; i < count; i++)
  716. total += exec[i].relocation_count;
  717. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  718. reloc = drm_malloc_ab(total, sizeof(*reloc));
  719. if (reloc == NULL || reloc_offset == NULL) {
  720. drm_free_large(reloc);
  721. drm_free_large(reloc_offset);
  722. mutex_lock(&dev->struct_mutex);
  723. return -ENOMEM;
  724. }
  725. total = 0;
  726. for (i = 0; i < count; i++) {
  727. struct drm_i915_gem_relocation_entry __user *user_relocs;
  728. u64 invalid_offset = (u64)-1;
  729. int j;
  730. user_relocs = to_user_ptr(exec[i].relocs_ptr);
  731. if (copy_from_user(reloc+total, user_relocs,
  732. exec[i].relocation_count * sizeof(*reloc))) {
  733. ret = -EFAULT;
  734. mutex_lock(&dev->struct_mutex);
  735. goto err;
  736. }
  737. /* As we do not update the known relocation offsets after
  738. * relocating (due to the complexities in lock handling),
  739. * we need to mark them as invalid now so that we force the
  740. * relocation processing next time. Just in case the target
  741. * object is evicted and then rebound into its old
  742. * presumed_offset before the next execbuffer - if that
  743. * happened we would make the mistake of assuming that the
  744. * relocations were valid.
  745. */
  746. for (j = 0; j < exec[i].relocation_count; j++) {
  747. if (__copy_to_user(&user_relocs[j].presumed_offset,
  748. &invalid_offset,
  749. sizeof(invalid_offset))) {
  750. ret = -EFAULT;
  751. mutex_lock(&dev->struct_mutex);
  752. goto err;
  753. }
  754. }
  755. reloc_offset[i] = total;
  756. total += exec[i].relocation_count;
  757. }
  758. ret = i915_mutex_lock_interruptible(dev);
  759. if (ret) {
  760. mutex_lock(&dev->struct_mutex);
  761. goto err;
  762. }
  763. /* reacquire the objects */
  764. eb_reset(eb);
  765. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  766. if (ret)
  767. goto err;
  768. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  769. ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
  770. &need_relocs);
  771. if (ret)
  772. goto err;
  773. list_for_each_entry(vma, &eb->vmas, exec_list) {
  774. int offset = vma->exec_entry - exec;
  775. ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
  776. reloc + reloc_offset[offset]);
  777. if (ret)
  778. goto err;
  779. }
  780. /* Leave the user relocations as are, this is the painfully slow path,
  781. * and we want to avoid the complication of dropping the lock whilst
  782. * having buffers reserved in the aperture and so causing spurious
  783. * ENOSPC for random operations.
  784. */
  785. err:
  786. drm_free_large(reloc);
  787. drm_free_large(reloc_offset);
  788. return ret;
  789. }
  790. static int
  791. i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
  792. struct list_head *vmas)
  793. {
  794. const unsigned other_rings = ~intel_engine_flag(req->engine);
  795. struct i915_vma *vma;
  796. uint32_t flush_domains = 0;
  797. bool flush_chipset = false;
  798. int ret;
  799. list_for_each_entry(vma, vmas, exec_list) {
  800. struct drm_i915_gem_object *obj = vma->obj;
  801. if (obj->active & other_rings) {
  802. ret = i915_gem_object_sync(obj, req->engine, &req);
  803. if (ret)
  804. return ret;
  805. }
  806. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  807. flush_chipset |= i915_gem_clflush_object(obj, false);
  808. flush_domains |= obj->base.write_domain;
  809. }
  810. if (flush_chipset)
  811. i915_gem_chipset_flush(req->engine->dev);
  812. if (flush_domains & I915_GEM_DOMAIN_GTT)
  813. wmb();
  814. /* Unconditionally invalidate gpu caches and ensure that we do flush
  815. * any residual writes from the previous batch.
  816. */
  817. return intel_ring_invalidate_all_caches(req);
  818. }
  819. static bool
  820. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  821. {
  822. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  823. return false;
  824. /* Kernel clipping was a DRI1 misfeature */
  825. if (exec->num_cliprects || exec->cliprects_ptr)
  826. return false;
  827. if (exec->DR4 == 0xffffffff) {
  828. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  829. exec->DR4 = 0;
  830. }
  831. if (exec->DR1 || exec->DR4)
  832. return false;
  833. if ((exec->batch_start_offset | exec->batch_len) & 0x7)
  834. return false;
  835. return true;
  836. }
  837. static int
  838. validate_exec_list(struct drm_device *dev,
  839. struct drm_i915_gem_exec_object2 *exec,
  840. int count)
  841. {
  842. unsigned relocs_total = 0;
  843. unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  844. unsigned invalid_flags;
  845. int i;
  846. invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  847. if (USES_FULL_PPGTT(dev))
  848. invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  849. for (i = 0; i < count; i++) {
  850. char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
  851. int length; /* limited by fault_in_pages_readable() */
  852. if (exec[i].flags & invalid_flags)
  853. return -EINVAL;
  854. /* Offset can be used as input (EXEC_OBJECT_PINNED), reject
  855. * any non-page-aligned or non-canonical addresses.
  856. */
  857. if (exec[i].flags & EXEC_OBJECT_PINNED) {
  858. if (exec[i].offset !=
  859. gen8_canonical_addr(exec[i].offset & PAGE_MASK))
  860. return -EINVAL;
  861. /* From drm_mm perspective address space is continuous,
  862. * so from this point we're always using non-canonical
  863. * form internally.
  864. */
  865. exec[i].offset = gen8_noncanonical_addr(exec[i].offset);
  866. }
  867. if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
  868. return -EINVAL;
  869. /* First check for malicious input causing overflow in
  870. * the worst case where we need to allocate the entire
  871. * relocation tree as a single array.
  872. */
  873. if (exec[i].relocation_count > relocs_max - relocs_total)
  874. return -EINVAL;
  875. relocs_total += exec[i].relocation_count;
  876. length = exec[i].relocation_count *
  877. sizeof(struct drm_i915_gem_relocation_entry);
  878. /*
  879. * We must check that the entire relocation array is safe
  880. * to read, but since we may need to update the presumed
  881. * offsets during execution, check for full write access.
  882. */
  883. if (!access_ok(VERIFY_WRITE, ptr, length))
  884. return -EFAULT;
  885. if (likely(!i915.prefault_disable)) {
  886. if (fault_in_multipages_readable(ptr, length))
  887. return -EFAULT;
  888. }
  889. }
  890. return 0;
  891. }
  892. static struct intel_context *
  893. i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
  894. struct intel_engine_cs *engine, const u32 ctx_id)
  895. {
  896. struct intel_context *ctx = NULL;
  897. struct i915_ctx_hang_stats *hs;
  898. if (engine->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
  899. return ERR_PTR(-EINVAL);
  900. ctx = i915_gem_context_get(file->driver_priv, ctx_id);
  901. if (IS_ERR(ctx))
  902. return ctx;
  903. hs = &ctx->hang_stats;
  904. if (hs->banned) {
  905. DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
  906. return ERR_PTR(-EIO);
  907. }
  908. if (i915.enable_execlists && !ctx->engine[engine->id].state) {
  909. int ret = intel_lr_context_deferred_alloc(ctx, engine);
  910. if (ret) {
  911. DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
  912. return ERR_PTR(ret);
  913. }
  914. }
  915. return ctx;
  916. }
  917. void
  918. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  919. struct drm_i915_gem_request *req)
  920. {
  921. struct intel_engine_cs *engine = i915_gem_request_get_engine(req);
  922. struct i915_vma *vma;
  923. list_for_each_entry(vma, vmas, exec_list) {
  924. struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  925. struct drm_i915_gem_object *obj = vma->obj;
  926. u32 old_read = obj->base.read_domains;
  927. u32 old_write = obj->base.write_domain;
  928. obj->dirty = 1; /* be paranoid */
  929. obj->base.write_domain = obj->base.pending_write_domain;
  930. if (obj->base.write_domain == 0)
  931. obj->base.pending_read_domains |= obj->base.read_domains;
  932. obj->base.read_domains = obj->base.pending_read_domains;
  933. i915_vma_move_to_active(vma, req);
  934. if (obj->base.write_domain) {
  935. i915_gem_request_assign(&obj->last_write_req, req);
  936. intel_fb_obj_invalidate(obj, ORIGIN_CS);
  937. /* update for the implicit flush after a batch */
  938. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  939. }
  940. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  941. i915_gem_request_assign(&obj->last_fenced_req, req);
  942. if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
  943. struct drm_i915_private *dev_priv = to_i915(engine->dev);
  944. list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
  945. &dev_priv->mm.fence_list);
  946. }
  947. }
  948. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  949. }
  950. }
  951. void
  952. i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params *params)
  953. {
  954. /* Unconditionally force add_request to emit a full flush. */
  955. params->engine->gpu_caches_dirty = true;
  956. /* Add a breadcrumb for the completion of the batch buffer */
  957. __i915_add_request(params->request, params->batch_obj, true);
  958. }
  959. static int
  960. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  961. struct drm_i915_gem_request *req)
  962. {
  963. struct intel_engine_cs *engine = req->engine;
  964. struct drm_i915_private *dev_priv = dev->dev_private;
  965. int ret, i;
  966. if (!IS_GEN7(dev) || engine != &dev_priv->engine[RCS]) {
  967. DRM_DEBUG("sol reset is gen7/rcs only\n");
  968. return -EINVAL;
  969. }
  970. ret = intel_ring_begin(req, 4 * 3);
  971. if (ret)
  972. return ret;
  973. for (i = 0; i < 4; i++) {
  974. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  975. intel_ring_emit_reg(engine, GEN7_SO_WRITE_OFFSET(i));
  976. intel_ring_emit(engine, 0);
  977. }
  978. intel_ring_advance(engine);
  979. return 0;
  980. }
  981. static struct drm_i915_gem_object*
  982. i915_gem_execbuffer_parse(struct intel_engine_cs *engine,
  983. struct drm_i915_gem_exec_object2 *shadow_exec_entry,
  984. struct eb_vmas *eb,
  985. struct drm_i915_gem_object *batch_obj,
  986. u32 batch_start_offset,
  987. u32 batch_len,
  988. bool is_master)
  989. {
  990. struct drm_i915_gem_object *shadow_batch_obj;
  991. struct i915_vma *vma;
  992. int ret;
  993. shadow_batch_obj = i915_gem_batch_pool_get(&engine->batch_pool,
  994. PAGE_ALIGN(batch_len));
  995. if (IS_ERR(shadow_batch_obj))
  996. return shadow_batch_obj;
  997. ret = i915_parse_cmds(engine,
  998. batch_obj,
  999. shadow_batch_obj,
  1000. batch_start_offset,
  1001. batch_len,
  1002. is_master);
  1003. if (ret)
  1004. goto err;
  1005. ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
  1006. if (ret)
  1007. goto err;
  1008. i915_gem_object_unpin_pages(shadow_batch_obj);
  1009. memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
  1010. vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
  1011. vma->exec_entry = shadow_exec_entry;
  1012. vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
  1013. drm_gem_object_reference(&shadow_batch_obj->base);
  1014. list_add_tail(&vma->exec_list, &eb->vmas);
  1015. shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  1016. return shadow_batch_obj;
  1017. err:
  1018. i915_gem_object_unpin_pages(shadow_batch_obj);
  1019. if (ret == -EACCES) /* unhandled chained batch */
  1020. return batch_obj;
  1021. else
  1022. return ERR_PTR(ret);
  1023. }
  1024. int
  1025. i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
  1026. struct drm_i915_gem_execbuffer2 *args,
  1027. struct list_head *vmas)
  1028. {
  1029. struct drm_device *dev = params->dev;
  1030. struct intel_engine_cs *engine = params->engine;
  1031. struct drm_i915_private *dev_priv = dev->dev_private;
  1032. u64 exec_start, exec_len;
  1033. int instp_mode;
  1034. u32 instp_mask;
  1035. int ret;
  1036. ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
  1037. if (ret)
  1038. return ret;
  1039. ret = i915_switch_context(params->request);
  1040. if (ret)
  1041. return ret;
  1042. WARN(params->ctx->ppgtt && params->ctx->ppgtt->pd_dirty_rings & (1<<engine->id),
  1043. "%s didn't clear reload\n", engine->name);
  1044. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  1045. instp_mask = I915_EXEC_CONSTANTS_MASK;
  1046. switch (instp_mode) {
  1047. case I915_EXEC_CONSTANTS_REL_GENERAL:
  1048. case I915_EXEC_CONSTANTS_ABSOLUTE:
  1049. case I915_EXEC_CONSTANTS_REL_SURFACE:
  1050. if (instp_mode != 0 && engine != &dev_priv->engine[RCS]) {
  1051. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  1052. return -EINVAL;
  1053. }
  1054. if (instp_mode != dev_priv->relative_constants_mode) {
  1055. if (INTEL_INFO(dev)->gen < 4) {
  1056. DRM_DEBUG("no rel constants on pre-gen4\n");
  1057. return -EINVAL;
  1058. }
  1059. if (INTEL_INFO(dev)->gen > 5 &&
  1060. instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  1061. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  1062. return -EINVAL;
  1063. }
  1064. /* The HW changed the meaning on this bit on gen6 */
  1065. if (INTEL_INFO(dev)->gen >= 6)
  1066. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  1067. }
  1068. break;
  1069. default:
  1070. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  1071. return -EINVAL;
  1072. }
  1073. if (engine == &dev_priv->engine[RCS] &&
  1074. instp_mode != dev_priv->relative_constants_mode) {
  1075. ret = intel_ring_begin(params->request, 4);
  1076. if (ret)
  1077. return ret;
  1078. intel_ring_emit(engine, MI_NOOP);
  1079. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  1080. intel_ring_emit_reg(engine, INSTPM);
  1081. intel_ring_emit(engine, instp_mask << 16 | instp_mode);
  1082. intel_ring_advance(engine);
  1083. dev_priv->relative_constants_mode = instp_mode;
  1084. }
  1085. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1086. ret = i915_reset_gen7_sol_offsets(dev, params->request);
  1087. if (ret)
  1088. return ret;
  1089. }
  1090. exec_len = args->batch_len;
  1091. exec_start = params->batch_obj_vm_offset +
  1092. params->args_batch_start_offset;
  1093. if (exec_len == 0)
  1094. exec_len = params->batch_obj->base.size;
  1095. ret = engine->dispatch_execbuffer(params->request,
  1096. exec_start, exec_len,
  1097. params->dispatch_flags);
  1098. if (ret)
  1099. return ret;
  1100. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  1101. i915_gem_execbuffer_move_to_active(vmas, params->request);
  1102. i915_gem_execbuffer_retire_commands(params);
  1103. return 0;
  1104. }
  1105. /**
  1106. * Find one BSD ring to dispatch the corresponding BSD command.
  1107. * The ring index is returned.
  1108. */
  1109. static unsigned int
  1110. gen8_dispatch_bsd_ring(struct drm_i915_private *dev_priv, struct drm_file *file)
  1111. {
  1112. struct drm_i915_file_private *file_priv = file->driver_priv;
  1113. /* Check whether the file_priv has already selected one ring. */
  1114. if ((int)file_priv->bsd_ring < 0) {
  1115. /* If not, use the ping-pong mechanism to select one. */
  1116. mutex_lock(&dev_priv->dev->struct_mutex);
  1117. file_priv->bsd_ring = dev_priv->mm.bsd_ring_dispatch_index;
  1118. dev_priv->mm.bsd_ring_dispatch_index ^= 1;
  1119. mutex_unlock(&dev_priv->dev->struct_mutex);
  1120. }
  1121. return file_priv->bsd_ring;
  1122. }
  1123. static struct drm_i915_gem_object *
  1124. eb_get_batch(struct eb_vmas *eb)
  1125. {
  1126. struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
  1127. /*
  1128. * SNA is doing fancy tricks with compressing batch buffers, which leads
  1129. * to negative relocation deltas. Usually that works out ok since the
  1130. * relocate address is still positive, except when the batch is placed
  1131. * very low in the GTT. Ensure this doesn't happen.
  1132. *
  1133. * Note that actual hangs have only been observed on gen7, but for
  1134. * paranoia do it everywhere.
  1135. */
  1136. if ((vma->exec_entry->flags & EXEC_OBJECT_PINNED) == 0)
  1137. vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  1138. return vma->obj;
  1139. }
  1140. #define I915_USER_RINGS (4)
  1141. static const enum intel_engine_id user_ring_map[I915_USER_RINGS + 1] = {
  1142. [I915_EXEC_DEFAULT] = RCS,
  1143. [I915_EXEC_RENDER] = RCS,
  1144. [I915_EXEC_BLT] = BCS,
  1145. [I915_EXEC_BSD] = VCS,
  1146. [I915_EXEC_VEBOX] = VECS
  1147. };
  1148. static int
  1149. eb_select_ring(struct drm_i915_private *dev_priv,
  1150. struct drm_file *file,
  1151. struct drm_i915_gem_execbuffer2 *args,
  1152. struct intel_engine_cs **ring)
  1153. {
  1154. unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK;
  1155. if (user_ring_id > I915_USER_RINGS) {
  1156. DRM_DEBUG("execbuf with unknown ring: %u\n", user_ring_id);
  1157. return -EINVAL;
  1158. }
  1159. if ((user_ring_id != I915_EXEC_BSD) &&
  1160. ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
  1161. DRM_DEBUG("execbuf with non bsd ring but with invalid "
  1162. "bsd dispatch flags: %d\n", (int)(args->flags));
  1163. return -EINVAL;
  1164. }
  1165. if (user_ring_id == I915_EXEC_BSD && HAS_BSD2(dev_priv)) {
  1166. unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK;
  1167. if (bsd_idx == I915_EXEC_BSD_DEFAULT) {
  1168. bsd_idx = gen8_dispatch_bsd_ring(dev_priv, file);
  1169. } else if (bsd_idx >= I915_EXEC_BSD_RING1 &&
  1170. bsd_idx <= I915_EXEC_BSD_RING2) {
  1171. bsd_idx >>= I915_EXEC_BSD_SHIFT;
  1172. bsd_idx--;
  1173. } else {
  1174. DRM_DEBUG("execbuf with unknown bsd ring: %u\n",
  1175. bsd_idx);
  1176. return -EINVAL;
  1177. }
  1178. *ring = &dev_priv->engine[_VCS(bsd_idx)];
  1179. } else {
  1180. *ring = &dev_priv->engine[user_ring_map[user_ring_id]];
  1181. }
  1182. if (!intel_engine_initialized(*ring)) {
  1183. DRM_DEBUG("execbuf with invalid ring: %u\n", user_ring_id);
  1184. return -EINVAL;
  1185. }
  1186. return 0;
  1187. }
  1188. static int
  1189. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  1190. struct drm_file *file,
  1191. struct drm_i915_gem_execbuffer2 *args,
  1192. struct drm_i915_gem_exec_object2 *exec)
  1193. {
  1194. struct drm_i915_private *dev_priv = dev->dev_private;
  1195. struct drm_i915_gem_request *req = NULL;
  1196. struct eb_vmas *eb;
  1197. struct drm_i915_gem_object *batch_obj;
  1198. struct drm_i915_gem_exec_object2 shadow_exec_entry;
  1199. struct intel_engine_cs *engine;
  1200. struct intel_context *ctx;
  1201. struct i915_address_space *vm;
  1202. struct i915_execbuffer_params params_master; /* XXX: will be removed later */
  1203. struct i915_execbuffer_params *params = &params_master;
  1204. const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  1205. u32 dispatch_flags;
  1206. int ret;
  1207. bool need_relocs;
  1208. if (!i915_gem_check_execbuffer(args))
  1209. return -EINVAL;
  1210. ret = validate_exec_list(dev, exec, args->buffer_count);
  1211. if (ret)
  1212. return ret;
  1213. dispatch_flags = 0;
  1214. if (args->flags & I915_EXEC_SECURE) {
  1215. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  1216. return -EPERM;
  1217. dispatch_flags |= I915_DISPATCH_SECURE;
  1218. }
  1219. if (args->flags & I915_EXEC_IS_PINNED)
  1220. dispatch_flags |= I915_DISPATCH_PINNED;
  1221. ret = eb_select_ring(dev_priv, file, args, &engine);
  1222. if (ret)
  1223. return ret;
  1224. if (args->buffer_count < 1) {
  1225. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1226. return -EINVAL;
  1227. }
  1228. if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
  1229. if (!HAS_RESOURCE_STREAMER(dev)) {
  1230. DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
  1231. return -EINVAL;
  1232. }
  1233. if (engine->id != RCS) {
  1234. DRM_DEBUG("RS is not available on %s\n",
  1235. engine->name);
  1236. return -EINVAL;
  1237. }
  1238. dispatch_flags |= I915_DISPATCH_RS;
  1239. }
  1240. intel_runtime_pm_get(dev_priv);
  1241. ret = i915_mutex_lock_interruptible(dev);
  1242. if (ret)
  1243. goto pre_mutex_err;
  1244. ctx = i915_gem_validate_context(dev, file, engine, ctx_id);
  1245. if (IS_ERR(ctx)) {
  1246. mutex_unlock(&dev->struct_mutex);
  1247. ret = PTR_ERR(ctx);
  1248. goto pre_mutex_err;
  1249. }
  1250. i915_gem_context_reference(ctx);
  1251. if (ctx->ppgtt)
  1252. vm = &ctx->ppgtt->base;
  1253. else
  1254. vm = &dev_priv->gtt.base;
  1255. memset(&params_master, 0x00, sizeof(params_master));
  1256. eb = eb_create(args);
  1257. if (eb == NULL) {
  1258. i915_gem_context_unreference(ctx);
  1259. mutex_unlock(&dev->struct_mutex);
  1260. ret = -ENOMEM;
  1261. goto pre_mutex_err;
  1262. }
  1263. /* Look up object handles */
  1264. ret = eb_lookup_vmas(eb, exec, args, vm, file);
  1265. if (ret)
  1266. goto err;
  1267. /* take note of the batch buffer before we might reorder the lists */
  1268. batch_obj = eb_get_batch(eb);
  1269. /* Move the objects en-masse into the GTT, evicting if necessary. */
  1270. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  1271. ret = i915_gem_execbuffer_reserve(engine, &eb->vmas, ctx,
  1272. &need_relocs);
  1273. if (ret)
  1274. goto err;
  1275. /* The objects are in their final locations, apply the relocations. */
  1276. if (need_relocs)
  1277. ret = i915_gem_execbuffer_relocate(eb);
  1278. if (ret) {
  1279. if (ret == -EFAULT) {
  1280. ret = i915_gem_execbuffer_relocate_slow(dev, args, file,
  1281. engine,
  1282. eb, exec, ctx);
  1283. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1284. }
  1285. if (ret)
  1286. goto err;
  1287. }
  1288. /* Set the pending read domains for the batch buffer to COMMAND */
  1289. if (batch_obj->base.pending_write_domain) {
  1290. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1291. ret = -EINVAL;
  1292. goto err;
  1293. }
  1294. params->args_batch_start_offset = args->batch_start_offset;
  1295. if (i915_needs_cmd_parser(engine) && args->batch_len) {
  1296. struct drm_i915_gem_object *parsed_batch_obj;
  1297. parsed_batch_obj = i915_gem_execbuffer_parse(engine,
  1298. &shadow_exec_entry,
  1299. eb,
  1300. batch_obj,
  1301. args->batch_start_offset,
  1302. args->batch_len,
  1303. file->is_master);
  1304. if (IS_ERR(parsed_batch_obj)) {
  1305. ret = PTR_ERR(parsed_batch_obj);
  1306. goto err;
  1307. }
  1308. /*
  1309. * parsed_batch_obj == batch_obj means batch not fully parsed:
  1310. * Accept, but don't promote to secure.
  1311. */
  1312. if (parsed_batch_obj != batch_obj) {
  1313. /*
  1314. * Batch parsed and accepted:
  1315. *
  1316. * Set the DISPATCH_SECURE bit to remove the NON_SECURE
  1317. * bit from MI_BATCH_BUFFER_START commands issued in
  1318. * the dispatch_execbuffer implementations. We
  1319. * specifically don't want that set on batches the
  1320. * command parser has accepted.
  1321. */
  1322. dispatch_flags |= I915_DISPATCH_SECURE;
  1323. params->args_batch_start_offset = 0;
  1324. batch_obj = parsed_batch_obj;
  1325. }
  1326. }
  1327. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1328. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1329. * batch" bit. Hence we need to pin secure batches into the global gtt.
  1330. * hsw should have this fixed, but bdw mucks it up again. */
  1331. if (dispatch_flags & I915_DISPATCH_SECURE) {
  1332. /*
  1333. * So on first glance it looks freaky that we pin the batch here
  1334. * outside of the reservation loop. But:
  1335. * - The batch is already pinned into the relevant ppgtt, so we
  1336. * already have the backing storage fully allocated.
  1337. * - No other BO uses the global gtt (well contexts, but meh),
  1338. * so we don't really have issues with multiple objects not
  1339. * fitting due to fragmentation.
  1340. * So this is actually safe.
  1341. */
  1342. ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
  1343. if (ret)
  1344. goto err;
  1345. params->batch_obj_vm_offset = i915_gem_obj_ggtt_offset(batch_obj);
  1346. } else
  1347. params->batch_obj_vm_offset = i915_gem_obj_offset(batch_obj, vm);
  1348. /* Allocate a request for this batch buffer nice and early. */
  1349. req = i915_gem_request_alloc(engine, ctx);
  1350. if (IS_ERR(req)) {
  1351. ret = PTR_ERR(req);
  1352. goto err_batch_unpin;
  1353. }
  1354. ret = i915_gem_request_add_to_client(req, file);
  1355. if (ret)
  1356. goto err_batch_unpin;
  1357. /*
  1358. * Save assorted stuff away to pass through to *_submission().
  1359. * NB: This data should be 'persistent' and not local as it will
  1360. * kept around beyond the duration of the IOCTL once the GPU
  1361. * scheduler arrives.
  1362. */
  1363. params->dev = dev;
  1364. params->file = file;
  1365. params->engine = engine;
  1366. params->dispatch_flags = dispatch_flags;
  1367. params->batch_obj = batch_obj;
  1368. params->ctx = ctx;
  1369. params->request = req;
  1370. ret = dev_priv->gt.execbuf_submit(params, args, &eb->vmas);
  1371. err_batch_unpin:
  1372. /*
  1373. * FIXME: We crucially rely upon the active tracking for the (ppgtt)
  1374. * batch vma for correctness. For less ugly and less fragility this
  1375. * needs to be adjusted to also track the ggtt batch vma properly as
  1376. * active.
  1377. */
  1378. if (dispatch_flags & I915_DISPATCH_SECURE)
  1379. i915_gem_object_ggtt_unpin(batch_obj);
  1380. err:
  1381. /* the request owns the ref now */
  1382. i915_gem_context_unreference(ctx);
  1383. eb_destroy(eb);
  1384. /*
  1385. * If the request was created but not successfully submitted then it
  1386. * must be freed again. If it was submitted then it is being tracked
  1387. * on the active request list and no clean up is required here.
  1388. */
  1389. if (ret && !IS_ERR_OR_NULL(req))
  1390. i915_gem_request_cancel(req);
  1391. mutex_unlock(&dev->struct_mutex);
  1392. pre_mutex_err:
  1393. /* intel_gpu_busy should also get a ref, so it will free when the device
  1394. * is really idle. */
  1395. intel_runtime_pm_put(dev_priv);
  1396. return ret;
  1397. }
  1398. /*
  1399. * Legacy execbuffer just creates an exec2 list from the original exec object
  1400. * list array and passes it to the real function.
  1401. */
  1402. int
  1403. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1404. struct drm_file *file)
  1405. {
  1406. struct drm_i915_gem_execbuffer *args = data;
  1407. struct drm_i915_gem_execbuffer2 exec2;
  1408. struct drm_i915_gem_exec_object *exec_list = NULL;
  1409. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1410. int ret, i;
  1411. if (args->buffer_count < 1) {
  1412. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1413. return -EINVAL;
  1414. }
  1415. /* Copy in the exec list from userland */
  1416. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1417. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1418. if (exec_list == NULL || exec2_list == NULL) {
  1419. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1420. args->buffer_count);
  1421. drm_free_large(exec_list);
  1422. drm_free_large(exec2_list);
  1423. return -ENOMEM;
  1424. }
  1425. ret = copy_from_user(exec_list,
  1426. to_user_ptr(args->buffers_ptr),
  1427. sizeof(*exec_list) * args->buffer_count);
  1428. if (ret != 0) {
  1429. DRM_DEBUG("copy %d exec entries failed %d\n",
  1430. args->buffer_count, ret);
  1431. drm_free_large(exec_list);
  1432. drm_free_large(exec2_list);
  1433. return -EFAULT;
  1434. }
  1435. for (i = 0; i < args->buffer_count; i++) {
  1436. exec2_list[i].handle = exec_list[i].handle;
  1437. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1438. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1439. exec2_list[i].alignment = exec_list[i].alignment;
  1440. exec2_list[i].offset = exec_list[i].offset;
  1441. if (INTEL_INFO(dev)->gen < 4)
  1442. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1443. else
  1444. exec2_list[i].flags = 0;
  1445. }
  1446. exec2.buffers_ptr = args->buffers_ptr;
  1447. exec2.buffer_count = args->buffer_count;
  1448. exec2.batch_start_offset = args->batch_start_offset;
  1449. exec2.batch_len = args->batch_len;
  1450. exec2.DR1 = args->DR1;
  1451. exec2.DR4 = args->DR4;
  1452. exec2.num_cliprects = args->num_cliprects;
  1453. exec2.cliprects_ptr = args->cliprects_ptr;
  1454. exec2.flags = I915_EXEC_RENDER;
  1455. i915_execbuffer2_set_context_id(exec2, 0);
  1456. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1457. if (!ret) {
  1458. struct drm_i915_gem_exec_object __user *user_exec_list =
  1459. to_user_ptr(args->buffers_ptr);
  1460. /* Copy the new buffer offsets back to the user's exec list. */
  1461. for (i = 0; i < args->buffer_count; i++) {
  1462. exec2_list[i].offset =
  1463. gen8_canonical_addr(exec2_list[i].offset);
  1464. ret = __copy_to_user(&user_exec_list[i].offset,
  1465. &exec2_list[i].offset,
  1466. sizeof(user_exec_list[i].offset));
  1467. if (ret) {
  1468. ret = -EFAULT;
  1469. DRM_DEBUG("failed to copy %d exec entries "
  1470. "back to user (%d)\n",
  1471. args->buffer_count, ret);
  1472. break;
  1473. }
  1474. }
  1475. }
  1476. drm_free_large(exec_list);
  1477. drm_free_large(exec2_list);
  1478. return ret;
  1479. }
  1480. int
  1481. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1482. struct drm_file *file)
  1483. {
  1484. struct drm_i915_gem_execbuffer2 *args = data;
  1485. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1486. int ret;
  1487. if (args->buffer_count < 1 ||
  1488. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1489. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1490. return -EINVAL;
  1491. }
  1492. if (args->rsvd2 != 0) {
  1493. DRM_DEBUG("dirty rvsd2 field\n");
  1494. return -EINVAL;
  1495. }
  1496. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1497. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  1498. if (exec2_list == NULL)
  1499. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1500. args->buffer_count);
  1501. if (exec2_list == NULL) {
  1502. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1503. args->buffer_count);
  1504. return -ENOMEM;
  1505. }
  1506. ret = copy_from_user(exec2_list,
  1507. to_user_ptr(args->buffers_ptr),
  1508. sizeof(*exec2_list) * args->buffer_count);
  1509. if (ret != 0) {
  1510. DRM_DEBUG("copy %d exec entries failed %d\n",
  1511. args->buffer_count, ret);
  1512. drm_free_large(exec2_list);
  1513. return -EFAULT;
  1514. }
  1515. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1516. if (!ret) {
  1517. /* Copy the new buffer offsets back to the user's exec list. */
  1518. struct drm_i915_gem_exec_object2 __user *user_exec_list =
  1519. to_user_ptr(args->buffers_ptr);
  1520. int i;
  1521. for (i = 0; i < args->buffer_count; i++) {
  1522. exec2_list[i].offset =
  1523. gen8_canonical_addr(exec2_list[i].offset);
  1524. ret = __copy_to_user(&user_exec_list[i].offset,
  1525. &exec2_list[i].offset,
  1526. sizeof(user_exec_list[i].offset));
  1527. if (ret) {
  1528. ret = -EFAULT;
  1529. DRM_DEBUG("failed to copy %d exec entries "
  1530. "back to user\n",
  1531. args->buffer_count);
  1532. break;
  1533. }
  1534. }
  1535. }
  1536. drm_free_large(exec2_list);
  1537. return ret;
  1538. }