xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/radeon/radeon_mn.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2014 Advanced Micro Devices, Inc.
3*4882a593Smuzhiyun  * All Rights Reserved.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
6*4882a593Smuzhiyun  * copy of this software and associated documentation files (the
7*4882a593Smuzhiyun  * "Software"), to deal in the Software without restriction, including
8*4882a593Smuzhiyun  * without limitation the rights to use, copy, modify, merge, publish,
9*4882a593Smuzhiyun  * distribute, sub license, and/or sell copies of the Software, and to
10*4882a593Smuzhiyun  * permit persons to whom the Software is furnished to do so, subject to
11*4882a593Smuzhiyun  * the following conditions:
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16*4882a593Smuzhiyun  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17*4882a593Smuzhiyun  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18*4882a593Smuzhiyun  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19*4882a593Smuzhiyun  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the
22*4882a593Smuzhiyun  * next paragraph) shall be included in all copies or substantial portions
23*4882a593Smuzhiyun  * of the Software.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun  * Authors:
28*4882a593Smuzhiyun  *    Christian König <christian.koenig@amd.com>
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <linux/firmware.h>
32*4882a593Smuzhiyun #include <linux/module.h>
33*4882a593Smuzhiyun #include <linux/mmu_notifier.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include <drm/drm.h>
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #include "radeon.h"
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /**
40*4882a593Smuzhiyun  * radeon_mn_invalidate - callback to notify about mm change
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * @mn: our notifier
43*4882a593Smuzhiyun  * @range: the VMA under invalidation
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * We block for all BOs between start and end to be idle and
46*4882a593Smuzhiyun  * unmap them by move them into system domain again.
47*4882a593Smuzhiyun  */
radeon_mn_invalidate(struct mmu_interval_notifier * mn,const struct mmu_notifier_range * range,unsigned long cur_seq)48*4882a593Smuzhiyun static bool radeon_mn_invalidate(struct mmu_interval_notifier *mn,
49*4882a593Smuzhiyun 				 const struct mmu_notifier_range *range,
50*4882a593Smuzhiyun 				 unsigned long cur_seq)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	struct radeon_bo *bo = container_of(mn, struct radeon_bo, notifier);
53*4882a593Smuzhiyun 	struct ttm_operation_ctx ctx = { false, false };
54*4882a593Smuzhiyun 	long r;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	if (!bo->tbo.ttm || !radeon_ttm_tt_is_bound(bo->tbo.bdev, bo->tbo.ttm))
57*4882a593Smuzhiyun 		return true;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	if (!mmu_notifier_range_blockable(range))
60*4882a593Smuzhiyun 		return false;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	r = radeon_bo_reserve(bo, true);
63*4882a593Smuzhiyun 	if (r) {
64*4882a593Smuzhiyun 		DRM_ERROR("(%ld) failed to reserve user bo\n", r);
65*4882a593Smuzhiyun 		return true;
66*4882a593Smuzhiyun 	}
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	r = dma_resv_wait_timeout_rcu(bo->tbo.base.resv, true, false,
69*4882a593Smuzhiyun 				      MAX_SCHEDULE_TIMEOUT);
70*4882a593Smuzhiyun 	if (r <= 0)
71*4882a593Smuzhiyun 		DRM_ERROR("(%ld) failed to wait for user bo\n", r);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_CPU);
74*4882a593Smuzhiyun 	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
75*4882a593Smuzhiyun 	if (r)
76*4882a593Smuzhiyun 		DRM_ERROR("(%ld) failed to validate user bo\n", r);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	radeon_bo_unreserve(bo);
79*4882a593Smuzhiyun 	return true;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun static const struct mmu_interval_notifier_ops radeon_mn_ops = {
83*4882a593Smuzhiyun 	.invalidate = radeon_mn_invalidate,
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /**
87*4882a593Smuzhiyun  * radeon_mn_register - register a BO for notifier updates
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  * @bo: radeon buffer object
90*4882a593Smuzhiyun  * @addr: userptr addr we should monitor
91*4882a593Smuzhiyun  *
92*4882a593Smuzhiyun  * Registers an MMU notifier for the given BO at the specified address.
93*4882a593Smuzhiyun  * Returns 0 on success, -ERRNO if anything goes wrong.
94*4882a593Smuzhiyun  */
radeon_mn_register(struct radeon_bo * bo,unsigned long addr)95*4882a593Smuzhiyun int radeon_mn_register(struct radeon_bo *bo, unsigned long addr)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	int ret;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	ret = mmu_interval_notifier_insert(&bo->notifier, current->mm, addr,
100*4882a593Smuzhiyun 					   radeon_bo_size(bo), &radeon_mn_ops);
101*4882a593Smuzhiyun 	if (ret)
102*4882a593Smuzhiyun 		return ret;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	/*
105*4882a593Smuzhiyun 	 * FIXME: radeon appears to allow get_user_pages to run during
106*4882a593Smuzhiyun 	 * invalidate_range_start/end, which is not a safe way to read the
107*4882a593Smuzhiyun 	 * PTEs. It should use the mmu_interval_read_begin() scheme around the
108*4882a593Smuzhiyun 	 * get_user_pages to ensure that the PTEs are read properly
109*4882a593Smuzhiyun 	 */
110*4882a593Smuzhiyun 	mmu_interval_read_begin(&bo->notifier);
111*4882a593Smuzhiyun 	return 0;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun /**
115*4882a593Smuzhiyun  * radeon_mn_unregister - unregister a BO for notifier updates
116*4882a593Smuzhiyun  *
117*4882a593Smuzhiyun  * @bo: radeon buffer object
118*4882a593Smuzhiyun  *
119*4882a593Smuzhiyun  * Remove any registration of MMU notifier updates from the buffer object.
120*4882a593Smuzhiyun  */
radeon_mn_unregister(struct radeon_bo * bo)121*4882a593Smuzhiyun void radeon_mn_unregister(struct radeon_bo *bo)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	if (!bo->notifier.mm)
124*4882a593Smuzhiyun 		return;
125*4882a593Smuzhiyun 	mmu_interval_notifier_remove(&bo->notifier);
126*4882a593Smuzhiyun 	bo->notifier.mm = NULL;
127*4882a593Smuzhiyun }
128