1
2
3
4
5
6
7#include "display/intel_frontbuffer.h"
8#include "gt/intel_gt.h"
9
10#include "i915_drv.h"
11#include "i915_gem_clflush.h"
12#include "i915_gem_gtt.h"
13#include "i915_gem_ioctls.h"
14#include "i915_gem_object.h"
15#include "i915_vma.h"
16#include "i915_gem_lmem.h"
17#include "i915_gem_mman.h"
18
19static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
20{
21 return !(obj->cache_level == I915_CACHE_NONE ||
22 obj->cache_level == I915_CACHE_WT);
23}
24
25static void
26flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
27{
28 struct i915_vma *vma;
29
30 assert_object_held(obj);
31
32 if (!(obj->write_domain & flush_domains))
33 return;
34
35 switch (obj->write_domain) {
36 case I915_GEM_DOMAIN_GTT:
37 spin_lock(&obj->vma.lock);
38 for_each_ggtt_vma(vma, obj) {
39 if (i915_vma_unset_ggtt_write(vma))
40 intel_gt_flush_ggtt_writes(vma->vm->gt);
41 }
42 spin_unlock(&obj->vma.lock);
43
44 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
45 break;
46
47 case I915_GEM_DOMAIN_WC:
48 wmb();
49 break;
50
51 case I915_GEM_DOMAIN_CPU:
52 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
53 break;
54
55 case I915_GEM_DOMAIN_RENDER:
56 if (gpu_write_needs_clflush(obj))
57 obj->cache_dirty = true;
58 break;
59 }
60
61 obj->write_domain = 0;
62}
63
64static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
65{
66
67
68
69
70 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
71 if (obj->cache_dirty)
72 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
73 obj->write_domain = 0;
74}
75
76void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
77{
78 if (!i915_gem_object_is_framebuffer(obj))
79 return;
80
81 i915_gem_object_lock(obj, NULL);
82 __i915_gem_object_flush_for_display(obj);
83 i915_gem_object_unlock(obj);
84}
85
86void i915_gem_object_flush_if_display_locked(struct drm_i915_gem_object *obj)
87{
88 if (i915_gem_object_is_framebuffer(obj))
89 __i915_gem_object_flush_for_display(obj);
90}
91
92
93
94
95
96
97
98
99
100int
101i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
102{
103 int ret;
104
105 assert_object_held(obj);
106
107 ret = i915_gem_object_wait(obj,
108 I915_WAIT_INTERRUPTIBLE |
109 (write ? I915_WAIT_ALL : 0),
110 MAX_SCHEDULE_TIMEOUT);
111 if (ret)
112 return ret;
113
114 if (obj->write_domain == I915_GEM_DOMAIN_WC)
115 return 0;
116
117
118
119
120
121
122
123
124
125 ret = i915_gem_object_pin_pages(obj);
126 if (ret)
127 return ret;
128
129 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
130
131
132
133
134
135 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
136 mb();
137
138
139
140
141 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
142 obj->read_domains |= I915_GEM_DOMAIN_WC;
143 if (write) {
144 obj->read_domains = I915_GEM_DOMAIN_WC;
145 obj->write_domain = I915_GEM_DOMAIN_WC;
146 obj->mm.dirty = true;
147 }
148
149 i915_gem_object_unpin_pages(obj);
150 return 0;
151}
152
153
154
155
156
157
158
159
160
161int
162i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
163{
164 int ret;
165
166 assert_object_held(obj);
167
168 ret = i915_gem_object_wait(obj,
169 I915_WAIT_INTERRUPTIBLE |
170 (write ? I915_WAIT_ALL : 0),
171 MAX_SCHEDULE_TIMEOUT);
172 if (ret)
173 return ret;
174
175 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
176 return 0;
177
178
179
180
181
182
183
184
185
186 ret = i915_gem_object_pin_pages(obj);
187 if (ret)
188 return ret;
189
190 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
191
192
193
194
195
196 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
197 mb();
198
199
200
201
202 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
203 obj->read_domains |= I915_GEM_DOMAIN_GTT;
204 if (write) {
205 struct i915_vma *vma;
206
207 obj->read_domains = I915_GEM_DOMAIN_GTT;
208 obj->write_domain = I915_GEM_DOMAIN_GTT;
209 obj->mm.dirty = true;
210
211 spin_lock(&obj->vma.lock);
212 for_each_ggtt_vma(vma, obj)
213 if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
214 i915_vma_set_ggtt_write(vma);
215 spin_unlock(&obj->vma.lock);
216 }
217
218 i915_gem_object_unpin_pages(obj);
219 return 0;
220}
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
238 enum i915_cache_level cache_level)
239{
240 int ret;
241
242 if (obj->cache_level == cache_level)
243 return 0;
244
245 ret = i915_gem_object_wait(obj,
246 I915_WAIT_INTERRUPTIBLE |
247 I915_WAIT_ALL,
248 MAX_SCHEDULE_TIMEOUT);
249 if (ret)
250 return ret;
251
252
253 if (obj->cache_level != cache_level) {
254 i915_gem_object_set_cache_coherency(obj, cache_level);
255 obj->cache_dirty = true;
256 }
257
258
259 return i915_gem_object_unbind(obj,
260 I915_GEM_OBJECT_UNBIND_ACTIVE |
261 I915_GEM_OBJECT_UNBIND_BARRIER);
262}
263
264int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
265 struct drm_file *file)
266{
267 struct drm_i915_gem_caching *args = data;
268 struct drm_i915_gem_object *obj;
269 int err = 0;
270
271 rcu_read_lock();
272 obj = i915_gem_object_lookup_rcu(file, args->handle);
273 if (!obj) {
274 err = -ENOENT;
275 goto out;
276 }
277
278 switch (obj->cache_level) {
279 case I915_CACHE_LLC:
280 case I915_CACHE_L3_LLC:
281 args->caching = I915_CACHING_CACHED;
282 break;
283
284 case I915_CACHE_WT:
285 args->caching = I915_CACHING_DISPLAY;
286 break;
287
288 default:
289 args->caching = I915_CACHING_NONE;
290 break;
291 }
292out:
293 rcu_read_unlock();
294 return err;
295}
296
297int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
298 struct drm_file *file)
299{
300 struct drm_i915_private *i915 = to_i915(dev);
301 struct drm_i915_gem_caching *args = data;
302 struct drm_i915_gem_object *obj;
303 enum i915_cache_level level;
304 int ret = 0;
305
306 switch (args->caching) {
307 case I915_CACHING_NONE:
308 level = I915_CACHE_NONE;
309 break;
310 case I915_CACHING_CACHED:
311
312
313
314
315
316
317 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
318 return -ENODEV;
319
320 level = I915_CACHE_LLC;
321 break;
322 case I915_CACHING_DISPLAY:
323 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
324 break;
325 default:
326 return -EINVAL;
327 }
328
329 obj = i915_gem_object_lookup(file, args->handle);
330 if (!obj)
331 return -ENOENT;
332
333
334
335
336
337 if (i915_gem_object_is_proxy(obj)) {
338 ret = -ENXIO;
339 goto out;
340 }
341
342 ret = i915_gem_object_lock_interruptible(obj, NULL);
343 if (ret)
344 goto out;
345
346 ret = i915_gem_object_set_cache_level(obj, level);
347 i915_gem_object_unlock(obj);
348
349out:
350 i915_gem_object_put(obj);
351 return ret;
352}
353
354
355
356
357
358
359
360struct i915_vma *
361i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
362 u32 alignment,
363 const struct i915_ggtt_view *view,
364 unsigned int flags)
365{
366 struct drm_i915_private *i915 = to_i915(obj->base.dev);
367 struct i915_gem_ww_ctx ww;
368 struct i915_vma *vma;
369 int ret;
370
371
372 if (HAS_LMEM(i915) && !i915_gem_object_is_lmem(obj))
373 return ERR_PTR(-EINVAL);
374
375 i915_gem_ww_ctx_init(&ww, true);
376retry:
377 ret = i915_gem_object_lock(obj, &ww);
378 if (ret)
379 goto err;
380
381
382
383
384
385
386
387
388
389
390 ret = i915_gem_object_set_cache_level(obj,
391 HAS_WT(i915) ?
392 I915_CACHE_WT : I915_CACHE_NONE);
393 if (ret)
394 goto err;
395
396
397
398
399
400
401
402
403
404 vma = ERR_PTR(-ENOSPC);
405 if ((flags & PIN_MAPPABLE) == 0 &&
406 (!view || view->type == I915_GGTT_VIEW_NORMAL))
407 vma = i915_gem_object_ggtt_pin_ww(obj, &ww, view, 0, alignment,
408 flags | PIN_MAPPABLE |
409 PIN_NONBLOCK);
410 if (IS_ERR(vma) && vma != ERR_PTR(-EDEADLK))
411 vma = i915_gem_object_ggtt_pin_ww(obj, &ww, view, 0,
412 alignment, flags);
413 if (IS_ERR(vma)) {
414 ret = PTR_ERR(vma);
415 goto err;
416 }
417
418 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
419 i915_vma_mark_scanout(vma);
420
421 i915_gem_object_flush_if_display_locked(obj);
422
423err:
424 if (ret == -EDEADLK) {
425 ret = i915_gem_ww_ctx_backoff(&ww);
426 if (!ret)
427 goto retry;
428 }
429 i915_gem_ww_ctx_fini(&ww);
430
431 if (ret)
432 return ERR_PTR(ret);
433
434 return vma;
435}
436
437
438
439
440
441
442
443
444
445int
446i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
447{
448 int ret;
449
450 assert_object_held(obj);
451
452 ret = i915_gem_object_wait(obj,
453 I915_WAIT_INTERRUPTIBLE |
454 (write ? I915_WAIT_ALL : 0),
455 MAX_SCHEDULE_TIMEOUT);
456 if (ret)
457 return ret;
458
459 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
460
461
462 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
463 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
464 obj->read_domains |= I915_GEM_DOMAIN_CPU;
465 }
466
467
468
469
470 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
471
472
473
474
475 if (write)
476 __start_cpu_write(obj);
477
478 return 0;
479}
480
481
482
483
484
485
486
487
488int
489i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
490 struct drm_file *file)
491{
492 struct drm_i915_gem_set_domain *args = data;
493 struct drm_i915_gem_object *obj;
494 u32 read_domains = args->read_domains;
495 u32 write_domain = args->write_domain;
496 int err;
497
498
499 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
500 return -EINVAL;
501
502
503
504
505
506 if (write_domain && read_domains != write_domain)
507 return -EINVAL;
508
509 if (!read_domains)
510 return 0;
511
512 obj = i915_gem_object_lookup(file, args->handle);
513 if (!obj)
514 return -ENOENT;
515
516
517
518
519
520
521 err = i915_gem_object_wait(obj,
522 I915_WAIT_INTERRUPTIBLE |
523 I915_WAIT_PRIORITY |
524 (write_domain ? I915_WAIT_ALL : 0),
525 MAX_SCHEDULE_TIMEOUT);
526 if (err)
527 goto out;
528
529
530
531
532
533
534
535 if (i915_gem_object_is_proxy(obj)) {
536 err = -ENXIO;
537 goto out;
538 }
539
540
541
542
543
544
545
546
547
548
549 err = i915_gem_object_pin_pages(obj);
550 if (err)
551 goto out;
552
553
554
555
556
557
558
559
560
561
562
563 if (READ_ONCE(obj->write_domain) == read_domains)
564 goto out_unpin;
565
566 err = i915_gem_object_lock_interruptible(obj, NULL);
567 if (err)
568 goto out_unpin;
569
570 if (read_domains & I915_GEM_DOMAIN_WC)
571 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
572 else if (read_domains & I915_GEM_DOMAIN_GTT)
573 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
574 else
575 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
576
577 i915_gem_object_unlock(obj);
578
579 if (write_domain)
580 i915_gem_object_invalidate_frontbuffer(obj, ORIGIN_CPU);
581
582out_unpin:
583 i915_gem_object_unpin_pages(obj);
584out:
585 i915_gem_object_put(obj);
586 return err;
587}
588
589
590
591
592
593
594int i915_gem_object_prepare_read(struct drm_i915_gem_object *obj,
595 unsigned int *needs_clflush)
596{
597 int ret;
598
599 *needs_clflush = 0;
600 if (!i915_gem_object_has_struct_page(obj))
601 return -ENODEV;
602
603 assert_object_held(obj);
604
605 ret = i915_gem_object_wait(obj,
606 I915_WAIT_INTERRUPTIBLE,
607 MAX_SCHEDULE_TIMEOUT);
608 if (ret)
609 return ret;
610
611 ret = i915_gem_object_pin_pages(obj);
612 if (ret)
613 return ret;
614
615 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
616 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
617 ret = i915_gem_object_set_to_cpu_domain(obj, false);
618 if (ret)
619 goto err_unpin;
620 else
621 goto out;
622 }
623
624 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
625
626
627
628
629
630
631 if (!obj->cache_dirty &&
632 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
633 *needs_clflush = CLFLUSH_BEFORE;
634
635out:
636
637 return 0;
638
639err_unpin:
640 i915_gem_object_unpin_pages(obj);
641 return ret;
642}
643
644int i915_gem_object_prepare_write(struct drm_i915_gem_object *obj,
645 unsigned int *needs_clflush)
646{
647 int ret;
648
649 *needs_clflush = 0;
650 if (!i915_gem_object_has_struct_page(obj))
651 return -ENODEV;
652
653 assert_object_held(obj);
654
655 ret = i915_gem_object_wait(obj,
656 I915_WAIT_INTERRUPTIBLE |
657 I915_WAIT_ALL,
658 MAX_SCHEDULE_TIMEOUT);
659 if (ret)
660 return ret;
661
662 ret = i915_gem_object_pin_pages(obj);
663 if (ret)
664 return ret;
665
666 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
667 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
668 ret = i915_gem_object_set_to_cpu_domain(obj, true);
669 if (ret)
670 goto err_unpin;
671 else
672 goto out;
673 }
674
675 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
676
677
678
679
680
681
682 if (!obj->cache_dirty) {
683 *needs_clflush |= CLFLUSH_AFTER;
684
685
686
687
688
689 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
690 *needs_clflush |= CLFLUSH_BEFORE;
691 }
692
693out:
694 i915_gem_object_invalidate_frontbuffer(obj, ORIGIN_CPU);
695 obj->mm.dirty = true;
696
697 return 0;
698
699err_unpin:
700 i915_gem_object_unpin_pages(obj);
701 return ret;
702}
703