i915_gem.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. /*
  2. * Copyright © 2008-2015 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. *
  26. */
  27. #include <linux/dma-fence-array.h>
  28. #include <linux/kthread.h>
  29. #include <linux/dma-resv.h>
  30. #include <linux/shmem_fs.h>
  31. #include <linux/slab.h>
  32. #include <linux/stop_machine.h>
  33. #include <linux/swap.h>
  34. #include <linux/pci.h>
  35. #include <linux/dma-buf.h>
  36. #include <linux/mman.h>
  37. #include <drm/drm_cache.h>
  38. #include <drm/drm_print.h>
  39. #include <drm/drm_vma_manager.h>
  40. #include "gem/i915_gem_clflush.h"
  41. #include "gem/i915_gem_context.h"
  42. #include "gem/i915_gem_ioctls.h"
  43. #include "gem/i915_gem_mman.h"
  44. #include "gem/i915_gem_object_frontbuffer.h"
  45. #include "gem/i915_gem_pm.h"
  46. #include "gem/i915_gem_region.h"
  47. #include "gt/intel_engine_user.h"
  48. #include "gt/intel_gt.h"
  49. #include "gt/intel_gt_pm.h"
  50. #include "gt/intel_workarounds.h"
  51. #include "i915_drv.h"
  52. #include "i915_file_private.h"
  53. #include "i915_trace.h"
  54. #include "i915_vgpu.h"
  55. #include "intel_clock_gating.h"
  56. static int
  57. insert_mappable_node(struct i915_ggtt *ggtt, struct drm_mm_node *node, u32 size)
  58. {
  59. int err;
  60. err = mutex_lock_interruptible(&ggtt->vm.mutex);
  61. if (err)
  62. return err;
  63. memset(node, 0, sizeof(*node));
  64. err = drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
  65. size, 0, I915_COLOR_UNEVICTABLE,
  66. 0, ggtt->mappable_end,
  67. DRM_MM_INSERT_LOW);
  68. mutex_unlock(&ggtt->vm.mutex);
  69. return err;
  70. }
  71. static void
  72. remove_mappable_node(struct i915_ggtt *ggtt, struct drm_mm_node *node)
  73. {
  74. mutex_lock(&ggtt->vm.mutex);
  75. drm_mm_remove_node(node);
  76. mutex_unlock(&ggtt->vm.mutex);
  77. }
  78. int
  79. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  80. struct drm_file *file)
  81. {
  82. struct drm_i915_private *i915 = to_i915(dev);
  83. struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
  84. struct drm_i915_gem_get_aperture *args = data;
  85. struct i915_vma *vma;
  86. u64 pinned;
  87. if (mutex_lock_interruptible(&ggtt->vm.mutex))
  88. return -EINTR;
  89. pinned = ggtt->vm.reserved;
  90. list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
  91. if (i915_vma_is_pinned(vma))
  92. pinned += vma->node.size;
  93. mutex_unlock(&ggtt->vm.mutex);
  94. args->aper_size = ggtt->vm.total;
  95. args->aper_available_size = args->aper_size - pinned;
  96. return 0;
  97. }
  98. int i915_gem_object_unbind(struct drm_i915_gem_object *obj,
  99. unsigned long flags)
  100. {
  101. struct intel_runtime_pm *rpm = &to_i915(obj->base.dev)->runtime_pm;
  102. bool vm_trylock = !!(flags & I915_GEM_OBJECT_UNBIND_VM_TRYLOCK);
  103. LIST_HEAD(still_in_list);
  104. intel_wakeref_t wakeref;
  105. struct i915_vma *vma;
  106. int ret;
  107. assert_object_held(obj);
  108. if (list_empty(&obj->vma.list))
  109. return 0;
  110. /*
  111. * As some machines use ACPI to handle runtime-resume callbacks, and
  112. * ACPI is quite kmalloc happy, we cannot resume beneath the vm->mutex
  113. * as they are required by the shrinker. Ergo, we wake the device up
  114. * first just in case.
  115. */
  116. wakeref = intel_runtime_pm_get(rpm);
  117. try_again:
  118. ret = 0;
  119. spin_lock(&obj->vma.lock);
  120. while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
  121. struct i915_vma,
  122. obj_link))) {
  123. list_move_tail(&vma->obj_link, &still_in_list);
  124. if (!i915_vma_is_bound(vma, I915_VMA_BIND_MASK))
  125. continue;
  126. if (flags & I915_GEM_OBJECT_UNBIND_TEST) {
  127. ret = -EBUSY;
  128. break;
  129. }
  130. /*
  131. * Requiring the vm destructor to take the object lock
  132. * before destroying a vma would help us eliminate the
  133. * i915_vm_tryget() here, AND thus also the barrier stuff
  134. * at the end. That's an easy fix, but sleeping locks in
  135. * a kthread should generally be avoided.
  136. */
  137. ret = -EAGAIN;
  138. if (!i915_vm_tryget(vma->vm))
  139. break;
  140. spin_unlock(&obj->vma.lock);
  141. /*
  142. * Since i915_vma_parked() takes the object lock
  143. * before vma destruction, it won't race us here,
  144. * and destroy the vma from under us.
  145. */
  146. ret = -EBUSY;
  147. if (flags & I915_GEM_OBJECT_UNBIND_ASYNC) {
  148. assert_object_held(vma->obj);
  149. ret = i915_vma_unbind_async(vma, vm_trylock);
  150. }
  151. if (ret == -EBUSY && (flags & I915_GEM_OBJECT_UNBIND_ACTIVE ||
  152. !i915_vma_is_active(vma))) {
  153. if (vm_trylock) {
  154. if (mutex_trylock(&vma->vm->mutex)) {
  155. ret = __i915_vma_unbind(vma);
  156. mutex_unlock(&vma->vm->mutex);
  157. }
  158. } else {
  159. ret = i915_vma_unbind(vma);
  160. }
  161. }
  162. i915_vm_put(vma->vm);
  163. spin_lock(&obj->vma.lock);
  164. }
  165. list_splice_init(&still_in_list, &obj->vma.list);
  166. spin_unlock(&obj->vma.lock);
  167. if (ret == -EAGAIN && flags & I915_GEM_OBJECT_UNBIND_BARRIER) {
  168. rcu_barrier(); /* flush the i915_vm_release() */
  169. goto try_again;
  170. }
  171. intel_runtime_pm_put(rpm, wakeref);
  172. return ret;
  173. }
  174. static int
  175. shmem_pread(struct page *page, int offset, int len, char __user *user_data,
  176. bool needs_clflush)
  177. {
  178. char *vaddr;
  179. int ret;
  180. vaddr = kmap(page);
  181. if (needs_clflush)
  182. drm_clflush_virt_range(vaddr + offset, len);
  183. ret = __copy_to_user(user_data, vaddr + offset, len);
  184. kunmap(page);
  185. return ret ? -EFAULT : 0;
  186. }
  187. static int
  188. i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
  189. struct drm_i915_gem_pread *args)
  190. {
  191. unsigned int needs_clflush;
  192. char __user *user_data;
  193. unsigned long offset;
  194. pgoff_t idx;
  195. u64 remain;
  196. int ret;
  197. ret = i915_gem_object_lock_interruptible(obj, NULL);
  198. if (ret)
  199. return ret;
  200. ret = i915_gem_object_pin_pages(obj);
  201. if (ret)
  202. goto err_unlock;
  203. ret = i915_gem_object_prepare_read(obj, &needs_clflush);
  204. if (ret)
  205. goto err_unpin;
  206. i915_gem_object_finish_access(obj);
  207. i915_gem_object_unlock(obj);
  208. remain = args->size;
  209. user_data = u64_to_user_ptr(args->data_ptr);
  210. offset = offset_in_page(args->offset);
  211. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  212. struct page *page = i915_gem_object_get_page(obj, idx);
  213. unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
  214. ret = shmem_pread(page, offset, length, user_data,
  215. needs_clflush);
  216. if (ret)
  217. break;
  218. remain -= length;
  219. user_data += length;
  220. offset = 0;
  221. }
  222. i915_gem_object_unpin_pages(obj);
  223. return ret;
  224. err_unpin:
  225. i915_gem_object_unpin_pages(obj);
  226. err_unlock:
  227. i915_gem_object_unlock(obj);
  228. return ret;
  229. }
  230. static inline bool
  231. gtt_user_read(struct io_mapping *mapping,
  232. loff_t base, int offset,
  233. char __user *user_data, int length)
  234. {
  235. void __iomem *vaddr;
  236. unsigned long unwritten;
  237. /* We can use the cpu mem copy function because this is X86. */
  238. vaddr = io_mapping_map_atomic_wc(mapping, base);
  239. unwritten = __copy_to_user_inatomic(user_data,
  240. (void __force *)vaddr + offset,
  241. length);
  242. io_mapping_unmap_atomic(vaddr);
  243. if (unwritten) {
  244. vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
  245. unwritten = copy_to_user(user_data,
  246. (void __force *)vaddr + offset,
  247. length);
  248. io_mapping_unmap(vaddr);
  249. }
  250. return unwritten;
  251. }
  252. static struct i915_vma *i915_gem_gtt_prepare(struct drm_i915_gem_object *obj,
  253. struct drm_mm_node *node,
  254. bool write)
  255. {
  256. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  257. struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
  258. struct i915_vma *vma;
  259. struct i915_gem_ww_ctx ww;
  260. int ret;
  261. i915_gem_ww_ctx_init(&ww, true);
  262. retry:
  263. vma = ERR_PTR(-ENODEV);
  264. ret = i915_gem_object_lock(obj, &ww);
  265. if (ret)
  266. goto err_ww;
  267. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  268. if (ret)
  269. goto err_ww;
  270. if (!i915_gem_object_is_tiled(obj))
  271. vma = i915_gem_object_ggtt_pin_ww(obj, &ww, NULL, 0, 0,
  272. PIN_MAPPABLE |
  273. PIN_NONBLOCK /* NOWARN */ |
  274. PIN_NOEVICT);
  275. if (vma == ERR_PTR(-EDEADLK)) {
  276. ret = -EDEADLK;
  277. goto err_ww;
  278. } else if (!IS_ERR(vma)) {
  279. node->start = i915_ggtt_offset(vma);
  280. node->flags = 0;
  281. } else {
  282. ret = insert_mappable_node(ggtt, node, PAGE_SIZE);
  283. if (ret)
  284. goto err_ww;
  285. GEM_BUG_ON(!drm_mm_node_allocated(node));
  286. vma = NULL;
  287. }
  288. ret = i915_gem_object_pin_pages(obj);
  289. if (ret) {
  290. if (drm_mm_node_allocated(node)) {
  291. ggtt->vm.clear_range(&ggtt->vm, node->start, node->size);
  292. remove_mappable_node(ggtt, node);
  293. } else {
  294. i915_vma_unpin(vma);
  295. }
  296. }
  297. err_ww:
  298. if (ret == -EDEADLK) {
  299. ret = i915_gem_ww_ctx_backoff(&ww);
  300. if (!ret)
  301. goto retry;
  302. }
  303. i915_gem_ww_ctx_fini(&ww);
  304. return ret ? ERR_PTR(ret) : vma;
  305. }
  306. static void i915_gem_gtt_cleanup(struct drm_i915_gem_object *obj,
  307. struct drm_mm_node *node,
  308. struct i915_vma *vma)
  309. {
  310. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  311. struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
  312. i915_gem_object_unpin_pages(obj);
  313. if (drm_mm_node_allocated(node)) {
  314. ggtt->vm.clear_range(&ggtt->vm, node->start, node->size);
  315. remove_mappable_node(ggtt, node);
  316. } else {
  317. i915_vma_unpin(vma);
  318. }
  319. }
  320. static int
  321. i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
  322. const struct drm_i915_gem_pread *args)
  323. {
  324. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  325. struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
  326. unsigned long remain, offset;
  327. intel_wakeref_t wakeref;
  328. struct drm_mm_node node;
  329. void __user *user_data;
  330. struct i915_vma *vma;
  331. int ret = 0;
  332. if (overflows_type(args->size, remain) ||
  333. overflows_type(args->offset, offset))
  334. return -EINVAL;
  335. wakeref = intel_runtime_pm_get(&i915->runtime_pm);
  336. vma = i915_gem_gtt_prepare(obj, &node, false);
  337. if (IS_ERR(vma)) {
  338. ret = PTR_ERR(vma);
  339. goto out_rpm;
  340. }
  341. user_data = u64_to_user_ptr(args->data_ptr);
  342. remain = args->size;
  343. offset = args->offset;
  344. while (remain > 0) {
  345. /* Operation in this page
  346. *
  347. * page_base = page offset within aperture
  348. * page_offset = offset within page
  349. * page_length = bytes to copy for this page
  350. */
  351. u32 page_base = node.start;
  352. unsigned page_offset = offset_in_page(offset);
  353. unsigned page_length = PAGE_SIZE - page_offset;
  354. page_length = remain < page_length ? remain : page_length;
  355. if (drm_mm_node_allocated(&node)) {
  356. ggtt->vm.insert_page(&ggtt->vm,
  357. i915_gem_object_get_dma_address(obj,
  358. offset >> PAGE_SHIFT),
  359. node.start,
  360. i915_gem_get_pat_index(i915,
  361. I915_CACHE_NONE), 0);
  362. } else {
  363. page_base += offset & PAGE_MASK;
  364. }
  365. if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
  366. user_data, page_length)) {
  367. ret = -EFAULT;
  368. break;
  369. }
  370. remain -= page_length;
  371. user_data += page_length;
  372. offset += page_length;
  373. }
  374. i915_gem_gtt_cleanup(obj, &node, vma);
  375. out_rpm:
  376. intel_runtime_pm_put(&i915->runtime_pm, wakeref);
  377. return ret;
  378. }
  379. /**
  380. * i915_gem_pread_ioctl - Reads data from the object referenced by handle.
  381. * @dev: drm device pointer
  382. * @data: ioctl data blob
  383. * @file: drm file pointer
  384. *
  385. * On error, the contents of *data are undefined.
  386. */
  387. int
  388. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  389. struct drm_file *file)
  390. {
  391. struct drm_i915_private *i915 = to_i915(dev);
  392. struct drm_i915_gem_pread *args = data;
  393. struct drm_i915_gem_object *obj;
  394. int ret;
  395. /* PREAD is disallowed for all platforms after TGL-LP. This also
  396. * covers all platforms with local memory.
  397. */
  398. if (GRAPHICS_VER(i915) >= 12 && !IS_TIGERLAKE(i915))
  399. return -EOPNOTSUPP;
  400. if (args->size == 0)
  401. return 0;
  402. if (!access_ok(u64_to_user_ptr(args->data_ptr),
  403. args->size))
  404. return -EFAULT;
  405. obj = i915_gem_object_lookup(file, args->handle);
  406. if (!obj)
  407. return -ENOENT;
  408. /* Bounds check source. */
  409. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  410. ret = -EINVAL;
  411. goto out;
  412. }
  413. trace_i915_gem_object_pread(obj, args->offset, args->size);
  414. ret = -ENODEV;
  415. if (obj->ops->pread)
  416. ret = obj->ops->pread(obj, args);
  417. if (ret != -ENODEV)
  418. goto out;
  419. ret = i915_gem_object_wait(obj,
  420. I915_WAIT_INTERRUPTIBLE,
  421. MAX_SCHEDULE_TIMEOUT);
  422. if (ret)
  423. goto out;
  424. ret = i915_gem_shmem_pread(obj, args);
  425. if (ret == -EFAULT || ret == -ENODEV)
  426. ret = i915_gem_gtt_pread(obj, args);
  427. out:
  428. i915_gem_object_put(obj);
  429. return ret;
  430. }
  431. /* This is the fast write path which cannot handle
  432. * page faults in the source data
  433. */
  434. static inline bool
  435. ggtt_write(struct io_mapping *mapping,
  436. loff_t base, int offset,
  437. char __user *user_data, int length)
  438. {
  439. void __iomem *vaddr;
  440. unsigned long unwritten;
  441. /* We can use the cpu mem copy function because this is X86. */
  442. vaddr = io_mapping_map_atomic_wc(mapping, base);
  443. unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
  444. user_data, length);
  445. io_mapping_unmap_atomic(vaddr);
  446. if (unwritten) {
  447. vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
  448. unwritten = copy_from_user((void __force *)vaddr + offset,
  449. user_data, length);
  450. io_mapping_unmap(vaddr);
  451. }
  452. return unwritten;
  453. }
  454. /**
  455. * i915_gem_gtt_pwrite_fast - This is the fast pwrite path, where we copy the data directly from the
  456. * user into the GTT, uncached.
  457. * @obj: i915 GEM object
  458. * @args: pwrite arguments structure
  459. */
  460. static int
  461. i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
  462. const struct drm_i915_gem_pwrite *args)
  463. {
  464. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  465. struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
  466. struct intel_runtime_pm *rpm = &i915->runtime_pm;
  467. unsigned long remain, offset;
  468. intel_wakeref_t wakeref;
  469. struct drm_mm_node node;
  470. struct i915_vma *vma;
  471. void __user *user_data;
  472. int ret = 0;
  473. if (overflows_type(args->size, remain) ||
  474. overflows_type(args->offset, offset))
  475. return -EINVAL;
  476. if (i915_gem_object_has_struct_page(obj)) {
  477. /*
  478. * Avoid waking the device up if we can fallback, as
  479. * waking/resuming is very slow (worst-case 10-100 ms
  480. * depending on PCI sleeps and our own resume time).
  481. * This easily dwarfs any performance advantage from
  482. * using the cache bypass of indirect GGTT access.
  483. */
  484. wakeref = intel_runtime_pm_get_if_in_use(rpm);
  485. if (!wakeref)
  486. return -EFAULT;
  487. } else {
  488. /* No backing pages, no fallback, we must force GGTT access */
  489. wakeref = intel_runtime_pm_get(rpm);
  490. }
  491. vma = i915_gem_gtt_prepare(obj, &node, true);
  492. if (IS_ERR(vma)) {
  493. ret = PTR_ERR(vma);
  494. goto out_rpm;
  495. }
  496. i915_gem_object_invalidate_frontbuffer(obj, ORIGIN_CPU);
  497. user_data = u64_to_user_ptr(args->data_ptr);
  498. offset = args->offset;
  499. remain = args->size;
  500. while (remain) {
  501. /* Operation in this page
  502. *
  503. * page_base = page offset within aperture
  504. * page_offset = offset within page
  505. * page_length = bytes to copy for this page
  506. */
  507. u32 page_base = node.start;
  508. unsigned int page_offset = offset_in_page(offset);
  509. unsigned int page_length = PAGE_SIZE - page_offset;
  510. page_length = remain < page_length ? remain : page_length;
  511. if (drm_mm_node_allocated(&node)) {
  512. /* flush the write before we modify the GGTT */
  513. intel_gt_flush_ggtt_writes(ggtt->vm.gt);
  514. ggtt->vm.insert_page(&ggtt->vm,
  515. i915_gem_object_get_dma_address(obj,
  516. offset >> PAGE_SHIFT),
  517. node.start,
  518. i915_gem_get_pat_index(i915,
  519. I915_CACHE_NONE), 0);
  520. wmb(); /* flush modifications to the GGTT (insert_page) */
  521. } else {
  522. page_base += offset & PAGE_MASK;
  523. }
  524. /* If we get a fault while copying data, then (presumably) our
  525. * source page isn't available. Return the error and we'll
  526. * retry in the slow path.
  527. * If the object is non-shmem backed, we retry again with the
  528. * path that handles page fault.
  529. */
  530. if (ggtt_write(&ggtt->iomap, page_base, page_offset,
  531. user_data, page_length)) {
  532. ret = -EFAULT;
  533. break;
  534. }
  535. remain -= page_length;
  536. user_data += page_length;
  537. offset += page_length;
  538. }
  539. intel_gt_flush_ggtt_writes(ggtt->vm.gt);
  540. i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
  541. i915_gem_gtt_cleanup(obj, &node, vma);
  542. out_rpm:
  543. intel_runtime_pm_put(rpm, wakeref);
  544. return ret;
  545. }
  546. /* Per-page copy function for the shmem pwrite fastpath.
  547. * Flushes invalid cachelines before writing to the target if
  548. * needs_clflush_before is set and flushes out any written cachelines after
  549. * writing if needs_clflush is set.
  550. */
  551. static int
  552. shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
  553. bool needs_clflush_before,
  554. bool needs_clflush_after)
  555. {
  556. char *vaddr;
  557. int ret;
  558. vaddr = kmap(page);
  559. if (needs_clflush_before)
  560. drm_clflush_virt_range(vaddr + offset, len);
  561. ret = __copy_from_user(vaddr + offset, user_data, len);
  562. if (!ret && needs_clflush_after)
  563. drm_clflush_virt_range(vaddr + offset, len);
  564. kunmap(page);
  565. return ret ? -EFAULT : 0;
  566. }
  567. static int
  568. i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
  569. const struct drm_i915_gem_pwrite *args)
  570. {
  571. unsigned int partial_cacheline_write;
  572. unsigned int needs_clflush;
  573. void __user *user_data;
  574. unsigned long offset;
  575. pgoff_t idx;
  576. u64 remain;
  577. int ret;
  578. ret = i915_gem_object_lock_interruptible(obj, NULL);
  579. if (ret)
  580. return ret;
  581. ret = i915_gem_object_pin_pages(obj);
  582. if (ret)
  583. goto err_unlock;
  584. ret = i915_gem_object_prepare_write(obj, &needs_clflush);
  585. if (ret)
  586. goto err_unpin;
  587. i915_gem_object_finish_access(obj);
  588. i915_gem_object_unlock(obj);
  589. /* If we don't overwrite a cacheline completely we need to be
  590. * careful to have up-to-date data by first clflushing. Don't
  591. * overcomplicate things and flush the entire patch.
  592. */
  593. partial_cacheline_write = 0;
  594. if (needs_clflush & CLFLUSH_BEFORE)
  595. partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
  596. user_data = u64_to_user_ptr(args->data_ptr);
  597. remain = args->size;
  598. offset = offset_in_page(args->offset);
  599. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  600. struct page *page = i915_gem_object_get_page(obj, idx);
  601. unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
  602. ret = shmem_pwrite(page, offset, length, user_data,
  603. (offset | length) & partial_cacheline_write,
  604. needs_clflush & CLFLUSH_AFTER);
  605. if (ret)
  606. break;
  607. remain -= length;
  608. user_data += length;
  609. offset = 0;
  610. }
  611. i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
  612. i915_gem_object_unpin_pages(obj);
  613. return ret;
  614. err_unpin:
  615. i915_gem_object_unpin_pages(obj);
  616. err_unlock:
  617. i915_gem_object_unlock(obj);
  618. return ret;
  619. }
  620. /**
  621. * i915_gem_pwrite_ioctl - Writes data to the object referenced by handle.
  622. * @dev: drm device
  623. * @data: ioctl data blob
  624. * @file: drm file
  625. *
  626. * On error, the contents of the buffer that were to be modified are undefined.
  627. */
  628. int
  629. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  630. struct drm_file *file)
  631. {
  632. struct drm_i915_private *i915 = to_i915(dev);
  633. struct drm_i915_gem_pwrite *args = data;
  634. struct drm_i915_gem_object *obj;
  635. int ret;
  636. /* PWRITE is disallowed for all platforms after TGL-LP. This also
  637. * covers all platforms with local memory.
  638. */
  639. if (GRAPHICS_VER(i915) >= 12 && !IS_TIGERLAKE(i915))
  640. return -EOPNOTSUPP;
  641. if (args->size == 0)
  642. return 0;
  643. if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
  644. return -EFAULT;
  645. obj = i915_gem_object_lookup(file, args->handle);
  646. if (!obj)
  647. return -ENOENT;
  648. /* Bounds check destination. */
  649. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  650. ret = -EINVAL;
  651. goto err;
  652. }
  653. /* Writes not allowed into this read-only object */
  654. if (i915_gem_object_is_readonly(obj)) {
  655. ret = -EINVAL;
  656. goto err;
  657. }
  658. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  659. ret = -ENODEV;
  660. if (obj->ops->pwrite)
  661. ret = obj->ops->pwrite(obj, args);
  662. if (ret != -ENODEV)
  663. goto err;
  664. ret = i915_gem_object_wait(obj,
  665. I915_WAIT_INTERRUPTIBLE |
  666. I915_WAIT_ALL,
  667. MAX_SCHEDULE_TIMEOUT);
  668. if (ret)
  669. goto err;
  670. ret = -EFAULT;
  671. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  672. * it would end up going through the fenced access, and we'll get
  673. * different detiling behavior between reading and writing.
  674. * pread/pwrite currently are reading and writing from the CPU
  675. * perspective, requiring manual detiling by the client.
  676. */
  677. if (!i915_gem_object_has_struct_page(obj) ||
  678. i915_gem_cpu_write_needs_clflush(obj))
  679. /* Note that the gtt paths might fail with non-page-backed user
  680. * pointers (e.g. gtt mappings when moving data between
  681. * textures). Fallback to the shmem path in that case.
  682. */
  683. ret = i915_gem_gtt_pwrite_fast(obj, args);
  684. if (ret == -EFAULT || ret == -ENOSPC) {
  685. if (i915_gem_object_has_struct_page(obj))
  686. ret = i915_gem_shmem_pwrite(obj, args);
  687. }
  688. err:
  689. i915_gem_object_put(obj);
  690. return ret;
  691. }
  692. /**
  693. * i915_gem_sw_finish_ioctl - Called when user space has done writes to this buffer
  694. * @dev: drm device
  695. * @data: ioctl data blob
  696. * @file: drm file
  697. */
  698. int
  699. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  700. struct drm_file *file)
  701. {
  702. struct drm_i915_gem_sw_finish *args = data;
  703. struct drm_i915_gem_object *obj;
  704. obj = i915_gem_object_lookup(file, args->handle);
  705. if (!obj)
  706. return -ENOENT;
  707. /*
  708. * Proxy objects are barred from CPU access, so there is no
  709. * need to ban sw_finish as it is a nop.
  710. */
  711. /* Pinned buffers may be scanout, so flush the cache */
  712. i915_gem_object_flush_if_display(obj);
  713. i915_gem_object_put(obj);
  714. return 0;
  715. }
  716. void i915_gem_runtime_suspend(struct drm_i915_private *i915)
  717. {
  718. struct drm_i915_gem_object *obj, *on;
  719. int i;
  720. /*
  721. * Only called during RPM suspend. All users of the userfault_list
  722. * must be holding an RPM wakeref to ensure that this can not
  723. * run concurrently with themselves.
  724. */
  725. list_for_each_entry_safe(obj, on,
  726. &to_gt(i915)->ggtt->userfault_list, userfault_link)
  727. __i915_gem_object_release_mmap_gtt(obj);
  728. list_for_each_entry_safe(obj, on,
  729. &i915->runtime_pm.lmem_userfault_list, userfault_link)
  730. i915_gem_object_runtime_pm_release_mmap_offset(obj);
  731. /*
  732. * The fence will be lost when the device powers down. If any were
  733. * in use by hardware (i.e. they are pinned), we should not be powering
  734. * down! All other fences will be reacquired by the user upon waking.
  735. */
  736. for (i = 0; i < to_gt(i915)->ggtt->num_fences; i++) {
  737. struct i915_fence_reg *reg = &to_gt(i915)->ggtt->fence_regs[i];
  738. /*
  739. * Ideally we want to assert that the fence register is not
  740. * live at this point (i.e. that no piece of code will be
  741. * trying to write through fence + GTT, as that both violates
  742. * our tracking of activity and associated locking/barriers,
  743. * but also is illegal given that the hw is powered down).
  744. *
  745. * Previously we used reg->pin_count as a "liveness" indicator.
  746. * That is not sufficient, and we need a more fine-grained
  747. * tool if we want to have a sanity check here.
  748. */
  749. if (!reg->vma)
  750. continue;
  751. GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
  752. reg->dirty = true;
  753. }
  754. }
  755. static void discard_ggtt_vma(struct i915_vma *vma)
  756. {
  757. struct drm_i915_gem_object *obj = vma->obj;
  758. spin_lock(&obj->vma.lock);
  759. if (!RB_EMPTY_NODE(&vma->obj_node)) {
  760. rb_erase(&vma->obj_node, &obj->vma.tree);
  761. RB_CLEAR_NODE(&vma->obj_node);
  762. }
  763. spin_unlock(&obj->vma.lock);
  764. }
  765. struct i915_vma *
  766. i915_gem_object_ggtt_pin_ww(struct drm_i915_gem_object *obj,
  767. struct i915_gem_ww_ctx *ww,
  768. const struct i915_gtt_view *view,
  769. u64 size, u64 alignment, u64 flags)
  770. {
  771. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  772. struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
  773. struct i915_vma *vma;
  774. int ret;
  775. GEM_WARN_ON(!ww);
  776. if (flags & PIN_MAPPABLE &&
  777. (!view || view->type == I915_GTT_VIEW_NORMAL)) {
  778. /*
  779. * If the required space is larger than the available
  780. * aperture, we will not able to find a slot for the
  781. * object and unbinding the object now will be in
  782. * vain. Worse, doing so may cause us to ping-pong
  783. * the object in and out of the Global GTT and
  784. * waste a lot of cycles under the mutex.
  785. */
  786. if (obj->base.size > ggtt->mappable_end)
  787. return ERR_PTR(-E2BIG);
  788. /*
  789. * If NONBLOCK is set the caller is optimistically
  790. * trying to cache the full object within the mappable
  791. * aperture, and *must* have a fallback in place for
  792. * situations where we cannot bind the object. We
  793. * can be a little more lax here and use the fallback
  794. * more often to avoid costly migrations of ourselves
  795. * and other objects within the aperture.
  796. *
  797. * Half-the-aperture is used as a simple heuristic.
  798. * More interesting would to do search for a free
  799. * block prior to making the commitment to unbind.
  800. * That caters for the self-harm case, and with a
  801. * little more heuristics (e.g. NOFAULT, NOEVICT)
  802. * we could try to minimise harm to others.
  803. */
  804. if (flags & PIN_NONBLOCK &&
  805. obj->base.size > ggtt->mappable_end / 2)
  806. return ERR_PTR(-ENOSPC);
  807. }
  808. new_vma:
  809. vma = i915_vma_instance(obj, &ggtt->vm, view);
  810. if (IS_ERR(vma))
  811. return vma;
  812. if (i915_vma_misplaced(vma, size, alignment, flags)) {
  813. if (flags & PIN_NONBLOCK) {
  814. if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
  815. return ERR_PTR(-ENOSPC);
  816. /*
  817. * If this misplaced vma is too big (i.e, at-least
  818. * half the size of aperture) or hasn't been pinned
  819. * mappable before, we ignore the misplacement when
  820. * PIN_NONBLOCK is set in order to avoid the ping-pong
  821. * issue described above. In other words, we try to
  822. * avoid the costly operation of unbinding this vma
  823. * from the GGTT and rebinding it back because there
  824. * may not be enough space for this vma in the aperture.
  825. */
  826. if (flags & PIN_MAPPABLE &&
  827. (vma->fence_size > ggtt->mappable_end / 2 ||
  828. !i915_vma_is_map_and_fenceable(vma)))
  829. return ERR_PTR(-ENOSPC);
  830. }
  831. if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) {
  832. discard_ggtt_vma(vma);
  833. goto new_vma;
  834. }
  835. ret = i915_vma_unbind(vma);
  836. if (ret)
  837. return ERR_PTR(ret);
  838. }
  839. ret = i915_vma_pin_ww(vma, ww, size, alignment, flags | PIN_GLOBAL);
  840. if (ret)
  841. return ERR_PTR(ret);
  842. if (vma->fence && !i915_gem_object_is_tiled(obj)) {
  843. mutex_lock(&ggtt->vm.mutex);
  844. i915_vma_revoke_fence(vma);
  845. mutex_unlock(&ggtt->vm.mutex);
  846. }
  847. ret = i915_vma_wait_for_bind(vma);
  848. if (ret) {
  849. i915_vma_unpin(vma);
  850. return ERR_PTR(ret);
  851. }
  852. return vma;
  853. }
  854. struct i915_vma * __must_check
  855. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  856. const struct i915_gtt_view *view,
  857. u64 size, u64 alignment, u64 flags)
  858. {
  859. struct i915_gem_ww_ctx ww;
  860. struct i915_vma *ret;
  861. int err;
  862. for_i915_gem_ww(&ww, err, true) {
  863. err = i915_gem_object_lock(obj, &ww);
  864. if (err)
  865. continue;
  866. ret = i915_gem_object_ggtt_pin_ww(obj, &ww, view, size,
  867. alignment, flags);
  868. if (IS_ERR(ret))
  869. err = PTR_ERR(ret);
  870. }
  871. return err ? ERR_PTR(err) : ret;
  872. }
  873. int
  874. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  875. struct drm_file *file_priv)
  876. {
  877. struct drm_i915_private *i915 = to_i915(dev);
  878. struct drm_i915_gem_madvise *args = data;
  879. struct drm_i915_gem_object *obj;
  880. int err;
  881. switch (args->madv) {
  882. case I915_MADV_DONTNEED:
  883. case I915_MADV_WILLNEED:
  884. break;
  885. default:
  886. return -EINVAL;
  887. }
  888. obj = i915_gem_object_lookup(file_priv, args->handle);
  889. if (!obj)
  890. return -ENOENT;
  891. err = i915_gem_object_lock_interruptible(obj, NULL);
  892. if (err)
  893. goto out;
  894. if (i915_gem_object_has_pages(obj) &&
  895. i915_gem_object_is_tiled(obj) &&
  896. i915->gem_quirks & GEM_QUIRK_PIN_SWIZZLED_PAGES) {
  897. if (obj->mm.madv == I915_MADV_WILLNEED) {
  898. GEM_BUG_ON(!i915_gem_object_has_tiling_quirk(obj));
  899. i915_gem_object_clear_tiling_quirk(obj);
  900. i915_gem_object_make_shrinkable(obj);
  901. }
  902. if (args->madv == I915_MADV_WILLNEED) {
  903. GEM_BUG_ON(i915_gem_object_has_tiling_quirk(obj));
  904. i915_gem_object_make_unshrinkable(obj);
  905. i915_gem_object_set_tiling_quirk(obj);
  906. }
  907. }
  908. if (obj->mm.madv != __I915_MADV_PURGED) {
  909. obj->mm.madv = args->madv;
  910. if (obj->ops->adjust_lru)
  911. obj->ops->adjust_lru(obj);
  912. }
  913. if (i915_gem_object_has_pages(obj) ||
  914. i915_gem_object_has_self_managed_shrink_list(obj)) {
  915. unsigned long flags;
  916. spin_lock_irqsave(&i915->mm.obj_lock, flags);
  917. if (!list_empty(&obj->mm.link)) {
  918. struct list_head *list;
  919. if (obj->mm.madv != I915_MADV_WILLNEED)
  920. list = &i915->mm.purge_list;
  921. else
  922. list = &i915->mm.shrink_list;
  923. list_move_tail(&obj->mm.link, list);
  924. }
  925. spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
  926. }
  927. /* if the object is no longer attached, discard its backing storage */
  928. if (obj->mm.madv == I915_MADV_DONTNEED &&
  929. !i915_gem_object_has_pages(obj))
  930. i915_gem_object_truncate(obj);
  931. args->retained = obj->mm.madv != __I915_MADV_PURGED;
  932. i915_gem_object_unlock(obj);
  933. out:
  934. i915_gem_object_put(obj);
  935. return err;
  936. }
  937. /*
  938. * A single pass should suffice to release all the freed objects (along most
  939. * call paths), but be a little more paranoid in that freeing the objects does
  940. * take a little amount of time, during which the rcu callbacks could have added
  941. * new objects into the freed list, and armed the work again.
  942. */
  943. void i915_gem_drain_freed_objects(struct drm_i915_private *i915)
  944. {
  945. while (atomic_read(&i915->mm.free_count)) {
  946. flush_work(&i915->mm.free_work);
  947. drain_workqueue(i915->bdev.wq);
  948. rcu_barrier();
  949. }
  950. }
  951. /*
  952. * Similar to objects above (see i915_gem_drain_freed-objects), in general we
  953. * have workers that are armed by RCU and then rearm themselves in their
  954. * callbacks. To be paranoid, we need to drain the workqueue a second time after
  955. * waiting for the RCU grace period so that we catch work queued via RCU from
  956. * the first pass. As neither drain_workqueue() nor flush_workqueue() report a
  957. * result, we make an assumption that we only don't require more than 3 passes
  958. * to catch all _recursive_ RCU delayed work.
  959. */
  960. void i915_gem_drain_workqueue(struct drm_i915_private *i915)
  961. {
  962. int i;
  963. for (i = 0; i < 3; i++) {
  964. flush_workqueue(i915->wq);
  965. rcu_barrier();
  966. i915_gem_drain_freed_objects(i915);
  967. }
  968. drain_workqueue(i915->wq);
  969. }
  970. int i915_gem_init(struct drm_i915_private *dev_priv)
  971. {
  972. struct intel_gt *gt;
  973. unsigned int i;
  974. int ret;
  975. /*
  976. * In the process of replacing cache_level with pat_index a tricky
  977. * dependency is created on the definition of the enum i915_cache_level.
  978. * In case this enum is changed, PTE encode would be broken.
  979. * Add a WARNING here. And remove when we completely quit using this
  980. * enum.
  981. */
  982. BUILD_BUG_ON(I915_CACHE_NONE != 0 ||
  983. I915_CACHE_LLC != 1 ||
  984. I915_CACHE_L3_LLC != 2 ||
  985. I915_CACHE_WT != 3 ||
  986. I915_MAX_CACHE_LEVEL != 4);
  987. /* We need to fallback to 4K pages if host doesn't support huge gtt. */
  988. if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
  989. RUNTIME_INFO(dev_priv)->page_sizes = I915_GTT_PAGE_SIZE_4K;
  990. for_each_gt(gt, dev_priv, i) {
  991. intel_uc_fetch_firmwares(&gt->uc);
  992. intel_wopcm_init(&gt->wopcm);
  993. if (GRAPHICS_VER(dev_priv) >= 8)
  994. setup_private_pat(gt);
  995. }
  996. ret = i915_init_ggtt(dev_priv);
  997. if (ret) {
  998. GEM_BUG_ON(ret == -EIO);
  999. goto err_unlock;
  1000. }
  1001. /*
  1002. * Despite its name intel_clock_gating_init applies both display
  1003. * clock gating workarounds; GT mmio workarounds and the occasional
  1004. * GT power context workaround. Worse, sometimes it includes a context
  1005. * register workaround which we need to apply before we record the
  1006. * default HW state for all contexts.
  1007. *
  1008. * FIXME: break up the workarounds and apply them at the right time!
  1009. */
  1010. intel_clock_gating_init(&dev_priv->drm);
  1011. for_each_gt(gt, dev_priv, i) {
  1012. ret = intel_gt_init(gt);
  1013. if (ret)
  1014. goto err_unlock;
  1015. }
  1016. /*
  1017. * Register engines early to ensure the engine list is in its final
  1018. * rb-tree form, lowering the amount of code that has to deal with
  1019. * the intermediate llist state.
  1020. */
  1021. intel_engines_driver_register(dev_priv);
  1022. return 0;
  1023. /*
  1024. * Unwinding is complicated by that we want to handle -EIO to mean
  1025. * disable GPU submission but keep KMS alive. We want to mark the
  1026. * HW as irrevisibly wedged, but keep enough state around that the
  1027. * driver doesn't explode during runtime.
  1028. */
  1029. err_unlock:
  1030. i915_gem_drain_workqueue(dev_priv);
  1031. if (ret != -EIO) {
  1032. for_each_gt(gt, dev_priv, i) {
  1033. intel_gt_driver_remove(gt);
  1034. intel_gt_driver_release(gt);
  1035. intel_uc_cleanup_firmwares(&gt->uc);
  1036. }
  1037. }
  1038. if (ret == -EIO) {
  1039. /*
  1040. * Allow engines or uC initialisation to fail by marking the GPU
  1041. * as wedged. But we only want to do this when the GPU is angry,
  1042. * for all other failure, such as an allocation failure, bail.
  1043. */
  1044. for_each_gt(gt, dev_priv, i) {
  1045. if (!intel_gt_is_wedged(gt)) {
  1046. i915_probe_error(dev_priv,
  1047. "Failed to initialize GPU, declaring it wedged!\n");
  1048. intel_gt_set_wedged(gt);
  1049. }
  1050. }
  1051. /* Minimal basic recovery for KMS */
  1052. ret = i915_ggtt_enable_hw(dev_priv);
  1053. i915_ggtt_resume(to_gt(dev_priv)->ggtt);
  1054. intel_clock_gating_init(&dev_priv->drm);
  1055. }
  1056. i915_gem_drain_freed_objects(dev_priv);
  1057. return ret;
  1058. }
  1059. void i915_gem_driver_register(struct drm_i915_private *i915)
  1060. {
  1061. i915_gem_driver_register__shrinker(i915);
  1062. }
  1063. void i915_gem_driver_unregister(struct drm_i915_private *i915)
  1064. {
  1065. i915_gem_driver_unregister__shrinker(i915);
  1066. }
  1067. void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
  1068. {
  1069. struct intel_gt *gt;
  1070. unsigned int i;
  1071. i915_gem_suspend_late(dev_priv);
  1072. for_each_gt(gt, dev_priv, i)
  1073. intel_gt_driver_remove(gt);
  1074. dev_priv->uabi_engines = RB_ROOT;
  1075. /* Flush any outstanding unpin_work. */
  1076. i915_gem_drain_workqueue(dev_priv);
  1077. }
  1078. void i915_gem_driver_release(struct drm_i915_private *dev_priv)
  1079. {
  1080. struct intel_gt *gt;
  1081. unsigned int i;
  1082. for_each_gt(gt, dev_priv, i) {
  1083. intel_gt_driver_release(gt);
  1084. intel_uc_cleanup_firmwares(&gt->uc);
  1085. }
  1086. /* Flush any outstanding work, including i915_gem_context.release_work. */
  1087. i915_gem_drain_workqueue(dev_priv);
  1088. drm_WARN_ON(&dev_priv->drm, !list_empty(&dev_priv->gem.contexts.list));
  1089. }
  1090. static void i915_gem_init__mm(struct drm_i915_private *i915)
  1091. {
  1092. spin_lock_init(&i915->mm.obj_lock);
  1093. init_llist_head(&i915->mm.free_list);
  1094. INIT_LIST_HEAD(&i915->mm.purge_list);
  1095. INIT_LIST_HEAD(&i915->mm.shrink_list);
  1096. i915_gem_init__objects(i915);
  1097. }
  1098. void i915_gem_init_early(struct drm_i915_private *dev_priv)
  1099. {
  1100. i915_gem_init__mm(dev_priv);
  1101. i915_gem_init__contexts(dev_priv);
  1102. spin_lock_init(&dev_priv->frontbuffer_lock);
  1103. }
  1104. void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
  1105. {
  1106. i915_gem_drain_workqueue(dev_priv);
  1107. GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
  1108. GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
  1109. drm_WARN_ON(&dev_priv->drm, dev_priv->mm.shrink_count);
  1110. }
  1111. int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
  1112. {
  1113. struct drm_i915_file_private *file_priv;
  1114. struct i915_drm_client *client;
  1115. int ret = -ENOMEM;
  1116. drm_dbg(&i915->drm, "\n");
  1117. file_priv = kzalloc_obj(*file_priv);
  1118. if (!file_priv)
  1119. goto err_alloc;
  1120. client = i915_drm_client_alloc();
  1121. if (!client)
  1122. goto err_client;
  1123. file->driver_priv = file_priv;
  1124. file_priv->i915 = i915;
  1125. file_priv->file = file;
  1126. file_priv->client = client;
  1127. file_priv->bsd_engine = -1;
  1128. file_priv->hang_timestamp = jiffies;
  1129. ret = i915_gem_context_open(i915, file);
  1130. if (ret)
  1131. goto err_context;
  1132. return 0;
  1133. err_context:
  1134. i915_drm_client_put(client);
  1135. err_client:
  1136. kfree(file_priv);
  1137. err_alloc:
  1138. return ret;
  1139. }
  1140. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1141. #include "selftests/mock_gem_device.c"
  1142. #include "selftests/i915_gem.c"
  1143. #endif