| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright 2009 Jerome Glisse. | 
 | 3 |  * All Rights Reserved. | 
 | 4 |  * | 
 | 5 |  * Permission is hereby granted, free of charge, to any person obtaining a | 
 | 6 |  * copy of this software and associated documentation files (the | 
 | 7 |  * "Software"), to deal in the Software without restriction, including | 
 | 8 |  * without limitation the rights to use, copy, modify, merge, publish, | 
 | 9 |  * distribute, sub license, and/or sell copies of the Software, and to | 
 | 10 |  * permit persons to whom the Software is furnished to do so, subject to | 
 | 11 |  * the following conditions: | 
 | 12 |  * | 
 | 13 |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
 | 14 |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
 | 15 |  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL | 
 | 16 |  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, | 
 | 17 |  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | 
 | 18 |  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | 
 | 19 |  * USE OR OTHER DEALINGS IN THE SOFTWARE. | 
 | 20 |  * | 
 | 21 |  * The above copyright notice and this permission notice (including the | 
 | 22 |  * next paragraph) shall be included in all copies or substantial portions | 
 | 23 |  * of the Software. | 
 | 24 |  * | 
 | 25 |  */ | 
 | 26 | /* | 
 | 27 |  * Authors: | 
 | 28 |  *    Jerome Glisse <glisse@freedesktop.org> | 
 | 29 |  *    Dave Airlie | 
 | 30 |  */ | 
 | 31 | #include <linux/seq_file.h> | 
 | 32 | #include <linux/atomic.h> | 
 | 33 | #include <linux/wait.h> | 
 | 34 | #include <linux/kref.h> | 
 | 35 | #include <linux/slab.h> | 
 | 36 | #include <linux/firmware.h> | 
 | 37 | #include <drm/drmP.h> | 
 | 38 | #include "amdgpu.h" | 
 | 39 | #include "amdgpu_trace.h" | 
 | 40 |  | 
 | 41 | /* | 
 | 42 |  * Fences | 
 | 43 |  * Fences mark an event in the GPUs pipeline and are used | 
 | 44 |  * for GPU/CPU synchronization.  When the fence is written, | 
 | 45 |  * it is expected that all buffers associated with that fence | 
 | 46 |  * are no longer in use by the associated ring on the GPU and | 
 | 47 |  * that the the relevant GPU caches have been flushed. | 
 | 48 |  */ | 
 | 49 |  | 
 | 50 | /** | 
 | 51 |  * amdgpu_fence_write - write a fence value | 
 | 52 |  * | 
 | 53 |  * @ring: ring the fence is associated with | 
 | 54 |  * @seq: sequence number to write | 
 | 55 |  * | 
 | 56 |  * Writes a fence value to memory (all asics). | 
 | 57 |  */ | 
 | 58 | static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq) | 
 | 59 | { | 
 | 60 | 	struct amdgpu_fence_driver *drv = &ring->fence_drv; | 
 | 61 |  | 
 | 62 | 	if (drv->cpu_addr) | 
 | 63 | 		*drv->cpu_addr = cpu_to_le32(seq); | 
 | 64 | } | 
 | 65 |  | 
 | 66 | /** | 
 | 67 |  * amdgpu_fence_read - read a fence value | 
 | 68 |  * | 
 | 69 |  * @ring: ring the fence is associated with | 
 | 70 |  * | 
 | 71 |  * Reads a fence value from memory (all asics). | 
 | 72 |  * Returns the value of the fence read from memory. | 
 | 73 |  */ | 
 | 74 | static u32 amdgpu_fence_read(struct amdgpu_ring *ring) | 
 | 75 | { | 
 | 76 | 	struct amdgpu_fence_driver *drv = &ring->fence_drv; | 
 | 77 | 	u32 seq = 0; | 
 | 78 |  | 
 | 79 | 	if (drv->cpu_addr) | 
 | 80 | 		seq = le32_to_cpu(*drv->cpu_addr); | 
 | 81 | 	else | 
 | 82 | 		seq = lower_32_bits(atomic64_read(&drv->last_seq)); | 
 | 83 |  | 
 | 84 | 	return seq; | 
 | 85 | } | 
 | 86 |  | 
 | 87 | /** | 
 | 88 |  * amdgpu_fence_schedule_check - schedule lockup check | 
 | 89 |  * | 
 | 90 |  * @ring: pointer to struct amdgpu_ring | 
 | 91 |  * | 
 | 92 |  * Queues a delayed work item to check for lockups. | 
 | 93 |  */ | 
 | 94 | static void amdgpu_fence_schedule_check(struct amdgpu_ring *ring) | 
 | 95 | { | 
 | 96 | 	/* | 
 | 97 | 	 * Do not reset the timer here with mod_delayed_work, | 
 | 98 | 	 * this can livelock in an interaction with TTM delayed destroy. | 
 | 99 | 	 */ | 
 | 100 | 	queue_delayed_work(system_power_efficient_wq, | 
 | 101 | 		&ring->fence_drv.lockup_work, | 
 | 102 | 		AMDGPU_FENCE_JIFFIES_TIMEOUT); | 
 | 103 | } | 
 | 104 |  | 
 | 105 | /** | 
 | 106 |  * amdgpu_fence_emit - emit a fence on the requested ring | 
 | 107 |  * | 
 | 108 |  * @ring: ring the fence is associated with | 
 | 109 |  * @owner: creator of the fence | 
 | 110 |  * @fence: amdgpu fence object | 
 | 111 |  * | 
 | 112 |  * Emits a fence command on the requested ring (all asics). | 
 | 113 |  * Returns 0 on success, -ENOMEM on failure. | 
 | 114 |  */ | 
 | 115 | int amdgpu_fence_emit(struct amdgpu_ring *ring, void *owner, | 
 | 116 | 		      struct amdgpu_fence **fence) | 
 | 117 | { | 
 | 118 | 	struct amdgpu_device *adev = ring->adev; | 
 | 119 |  | 
 | 120 | 	/* we are protected by the ring emission mutex */ | 
 | 121 | 	*fence = kmalloc(sizeof(struct amdgpu_fence), GFP_KERNEL); | 
 | 122 | 	if ((*fence) == NULL) { | 
 | 123 | 		return -ENOMEM; | 
 | 124 | 	} | 
 | 125 | 	(*fence)->seq = ++ring->fence_drv.sync_seq[ring->idx]; | 
 | 126 | 	(*fence)->ring = ring; | 
 | 127 | 	(*fence)->owner = owner; | 
 | 128 | 	fence_init(&(*fence)->base, &amdgpu_fence_ops, | 
 | 129 | 		&adev->fence_queue.lock, adev->fence_context + ring->idx, | 
 | 130 | 		(*fence)->seq); | 
| Chunming Zhou | 890ee23 | 2015-06-01 14:35:03 +0800 | [diff] [blame] | 131 | 	amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr, | 
 | 132 | 			       (*fence)->seq, | 
 | 133 | 			       AMDGPU_FENCE_FLAG_INT); | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 134 | 	trace_amdgpu_fence_emit(ring->adev->ddev, ring->idx, (*fence)->seq); | 
 | 135 | 	return 0; | 
 | 136 | } | 
 | 137 |  | 
 | 138 | /** | 
 | 139 |  * amdgpu_fence_check_signaled - callback from fence_queue | 
 | 140 |  * | 
 | 141 |  * this function is called with fence_queue lock held, which is also used | 
 | 142 |  * for the fence locking itself, so unlocked variants are used for | 
 | 143 |  * fence_signal, and remove_wait_queue. | 
 | 144 |  */ | 
 | 145 | static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key) | 
 | 146 | { | 
 | 147 | 	struct amdgpu_fence *fence; | 
 | 148 | 	struct amdgpu_device *adev; | 
 | 149 | 	u64 seq; | 
 | 150 | 	int ret; | 
 | 151 |  | 
 | 152 | 	fence = container_of(wait, struct amdgpu_fence, fence_wake); | 
 | 153 | 	adev = fence->ring->adev; | 
 | 154 |  | 
 | 155 | 	/* | 
 | 156 | 	 * We cannot use amdgpu_fence_process here because we're already | 
 | 157 | 	 * in the waitqueue, in a call from wake_up_all. | 
 | 158 | 	 */ | 
 | 159 | 	seq = atomic64_read(&fence->ring->fence_drv.last_seq); | 
 | 160 | 	if (seq >= fence->seq) { | 
 | 161 | 		ret = fence_signal_locked(&fence->base); | 
 | 162 | 		if (!ret) | 
 | 163 | 			FENCE_TRACE(&fence->base, "signaled from irq context\n"); | 
 | 164 | 		else | 
 | 165 | 			FENCE_TRACE(&fence->base, "was already signaled\n"); | 
 | 166 |  | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 167 | 		__remove_wait_queue(&adev->fence_queue, &fence->fence_wake); | 
 | 168 | 		fence_put(&fence->base); | 
 | 169 | 	} else | 
 | 170 | 		FENCE_TRACE(&fence->base, "pending\n"); | 
 | 171 | 	return 0; | 
 | 172 | } | 
 | 173 |  | 
 | 174 | /** | 
 | 175 |  * amdgpu_fence_activity - check for fence activity | 
 | 176 |  * | 
 | 177 |  * @ring: pointer to struct amdgpu_ring | 
 | 178 |  * | 
 | 179 |  * Checks the current fence value and calculates the last | 
 | 180 |  * signalled fence value. Returns true if activity occured | 
 | 181 |  * on the ring, and the fence_queue should be waken up. | 
 | 182 |  */ | 
 | 183 | static bool amdgpu_fence_activity(struct amdgpu_ring *ring) | 
 | 184 | { | 
 | 185 | 	uint64_t seq, last_seq, last_emitted; | 
 | 186 | 	unsigned count_loop = 0; | 
 | 187 | 	bool wake = false; | 
 | 188 |  | 
 | 189 | 	/* Note there is a scenario here for an infinite loop but it's | 
 | 190 | 	 * very unlikely to happen. For it to happen, the current polling | 
 | 191 | 	 * process need to be interrupted by another process and another | 
 | 192 | 	 * process needs to update the last_seq btw the atomic read and | 
 | 193 | 	 * xchg of the current process. | 
 | 194 | 	 * | 
 | 195 | 	 * More over for this to go in infinite loop there need to be | 
| Jammy Zhou | 86c2b79 | 2015-05-13 22:52:42 +0800 | [diff] [blame] | 196 | 	 * continuously new fence signaled ie amdgpu_fence_read needs | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 197 | 	 * to return a different value each time for both the currently | 
 | 198 | 	 * polling process and the other process that xchg the last_seq | 
 | 199 | 	 * btw atomic read and xchg of the current process. And the | 
 | 200 | 	 * value the other process set as last seq must be higher than | 
 | 201 | 	 * the seq value we just read. Which means that current process | 
| Jammy Zhou | 86c2b79 | 2015-05-13 22:52:42 +0800 | [diff] [blame] | 202 | 	 * need to be interrupted after amdgpu_fence_read and before | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 203 | 	 * atomic xchg. | 
 | 204 | 	 * | 
 | 205 | 	 * To be even more safe we count the number of time we loop and | 
 | 206 | 	 * we bail after 10 loop just accepting the fact that we might | 
 | 207 | 	 * have temporarly set the last_seq not to the true real last | 
 | 208 | 	 * seq but to an older one. | 
 | 209 | 	 */ | 
 | 210 | 	last_seq = atomic64_read(&ring->fence_drv.last_seq); | 
 | 211 | 	do { | 
 | 212 | 		last_emitted = ring->fence_drv.sync_seq[ring->idx]; | 
 | 213 | 		seq = amdgpu_fence_read(ring); | 
 | 214 | 		seq |= last_seq & 0xffffffff00000000LL; | 
 | 215 | 		if (seq < last_seq) { | 
 | 216 | 			seq &= 0xffffffff; | 
 | 217 | 			seq |= last_emitted & 0xffffffff00000000LL; | 
 | 218 | 		} | 
 | 219 |  | 
 | 220 | 		if (seq <= last_seq || seq > last_emitted) { | 
 | 221 | 			break; | 
 | 222 | 		} | 
 | 223 | 		/* If we loop over we don't want to return without | 
 | 224 | 		 * checking if a fence is signaled as it means that the | 
 | 225 | 		 * seq we just read is different from the previous on. | 
 | 226 | 		 */ | 
 | 227 | 		wake = true; | 
 | 228 | 		last_seq = seq; | 
 | 229 | 		if ((count_loop++) > 10) { | 
 | 230 | 			/* We looped over too many time leave with the | 
 | 231 | 			 * fact that we might have set an older fence | 
 | 232 | 			 * seq then the current real last seq as signaled | 
 | 233 | 			 * by the hw. | 
 | 234 | 			 */ | 
 | 235 | 			break; | 
 | 236 | 		} | 
 | 237 | 	} while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq); | 
 | 238 |  | 
 | 239 | 	if (seq < last_emitted) | 
 | 240 | 		amdgpu_fence_schedule_check(ring); | 
 | 241 |  | 
 | 242 | 	return wake; | 
 | 243 | } | 
 | 244 |  | 
 | 245 | /** | 
 | 246 |  * amdgpu_fence_check_lockup - check for hardware lockup | 
 | 247 |  * | 
 | 248 |  * @work: delayed work item | 
 | 249 |  * | 
 | 250 |  * Checks for fence activity and if there is none probe | 
 | 251 |  * the hardware if a lockup occured. | 
 | 252 |  */ | 
 | 253 | static void amdgpu_fence_check_lockup(struct work_struct *work) | 
 | 254 | { | 
 | 255 | 	struct amdgpu_fence_driver *fence_drv; | 
 | 256 | 	struct amdgpu_ring *ring; | 
 | 257 |  | 
 | 258 | 	fence_drv = container_of(work, struct amdgpu_fence_driver, | 
 | 259 | 				lockup_work.work); | 
 | 260 | 	ring = fence_drv->ring; | 
 | 261 |  | 
 | 262 | 	if (!down_read_trylock(&ring->adev->exclusive_lock)) { | 
 | 263 | 		/* just reschedule the check if a reset is going on */ | 
 | 264 | 		amdgpu_fence_schedule_check(ring); | 
 | 265 | 		return; | 
 | 266 | 	} | 
 | 267 |  | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 268 | 	if (amdgpu_fence_activity(ring)) | 
 | 269 | 		wake_up_all(&ring->adev->fence_queue); | 
 | 270 | 	else if (amdgpu_ring_is_lockup(ring)) { | 
 | 271 | 		/* good news we believe it's a lockup */ | 
 | 272 | 		dev_warn(ring->adev->dev, "GPU lockup (current fence id " | 
 | 273 | 			"0x%016llx last fence id 0x%016llx on ring %d)\n", | 
 | 274 | 			(uint64_t)atomic64_read(&fence_drv->last_seq), | 
 | 275 | 			fence_drv->sync_seq[ring->idx], ring->idx); | 
 | 276 |  | 
 | 277 | 		/* remember that we need an reset */ | 
 | 278 | 		ring->adev->needs_reset = true; | 
 | 279 | 		wake_up_all(&ring->adev->fence_queue); | 
 | 280 | 	} | 
 | 281 | 	up_read(&ring->adev->exclusive_lock); | 
 | 282 | } | 
 | 283 |  | 
 | 284 | /** | 
 | 285 |  * amdgpu_fence_process - process a fence | 
 | 286 |  * | 
 | 287 |  * @adev: amdgpu_device pointer | 
 | 288 |  * @ring: ring index the fence is associated with | 
 | 289 |  * | 
 | 290 |  * Checks the current fence value and wakes the fence queue | 
 | 291 |  * if the sequence number has increased (all asics). | 
 | 292 |  */ | 
 | 293 | void amdgpu_fence_process(struct amdgpu_ring *ring) | 
 | 294 | { | 
 | 295 | 	uint64_t seq, last_seq, last_emitted; | 
 | 296 | 	unsigned count_loop = 0; | 
 | 297 | 	bool wake = false; | 
| Chunming Zhou | 176e1ab | 2015-07-24 10:49:47 +0800 | [diff] [blame] | 298 | 	unsigned long irqflags; | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 299 |  | 
 | 300 | 	/* Note there is a scenario here for an infinite loop but it's | 
 | 301 | 	 * very unlikely to happen. For it to happen, the current polling | 
 | 302 | 	 * process need to be interrupted by another process and another | 
 | 303 | 	 * process needs to update the last_seq btw the atomic read and | 
 | 304 | 	 * xchg of the current process. | 
 | 305 | 	 * | 
 | 306 | 	 * More over for this to go in infinite loop there need to be | 
 | 307 | 	 * continuously new fence signaled ie amdgpu_fence_read needs | 
 | 308 | 	 * to return a different value each time for both the currently | 
 | 309 | 	 * polling process and the other process that xchg the last_seq | 
 | 310 | 	 * btw atomic read and xchg of the current process. And the | 
 | 311 | 	 * value the other process set as last seq must be higher than | 
 | 312 | 	 * the seq value we just read. Which means that current process | 
 | 313 | 	 * need to be interrupted after amdgpu_fence_read and before | 
 | 314 | 	 * atomic xchg. | 
 | 315 | 	 * | 
 | 316 | 	 * To be even more safe we count the number of time we loop and | 
 | 317 | 	 * we bail after 10 loop just accepting the fact that we might | 
 | 318 | 	 * have temporarly set the last_seq not to the true real last | 
 | 319 | 	 * seq but to an older one. | 
 | 320 | 	 */ | 
| Chunming Zhou | 176e1ab | 2015-07-24 10:49:47 +0800 | [diff] [blame] | 321 | 	spin_lock_irqsave(&ring->fence_lock, irqflags); | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 322 | 	last_seq = atomic64_read(&ring->fence_drv.last_seq); | 
 | 323 | 	do { | 
 | 324 | 		last_emitted = ring->fence_drv.sync_seq[ring->idx]; | 
 | 325 | 		seq = amdgpu_fence_read(ring); | 
 | 326 | 		seq |= last_seq & 0xffffffff00000000LL; | 
 | 327 | 		if (seq < last_seq) { | 
 | 328 | 			seq &= 0xffffffff; | 
 | 329 | 			seq |= last_emitted & 0xffffffff00000000LL; | 
 | 330 | 		} | 
 | 331 |  | 
 | 332 | 		if (seq <= last_seq || seq > last_emitted) { | 
 | 333 | 			break; | 
 | 334 | 		} | 
 | 335 | 		/* If we loop over we don't want to return without | 
 | 336 | 		 * checking if a fence is signaled as it means that the | 
 | 337 | 		 * seq we just read is different from the previous on. | 
 | 338 | 		 */ | 
 | 339 | 		wake = true; | 
 | 340 | 		last_seq = seq; | 
 | 341 | 		if ((count_loop++) > 10) { | 
 | 342 | 			/* We looped over too many time leave with the | 
 | 343 | 			 * fact that we might have set an older fence | 
 | 344 | 			 * seq then the current real last seq as signaled | 
 | 345 | 			 * by the hw. | 
 | 346 | 			 */ | 
 | 347 | 			break; | 
 | 348 | 		} | 
 | 349 | 	} while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq); | 
 | 350 |  | 
| Chunming Zhou | e0d8f3c | 2015-07-21 17:43:41 +0800 | [diff] [blame] | 351 | 	if (wake) { | 
 | 352 | 		if (amdgpu_enable_scheduler) { | 
 | 353 | 			uint64_t handled_seq = | 
 | 354 | 				amd_sched_get_handled_seq(ring->scheduler); | 
 | 355 | 			uint64_t latest_seq = | 
 | 356 | 				atomic64_read(&ring->fence_drv.last_seq); | 
 | 357 | 			if (handled_seq == latest_seq) { | 
 | 358 | 				DRM_ERROR("ring %d, EOP without seq update (lastest_seq=%llu)\n", | 
 | 359 | 					  ring->idx, latest_seq); | 
| Chunming Zhou | 176e1ab | 2015-07-24 10:49:47 +0800 | [diff] [blame] | 360 | 				goto exit; | 
| Chunming Zhou | e0d8f3c | 2015-07-21 17:43:41 +0800 | [diff] [blame] | 361 | 			} | 
 | 362 | 			do { | 
 | 363 | 				amd_sched_isr(ring->scheduler); | 
 | 364 | 			} while (amd_sched_get_handled_seq(ring->scheduler) < latest_seq); | 
 | 365 | 		} | 
 | 366 |  | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 367 | 		wake_up_all(&ring->adev->fence_queue); | 
| Chunming Zhou | e0d8f3c | 2015-07-21 17:43:41 +0800 | [diff] [blame] | 368 | 	} | 
| Chunming Zhou | 176e1ab | 2015-07-24 10:49:47 +0800 | [diff] [blame] | 369 | exit: | 
 | 370 | 	spin_unlock_irqrestore(&ring->fence_lock, irqflags); | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 371 | } | 
 | 372 |  | 
 | 373 | /** | 
 | 374 |  * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled | 
 | 375 |  * | 
 | 376 |  * @ring: ring the fence is associated with | 
 | 377 |  * @seq: sequence number | 
 | 378 |  * | 
 | 379 |  * Check if the last signaled fence sequnce number is >= the requested | 
 | 380 |  * sequence number (all asics). | 
 | 381 |  * Returns true if the fence has signaled (current fence value | 
 | 382 |  * is >= requested value) or false if it has not (current fence | 
 | 383 |  * value is < the requested value.  Helper function for | 
 | 384 |  * amdgpu_fence_signaled(). | 
 | 385 |  */ | 
 | 386 | static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq) | 
 | 387 | { | 
 | 388 | 	if (atomic64_read(&ring->fence_drv.last_seq) >= seq) | 
 | 389 | 		return true; | 
 | 390 |  | 
 | 391 | 	/* poll new last sequence at least once */ | 
 | 392 | 	amdgpu_fence_process(ring); | 
 | 393 | 	if (atomic64_read(&ring->fence_drv.last_seq) >= seq) | 
 | 394 | 		return true; | 
 | 395 |  | 
 | 396 | 	return false; | 
 | 397 | } | 
 | 398 |  | 
 | 399 | static bool amdgpu_fence_is_signaled(struct fence *f) | 
 | 400 | { | 
 | 401 | 	struct amdgpu_fence *fence = to_amdgpu_fence(f); | 
 | 402 | 	struct amdgpu_ring *ring = fence->ring; | 
 | 403 | 	struct amdgpu_device *adev = ring->adev; | 
 | 404 |  | 
 | 405 | 	if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) | 
 | 406 | 		return true; | 
 | 407 |  | 
 | 408 | 	if (down_read_trylock(&adev->exclusive_lock)) { | 
 | 409 | 		amdgpu_fence_process(ring); | 
 | 410 | 		up_read(&adev->exclusive_lock); | 
 | 411 |  | 
 | 412 | 		if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) | 
 | 413 | 			return true; | 
 | 414 | 	} | 
 | 415 | 	return false; | 
 | 416 | } | 
 | 417 |  | 
 | 418 | /** | 
 | 419 |  * amdgpu_fence_enable_signaling - enable signalling on fence | 
 | 420 |  * @fence: fence | 
 | 421 |  * | 
 | 422 |  * This function is called with fence_queue lock held, and adds a callback | 
 | 423 |  * to fence_queue that checks if this fence is signaled, and if so it | 
 | 424 |  * signals the fence and removes itself. | 
 | 425 |  */ | 
 | 426 | static bool amdgpu_fence_enable_signaling(struct fence *f) | 
 | 427 | { | 
 | 428 | 	struct amdgpu_fence *fence = to_amdgpu_fence(f); | 
 | 429 | 	struct amdgpu_ring *ring = fence->ring; | 
 | 430 | 	struct amdgpu_device *adev = ring->adev; | 
 | 431 |  | 
 | 432 | 	if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) | 
 | 433 | 		return false; | 
 | 434 |  | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 435 | 	fence->fence_wake.flags = 0; | 
 | 436 | 	fence->fence_wake.private = NULL; | 
 | 437 | 	fence->fence_wake.func = amdgpu_fence_check_signaled; | 
 | 438 | 	__add_wait_queue(&adev->fence_queue, &fence->fence_wake); | 
 | 439 | 	fence_get(f); | 
 | 440 | 	FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx); | 
 | 441 | 	return true; | 
 | 442 | } | 
 | 443 |  | 
 | 444 | /** | 
 | 445 |  * amdgpu_fence_signaled - check if a fence has signaled | 
 | 446 |  * | 
 | 447 |  * @fence: amdgpu fence object | 
 | 448 |  * | 
 | 449 |  * Check if the requested fence has signaled (all asics). | 
 | 450 |  * Returns true if the fence has signaled or false if it has not. | 
 | 451 |  */ | 
 | 452 | bool amdgpu_fence_signaled(struct amdgpu_fence *fence) | 
 | 453 | { | 
 | 454 | 	if (!fence) | 
 | 455 | 		return true; | 
 | 456 |  | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 457 | 	if (amdgpu_fence_seq_signaled(fence->ring, fence->seq)) { | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 458 | 		if (!fence_signal(&fence->base)) | 
 | 459 | 			FENCE_TRACE(&fence->base, "signaled from amdgpu_fence_signaled\n"); | 
 | 460 | 		return true; | 
 | 461 | 	} | 
 | 462 |  | 
 | 463 | 	return false; | 
 | 464 | } | 
 | 465 |  | 
 | 466 | /** | 
 | 467 |  * amdgpu_fence_any_seq_signaled - check if any sequence number is signaled | 
 | 468 |  * | 
 | 469 |  * @adev: amdgpu device pointer | 
 | 470 |  * @seq: sequence numbers | 
 | 471 |  * | 
 | 472 |  * Check if the last signaled fence sequnce number is >= the requested | 
 | 473 |  * sequence number (all asics). | 
 | 474 |  * Returns true if any has signaled (current value is >= requested value) | 
 | 475 |  * or false if it has not. Helper function for amdgpu_fence_wait_seq. | 
 | 476 |  */ | 
 | 477 | static bool amdgpu_fence_any_seq_signaled(struct amdgpu_device *adev, u64 *seq) | 
 | 478 | { | 
 | 479 | 	unsigned i; | 
 | 480 |  | 
 | 481 | 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 482 | 		if (!adev->rings[i] || !seq[i]) | 
 | 483 | 			continue; | 
 | 484 |  | 
 | 485 | 		if (amdgpu_fence_seq_signaled(adev->rings[i], seq[i])) | 
 | 486 | 			return true; | 
 | 487 | 	} | 
 | 488 |  | 
 | 489 | 	return false; | 
 | 490 | } | 
 | 491 |  | 
 | 492 | /** | 
 | 493 |  * amdgpu_fence_wait_seq_timeout - wait for a specific sequence numbers | 
 | 494 |  * | 
 | 495 |  * @adev: amdgpu device pointer | 
 | 496 |  * @target_seq: sequence number(s) we want to wait for | 
 | 497 |  * @intr: use interruptable sleep | 
 | 498 |  * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait | 
 | 499 |  * | 
 | 500 |  * Wait for the requested sequence number(s) to be written by any ring | 
 | 501 |  * (all asics).  Sequnce number array is indexed by ring id. | 
 | 502 |  * @intr selects whether to use interruptable (true) or non-interruptable | 
 | 503 |  * (false) sleep when waiting for the sequence number.  Helper function | 
 | 504 |  * for amdgpu_fence_wait_*(). | 
 | 505 |  * Returns remaining time if the sequence number has passed, 0 when | 
 | 506 |  * the wait timeout, or an error for all other cases. | 
 | 507 |  * -EDEADLK is returned when a GPU lockup has been detected. | 
 | 508 |  */ | 
| Christian König | 03507c4 | 2015-06-19 17:00:19 +0200 | [diff] [blame] | 509 | static long amdgpu_fence_wait_seq_timeout(struct amdgpu_device *adev, | 
 | 510 | 					  u64 *target_seq, bool intr, | 
 | 511 | 					  long timeout) | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 512 | { | 
 | 513 | 	uint64_t last_seq[AMDGPU_MAX_RINGS]; | 
 | 514 | 	bool signaled; | 
| monk.liu | 332300b | 2015-06-08 14:48:15 +0800 | [diff] [blame] | 515 | 	int i; | 
 | 516 | 	long r; | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 517 |  | 
| Jack Xiao | 25f45e6 | 2015-06-04 12:18:27 +0800 | [diff] [blame] | 518 | 	if (timeout == 0) { | 
 | 519 | 		return amdgpu_fence_any_seq_signaled(adev, target_seq); | 
 | 520 | 	} | 
 | 521 |  | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 522 | 	while (!amdgpu_fence_any_seq_signaled(adev, target_seq)) { | 
 | 523 |  | 
 | 524 | 		/* Save current sequence values, used to check for GPU lockups */ | 
 | 525 | 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 526 | 			struct amdgpu_ring *ring = adev->rings[i]; | 
 | 527 |  | 
 | 528 | 			if (!ring || !target_seq[i]) | 
 | 529 | 				continue; | 
 | 530 |  | 
 | 531 | 			last_seq[i] = atomic64_read(&ring->fence_drv.last_seq); | 
 | 532 | 			trace_amdgpu_fence_wait_begin(adev->ddev, i, target_seq[i]); | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 533 | 		} | 
 | 534 |  | 
 | 535 | 		if (intr) { | 
 | 536 | 			r = wait_event_interruptible_timeout(adev->fence_queue, ( | 
 | 537 | 				(signaled = amdgpu_fence_any_seq_signaled(adev, target_seq)) | 
 | 538 | 				 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT); | 
 | 539 | 		} else { | 
 | 540 | 			r = wait_event_timeout(adev->fence_queue, ( | 
 | 541 | 				(signaled = amdgpu_fence_any_seq_signaled(adev, target_seq)) | 
 | 542 | 				 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT); | 
 | 543 | 		} | 
 | 544 |  | 
 | 545 | 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 546 | 			struct amdgpu_ring *ring = adev->rings[i]; | 
 | 547 |  | 
 | 548 | 			if (!ring || !target_seq[i]) | 
 | 549 | 				continue; | 
 | 550 |  | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 551 | 			trace_amdgpu_fence_wait_end(adev->ddev, i, target_seq[i]); | 
 | 552 | 		} | 
 | 553 |  | 
 | 554 | 		if (unlikely(r < 0)) | 
 | 555 | 			return r; | 
 | 556 |  | 
 | 557 | 		if (unlikely(!signaled)) { | 
 | 558 |  | 
 | 559 | 			if (adev->needs_reset) | 
 | 560 | 				return -EDEADLK; | 
 | 561 |  | 
 | 562 | 			/* we were interrupted for some reason and fence | 
 | 563 | 			 * isn't signaled yet, resume waiting */ | 
 | 564 | 			if (r) | 
 | 565 | 				continue; | 
 | 566 |  | 
 | 567 | 			for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 568 | 				struct amdgpu_ring *ring = adev->rings[i]; | 
 | 569 |  | 
 | 570 | 				if (!ring || !target_seq[i]) | 
 | 571 | 					continue; | 
 | 572 |  | 
 | 573 | 				if (last_seq[i] != atomic64_read(&ring->fence_drv.last_seq)) | 
 | 574 | 					break; | 
 | 575 | 			} | 
 | 576 |  | 
 | 577 | 			if (i != AMDGPU_MAX_RINGS) | 
 | 578 | 				continue; | 
 | 579 |  | 
 | 580 | 			for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 581 | 				if (!adev->rings[i] || !target_seq[i]) | 
 | 582 | 					continue; | 
 | 583 |  | 
 | 584 | 				if (amdgpu_ring_is_lockup(adev->rings[i])) | 
 | 585 | 					break; | 
 | 586 | 			} | 
 | 587 |  | 
 | 588 | 			if (i < AMDGPU_MAX_RINGS) { | 
 | 589 | 				/* good news we believe it's a lockup */ | 
 | 590 | 				dev_warn(adev->dev, "GPU lockup (waiting for " | 
 | 591 | 					 "0x%016llx last fence id 0x%016llx on" | 
 | 592 | 					 " ring %d)\n", | 
 | 593 | 					 target_seq[i], last_seq[i], i); | 
 | 594 |  | 
 | 595 | 				/* remember that we need an reset */ | 
 | 596 | 				adev->needs_reset = true; | 
 | 597 | 				wake_up_all(&adev->fence_queue); | 
 | 598 | 				return -EDEADLK; | 
 | 599 | 			} | 
 | 600 |  | 
 | 601 | 			if (timeout < MAX_SCHEDULE_TIMEOUT) { | 
 | 602 | 				timeout -= AMDGPU_FENCE_JIFFIES_TIMEOUT; | 
 | 603 | 				if (timeout <= 0) { | 
 | 604 | 					return 0; | 
 | 605 | 				} | 
 | 606 | 			} | 
 | 607 | 		} | 
 | 608 | 	} | 
 | 609 | 	return timeout; | 
 | 610 | } | 
 | 611 |  | 
 | 612 | /** | 
 | 613 |  * amdgpu_fence_wait - wait for a fence to signal | 
 | 614 |  * | 
 | 615 |  * @fence: amdgpu fence object | 
 | 616 |  * @intr: use interruptable sleep | 
 | 617 |  * | 
 | 618 |  * Wait for the requested fence to signal (all asics). | 
 | 619 |  * @intr selects whether to use interruptable (true) or non-interruptable | 
 | 620 |  * (false) sleep when waiting for the fence. | 
 | 621 |  * Returns 0 if the fence has passed, error for all other cases. | 
 | 622 |  */ | 
 | 623 | int amdgpu_fence_wait(struct amdgpu_fence *fence, bool intr) | 
 | 624 | { | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 625 | 	long r; | 
 | 626 |  | 
| monk.liu | 2e53608 | 2015-07-30 14:56:18 +0800 | [diff] [blame^] | 627 | 	r = fence_wait_timeout(&fence->base, intr, MAX_SCHEDULE_TIMEOUT); | 
 | 628 | 	if (r < 0) | 
 | 629 | 		return  r; | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 630 | 	return 0; | 
 | 631 | } | 
 | 632 |  | 
 | 633 | /** | 
 | 634 |  * amdgpu_fence_wait_any - wait for a fence to signal on any ring | 
 | 635 |  * | 
 | 636 |  * @adev: amdgpu device pointer | 
 | 637 |  * @fences: amdgpu fence object(s) | 
 | 638 |  * @intr: use interruptable sleep | 
 | 639 |  * | 
 | 640 |  * Wait for any requested fence to signal (all asics).  Fence | 
 | 641 |  * array is indexed by ring id.  @intr selects whether to use | 
 | 642 |  * interruptable (true) or non-interruptable (false) sleep when | 
 | 643 |  * waiting for the fences. Used by the suballocator. | 
 | 644 |  * Returns 0 if any fence has passed, error for all other cases. | 
 | 645 |  */ | 
 | 646 | int amdgpu_fence_wait_any(struct amdgpu_device *adev, | 
 | 647 | 			  struct amdgpu_fence **fences, | 
 | 648 | 			  bool intr) | 
 | 649 | { | 
 | 650 | 	uint64_t seq[AMDGPU_MAX_RINGS]; | 
 | 651 | 	unsigned i, num_rings = 0; | 
 | 652 | 	long r; | 
 | 653 |  | 
 | 654 | 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 655 | 		seq[i] = 0; | 
 | 656 |  | 
 | 657 | 		if (!fences[i]) { | 
 | 658 | 			continue; | 
 | 659 | 		} | 
 | 660 |  | 
 | 661 | 		seq[i] = fences[i]->seq; | 
 | 662 | 		++num_rings; | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 663 | 	} | 
 | 664 |  | 
 | 665 | 	/* nothing to wait for ? */ | 
 | 666 | 	if (num_rings == 0) | 
 | 667 | 		return -ENOENT; | 
 | 668 |  | 
 | 669 | 	r = amdgpu_fence_wait_seq_timeout(adev, seq, intr, MAX_SCHEDULE_TIMEOUT); | 
 | 670 | 	if (r < 0) { | 
 | 671 | 		return r; | 
 | 672 | 	} | 
 | 673 | 	return 0; | 
 | 674 | } | 
 | 675 |  | 
 | 676 | /** | 
 | 677 |  * amdgpu_fence_wait_next - wait for the next fence to signal | 
 | 678 |  * | 
 | 679 |  * @adev: amdgpu device pointer | 
 | 680 |  * @ring: ring index the fence is associated with | 
 | 681 |  * | 
 | 682 |  * Wait for the next fence on the requested ring to signal (all asics). | 
 | 683 |  * Returns 0 if the next fence has passed, error for all other cases. | 
 | 684 |  * Caller must hold ring lock. | 
 | 685 |  */ | 
 | 686 | int amdgpu_fence_wait_next(struct amdgpu_ring *ring) | 
 | 687 | { | 
 | 688 | 	uint64_t seq[AMDGPU_MAX_RINGS] = {}; | 
 | 689 | 	long r; | 
 | 690 |  | 
 | 691 | 	seq[ring->idx] = atomic64_read(&ring->fence_drv.last_seq) + 1ULL; | 
 | 692 | 	if (seq[ring->idx] >= ring->fence_drv.sync_seq[ring->idx]) { | 
 | 693 | 		/* nothing to wait for, last_seq is | 
 | 694 | 		   already the last emited fence */ | 
 | 695 | 		return -ENOENT; | 
 | 696 | 	} | 
 | 697 | 	r = amdgpu_fence_wait_seq_timeout(ring->adev, seq, false, MAX_SCHEDULE_TIMEOUT); | 
 | 698 | 	if (r < 0) | 
 | 699 | 		return r; | 
 | 700 | 	return 0; | 
 | 701 | } | 
 | 702 |  | 
 | 703 | /** | 
 | 704 |  * amdgpu_fence_wait_empty - wait for all fences to signal | 
 | 705 |  * | 
 | 706 |  * @adev: amdgpu device pointer | 
 | 707 |  * @ring: ring index the fence is associated with | 
 | 708 |  * | 
 | 709 |  * Wait for all fences on the requested ring to signal (all asics). | 
 | 710 |  * Returns 0 if the fences have passed, error for all other cases. | 
 | 711 |  * Caller must hold ring lock. | 
 | 712 |  */ | 
 | 713 | int amdgpu_fence_wait_empty(struct amdgpu_ring *ring) | 
 | 714 | { | 
 | 715 | 	struct amdgpu_device *adev = ring->adev; | 
 | 716 | 	uint64_t seq[AMDGPU_MAX_RINGS] = {}; | 
 | 717 | 	long r; | 
 | 718 |  | 
 | 719 | 	seq[ring->idx] = ring->fence_drv.sync_seq[ring->idx]; | 
 | 720 | 	if (!seq[ring->idx]) | 
 | 721 | 		return 0; | 
 | 722 |  | 
 | 723 | 	r = amdgpu_fence_wait_seq_timeout(adev, seq, false, MAX_SCHEDULE_TIMEOUT); | 
 | 724 | 	if (r < 0) { | 
 | 725 | 		if (r == -EDEADLK) | 
 | 726 | 			return -EDEADLK; | 
 | 727 |  | 
 | 728 | 		dev_err(adev->dev, "error waiting for ring[%d] to become idle (%ld)\n", | 
 | 729 | 			ring->idx, r); | 
 | 730 | 	} | 
 | 731 | 	return 0; | 
 | 732 | } | 
 | 733 |  | 
 | 734 | /** | 
 | 735 |  * amdgpu_fence_ref - take a ref on a fence | 
 | 736 |  * | 
 | 737 |  * @fence: amdgpu fence object | 
 | 738 |  * | 
 | 739 |  * Take a reference on a fence (all asics). | 
 | 740 |  * Returns the fence. | 
 | 741 |  */ | 
 | 742 | struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence) | 
 | 743 | { | 
 | 744 | 	fence_get(&fence->base); | 
 | 745 | 	return fence; | 
 | 746 | } | 
 | 747 |  | 
 | 748 | /** | 
 | 749 |  * amdgpu_fence_unref - remove a ref on a fence | 
 | 750 |  * | 
 | 751 |  * @fence: amdgpu fence object | 
 | 752 |  * | 
 | 753 |  * Remove a reference on a fence (all asics). | 
 | 754 |  */ | 
 | 755 | void amdgpu_fence_unref(struct amdgpu_fence **fence) | 
 | 756 | { | 
 | 757 | 	struct amdgpu_fence *tmp = *fence; | 
 | 758 |  | 
 | 759 | 	*fence = NULL; | 
 | 760 | 	if (tmp) | 
 | 761 | 		fence_put(&tmp->base); | 
 | 762 | } | 
 | 763 |  | 
 | 764 | /** | 
 | 765 |  * amdgpu_fence_count_emitted - get the count of emitted fences | 
 | 766 |  * | 
 | 767 |  * @ring: ring the fence is associated with | 
 | 768 |  * | 
 | 769 |  * Get the number of fences emitted on the requested ring (all asics). | 
 | 770 |  * Returns the number of emitted fences on the ring.  Used by the | 
 | 771 |  * dynpm code to ring track activity. | 
 | 772 |  */ | 
 | 773 | unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring) | 
 | 774 | { | 
 | 775 | 	uint64_t emitted; | 
 | 776 |  | 
 | 777 | 	/* We are not protected by ring lock when reading the last sequence | 
 | 778 | 	 * but it's ok to report slightly wrong fence count here. | 
 | 779 | 	 */ | 
 | 780 | 	amdgpu_fence_process(ring); | 
 | 781 | 	emitted = ring->fence_drv.sync_seq[ring->idx] | 
 | 782 | 		- atomic64_read(&ring->fence_drv.last_seq); | 
 | 783 | 	/* to avoid 32bits warp around */ | 
 | 784 | 	if (emitted > 0x10000000) | 
 | 785 | 		emitted = 0x10000000; | 
 | 786 |  | 
 | 787 | 	return (unsigned)emitted; | 
 | 788 | } | 
 | 789 |  | 
 | 790 | /** | 
 | 791 |  * amdgpu_fence_need_sync - do we need a semaphore | 
 | 792 |  * | 
 | 793 |  * @fence: amdgpu fence object | 
 | 794 |  * @dst_ring: which ring to check against | 
 | 795 |  * | 
 | 796 |  * Check if the fence needs to be synced against another ring | 
 | 797 |  * (all asics).  If so, we need to emit a semaphore. | 
 | 798 |  * Returns true if we need to sync with another ring, false if | 
 | 799 |  * not. | 
 | 800 |  */ | 
 | 801 | bool amdgpu_fence_need_sync(struct amdgpu_fence *fence, | 
 | 802 | 			    struct amdgpu_ring *dst_ring) | 
 | 803 | { | 
 | 804 | 	struct amdgpu_fence_driver *fdrv; | 
 | 805 |  | 
 | 806 | 	if (!fence) | 
 | 807 | 		return false; | 
 | 808 |  | 
 | 809 | 	if (fence->ring == dst_ring) | 
 | 810 | 		return false; | 
 | 811 |  | 
 | 812 | 	/* we are protected by the ring mutex */ | 
 | 813 | 	fdrv = &dst_ring->fence_drv; | 
 | 814 | 	if (fence->seq <= fdrv->sync_seq[fence->ring->idx]) | 
 | 815 | 		return false; | 
 | 816 |  | 
 | 817 | 	return true; | 
 | 818 | } | 
 | 819 |  | 
 | 820 | /** | 
 | 821 |  * amdgpu_fence_note_sync - record the sync point | 
 | 822 |  * | 
 | 823 |  * @fence: amdgpu fence object | 
 | 824 |  * @dst_ring: which ring to check against | 
 | 825 |  * | 
 | 826 |  * Note the sequence number at which point the fence will | 
 | 827 |  * be synced with the requested ring (all asics). | 
 | 828 |  */ | 
 | 829 | void amdgpu_fence_note_sync(struct amdgpu_fence *fence, | 
 | 830 | 			    struct amdgpu_ring *dst_ring) | 
 | 831 | { | 
 | 832 | 	struct amdgpu_fence_driver *dst, *src; | 
 | 833 | 	unsigned i; | 
 | 834 |  | 
 | 835 | 	if (!fence) | 
 | 836 | 		return; | 
 | 837 |  | 
 | 838 | 	if (fence->ring == dst_ring) | 
 | 839 | 		return; | 
 | 840 |  | 
 | 841 | 	/* we are protected by the ring mutex */ | 
 | 842 | 	src = &fence->ring->fence_drv; | 
 | 843 | 	dst = &dst_ring->fence_drv; | 
 | 844 | 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 845 | 		if (i == dst_ring->idx) | 
 | 846 | 			continue; | 
 | 847 |  | 
 | 848 | 		dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]); | 
 | 849 | 	} | 
 | 850 | } | 
 | 851 |  | 
 | 852 | /** | 
 | 853 |  * amdgpu_fence_driver_start_ring - make the fence driver | 
 | 854 |  * ready for use on the requested ring. | 
 | 855 |  * | 
 | 856 |  * @ring: ring to start the fence driver on | 
 | 857 |  * @irq_src: interrupt source to use for this ring | 
 | 858 |  * @irq_type: interrupt type to use for this ring | 
 | 859 |  * | 
 | 860 |  * Make the fence driver ready for processing (all asics). | 
 | 861 |  * Not all asics have all rings, so each asic will only | 
 | 862 |  * start the fence driver on the rings it has. | 
 | 863 |  * Returns 0 for success, errors for failure. | 
 | 864 |  */ | 
 | 865 | int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring, | 
 | 866 | 				   struct amdgpu_irq_src *irq_src, | 
 | 867 | 				   unsigned irq_type) | 
 | 868 | { | 
 | 869 | 	struct amdgpu_device *adev = ring->adev; | 
 | 870 | 	uint64_t index; | 
 | 871 |  | 
 | 872 | 	if (ring != &adev->uvd.ring) { | 
 | 873 | 		ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs]; | 
 | 874 | 		ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4); | 
 | 875 | 	} else { | 
 | 876 | 		/* put fence directly behind firmware */ | 
 | 877 | 		index = ALIGN(adev->uvd.fw->size, 8); | 
 | 878 | 		ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index; | 
 | 879 | 		ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index; | 
 | 880 | 	} | 
 | 881 | 	amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq)); | 
| Chunming Zhou | c6a4079 | 2015-06-01 14:14:32 +0800 | [diff] [blame] | 882 | 	amdgpu_irq_get(adev, irq_src, irq_type); | 
 | 883 |  | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 884 | 	ring->fence_drv.irq_src = irq_src; | 
 | 885 | 	ring->fence_drv.irq_type = irq_type; | 
| Chunming Zhou | c6a4079 | 2015-06-01 14:14:32 +0800 | [diff] [blame] | 886 | 	ring->fence_drv.initialized = true; | 
 | 887 |  | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 888 | 	dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, " | 
 | 889 | 		 "cpu addr 0x%p\n", ring->idx, | 
 | 890 | 		 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr); | 
 | 891 | 	return 0; | 
 | 892 | } | 
 | 893 |  | 
 | 894 | /** | 
 | 895 |  * amdgpu_fence_driver_init_ring - init the fence driver | 
 | 896 |  * for the requested ring. | 
 | 897 |  * | 
 | 898 |  * @ring: ring to init the fence driver on | 
 | 899 |  * | 
 | 900 |  * Init the fence driver for the requested ring (all asics). | 
 | 901 |  * Helper function for amdgpu_fence_driver_init(). | 
 | 902 |  */ | 
 | 903 | void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring) | 
 | 904 | { | 
 | 905 | 	int i; | 
 | 906 |  | 
 | 907 | 	ring->fence_drv.cpu_addr = NULL; | 
 | 908 | 	ring->fence_drv.gpu_addr = 0; | 
 | 909 | 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) | 
 | 910 | 		ring->fence_drv.sync_seq[i] = 0; | 
 | 911 |  | 
 | 912 | 	atomic64_set(&ring->fence_drv.last_seq, 0); | 
 | 913 | 	ring->fence_drv.initialized = false; | 
 | 914 |  | 
 | 915 | 	INIT_DELAYED_WORK(&ring->fence_drv.lockup_work, | 
 | 916 | 			amdgpu_fence_check_lockup); | 
 | 917 | 	ring->fence_drv.ring = ring; | 
| Alex Deucher | b80d847 | 2015-08-16 22:55:02 -0400 | [diff] [blame] | 918 |  | 
 | 919 | 	if (amdgpu_enable_scheduler) { | 
 | 920 | 		ring->scheduler = amd_sched_create((void *)ring->adev, | 
| Chunming Zhou | c1b69ed | 2015-07-21 13:45:14 +0800 | [diff] [blame] | 921 | 						   &amdgpu_sched_ops, | 
| Jammy Zhou | 4afcb30 | 2015-07-30 16:44:05 +0800 | [diff] [blame] | 922 | 						   ring->idx, 5, 0, | 
 | 923 | 						   amdgpu_sched_hw_submission); | 
| Alex Deucher | b80d847 | 2015-08-16 22:55:02 -0400 | [diff] [blame] | 924 | 		if (!ring->scheduler) | 
 | 925 | 			DRM_ERROR("Failed to create scheduler on ring %d.\n", | 
 | 926 | 				  ring->idx); | 
 | 927 | 	} | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 928 | } | 
 | 929 |  | 
 | 930 | /** | 
 | 931 |  * amdgpu_fence_driver_init - init the fence driver | 
 | 932 |  * for all possible rings. | 
 | 933 |  * | 
 | 934 |  * @adev: amdgpu device pointer | 
 | 935 |  * | 
 | 936 |  * Init the fence driver for all possible rings (all asics). | 
 | 937 |  * Not all asics have all rings, so each asic will only | 
 | 938 |  * start the fence driver on the rings it has using | 
 | 939 |  * amdgpu_fence_driver_start_ring(). | 
 | 940 |  * Returns 0 for success. | 
 | 941 |  */ | 
 | 942 | int amdgpu_fence_driver_init(struct amdgpu_device *adev) | 
 | 943 | { | 
 | 944 | 	init_waitqueue_head(&adev->fence_queue); | 
 | 945 | 	if (amdgpu_debugfs_fence_init(adev)) | 
 | 946 | 		dev_err(adev->dev, "fence debugfs file creation failed\n"); | 
 | 947 |  | 
 | 948 | 	return 0; | 
 | 949 | } | 
 | 950 |  | 
 | 951 | /** | 
 | 952 |  * amdgpu_fence_driver_fini - tear down the fence driver | 
 | 953 |  * for all possible rings. | 
 | 954 |  * | 
 | 955 |  * @adev: amdgpu device pointer | 
 | 956 |  * | 
 | 957 |  * Tear down the fence driver for all possible rings (all asics). | 
 | 958 |  */ | 
 | 959 | void amdgpu_fence_driver_fini(struct amdgpu_device *adev) | 
 | 960 | { | 
 | 961 | 	int i, r; | 
 | 962 |  | 
 | 963 | 	mutex_lock(&adev->ring_lock); | 
 | 964 | 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) { | 
 | 965 | 		struct amdgpu_ring *ring = adev->rings[i]; | 
 | 966 | 		if (!ring || !ring->fence_drv.initialized) | 
 | 967 | 			continue; | 
 | 968 | 		r = amdgpu_fence_wait_empty(ring); | 
 | 969 | 		if (r) { | 
 | 970 | 			/* no need to trigger GPU reset as we are unloading */ | 
 | 971 | 			amdgpu_fence_driver_force_completion(adev); | 
 | 972 | 		} | 
 | 973 | 		wake_up_all(&adev->fence_queue); | 
| Chunming Zhou | c6a4079 | 2015-06-01 14:14:32 +0800 | [diff] [blame] | 974 | 		amdgpu_irq_put(adev, ring->fence_drv.irq_src, | 
 | 975 | 			       ring->fence_drv.irq_type); | 
| Alex Deucher | b80d847 | 2015-08-16 22:55:02 -0400 | [diff] [blame] | 976 | 		if (ring->scheduler) | 
 | 977 | 			amd_sched_destroy(ring->scheduler); | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 978 | 		ring->fence_drv.initialized = false; | 
 | 979 | 	} | 
 | 980 | 	mutex_unlock(&adev->ring_lock); | 
 | 981 | } | 
 | 982 |  | 
 | 983 | /** | 
| Alex Deucher | 5ceb54c | 2015-08-05 12:41:48 -0400 | [diff] [blame] | 984 |  * amdgpu_fence_driver_suspend - suspend the fence driver | 
 | 985 |  * for all possible rings. | 
 | 986 |  * | 
 | 987 |  * @adev: amdgpu device pointer | 
 | 988 |  * | 
 | 989 |  * Suspend the fence driver for all possible rings (all asics). | 
 | 990 |  */ | 
 | 991 | void amdgpu_fence_driver_suspend(struct amdgpu_device *adev) | 
 | 992 | { | 
 | 993 | 	int i, r; | 
 | 994 |  | 
 | 995 | 	mutex_lock(&adev->ring_lock); | 
 | 996 | 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) { | 
 | 997 | 		struct amdgpu_ring *ring = adev->rings[i]; | 
 | 998 | 		if (!ring || !ring->fence_drv.initialized) | 
 | 999 | 			continue; | 
 | 1000 |  | 
 | 1001 | 		/* wait for gpu to finish processing current batch */ | 
 | 1002 | 		r = amdgpu_fence_wait_empty(ring); | 
 | 1003 | 		if (r) { | 
 | 1004 | 			/* delay GPU reset to resume */ | 
 | 1005 | 			amdgpu_fence_driver_force_completion(adev); | 
 | 1006 | 		} | 
 | 1007 |  | 
 | 1008 | 		/* disable the interrupt */ | 
 | 1009 | 		amdgpu_irq_put(adev, ring->fence_drv.irq_src, | 
 | 1010 | 			       ring->fence_drv.irq_type); | 
 | 1011 | 	} | 
 | 1012 | 	mutex_unlock(&adev->ring_lock); | 
 | 1013 | } | 
 | 1014 |  | 
 | 1015 | /** | 
 | 1016 |  * amdgpu_fence_driver_resume - resume the fence driver | 
 | 1017 |  * for all possible rings. | 
 | 1018 |  * | 
 | 1019 |  * @adev: amdgpu device pointer | 
 | 1020 |  * | 
 | 1021 |  * Resume the fence driver for all possible rings (all asics). | 
 | 1022 |  * Not all asics have all rings, so each asic will only | 
 | 1023 |  * start the fence driver on the rings it has using | 
 | 1024 |  * amdgpu_fence_driver_start_ring(). | 
 | 1025 |  * Returns 0 for success. | 
 | 1026 |  */ | 
 | 1027 | void amdgpu_fence_driver_resume(struct amdgpu_device *adev) | 
 | 1028 | { | 
 | 1029 | 	int i; | 
 | 1030 |  | 
 | 1031 | 	mutex_lock(&adev->ring_lock); | 
 | 1032 | 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) { | 
 | 1033 | 		struct amdgpu_ring *ring = adev->rings[i]; | 
 | 1034 | 		if (!ring || !ring->fence_drv.initialized) | 
 | 1035 | 			continue; | 
 | 1036 |  | 
 | 1037 | 		/* enable the interrupt */ | 
 | 1038 | 		amdgpu_irq_get(adev, ring->fence_drv.irq_src, | 
 | 1039 | 			       ring->fence_drv.irq_type); | 
 | 1040 | 	} | 
 | 1041 | 	mutex_unlock(&adev->ring_lock); | 
 | 1042 | } | 
 | 1043 |  | 
 | 1044 | /** | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1045 |  * amdgpu_fence_driver_force_completion - force all fence waiter to complete | 
 | 1046 |  * | 
 | 1047 |  * @adev: amdgpu device pointer | 
 | 1048 |  * | 
 | 1049 |  * In case of GPU reset failure make sure no process keep waiting on fence | 
 | 1050 |  * that will never complete. | 
 | 1051 |  */ | 
 | 1052 | void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev) | 
 | 1053 | { | 
 | 1054 | 	int i; | 
 | 1055 |  | 
 | 1056 | 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) { | 
 | 1057 | 		struct amdgpu_ring *ring = adev->rings[i]; | 
 | 1058 | 		if (!ring || !ring->fence_drv.initialized) | 
 | 1059 | 			continue; | 
 | 1060 |  | 
 | 1061 | 		amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]); | 
 | 1062 | 	} | 
 | 1063 | } | 
 | 1064 |  | 
 | 1065 |  | 
 | 1066 | /* | 
 | 1067 |  * Fence debugfs | 
 | 1068 |  */ | 
 | 1069 | #if defined(CONFIG_DEBUG_FS) | 
 | 1070 | static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data) | 
 | 1071 | { | 
 | 1072 | 	struct drm_info_node *node = (struct drm_info_node *)m->private; | 
 | 1073 | 	struct drm_device *dev = node->minor->dev; | 
 | 1074 | 	struct amdgpu_device *adev = dev->dev_private; | 
 | 1075 | 	int i, j; | 
 | 1076 |  | 
 | 1077 | 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 1078 | 		struct amdgpu_ring *ring = adev->rings[i]; | 
 | 1079 | 		if (!ring || !ring->fence_drv.initialized) | 
 | 1080 | 			continue; | 
 | 1081 |  | 
 | 1082 | 		amdgpu_fence_process(ring); | 
 | 1083 |  | 
| Christian König | 344c19f | 2015-06-02 15:47:16 +0200 | [diff] [blame] | 1084 | 		seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name); | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1085 | 		seq_printf(m, "Last signaled fence 0x%016llx\n", | 
 | 1086 | 			   (unsigned long long)atomic64_read(&ring->fence_drv.last_seq)); | 
 | 1087 | 		seq_printf(m, "Last emitted        0x%016llx\n", | 
 | 1088 | 			   ring->fence_drv.sync_seq[i]); | 
 | 1089 |  | 
 | 1090 | 		for (j = 0; j < AMDGPU_MAX_RINGS; ++j) { | 
 | 1091 | 			struct amdgpu_ring *other = adev->rings[j]; | 
| Christian König | 344c19f | 2015-06-02 15:47:16 +0200 | [diff] [blame] | 1092 | 			if (i != j && other && other->fence_drv.initialized && | 
 | 1093 | 			    ring->fence_drv.sync_seq[j]) | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1094 | 				seq_printf(m, "Last sync to ring %d 0x%016llx\n", | 
 | 1095 | 					   j, ring->fence_drv.sync_seq[j]); | 
 | 1096 | 		} | 
 | 1097 | 	} | 
 | 1098 | 	return 0; | 
 | 1099 | } | 
 | 1100 |  | 
 | 1101 | static struct drm_info_list amdgpu_debugfs_fence_list[] = { | 
 | 1102 | 	{"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL}, | 
 | 1103 | }; | 
 | 1104 | #endif | 
 | 1105 |  | 
 | 1106 | int amdgpu_debugfs_fence_init(struct amdgpu_device *adev) | 
 | 1107 | { | 
 | 1108 | #if defined(CONFIG_DEBUG_FS) | 
 | 1109 | 	return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1); | 
 | 1110 | #else | 
 | 1111 | 	return 0; | 
 | 1112 | #endif | 
 | 1113 | } | 
 | 1114 |  | 
 | 1115 | static const char *amdgpu_fence_get_driver_name(struct fence *fence) | 
 | 1116 | { | 
 | 1117 | 	return "amdgpu"; | 
 | 1118 | } | 
 | 1119 |  | 
 | 1120 | static const char *amdgpu_fence_get_timeline_name(struct fence *f) | 
 | 1121 | { | 
 | 1122 | 	struct amdgpu_fence *fence = to_amdgpu_fence(f); | 
 | 1123 | 	return (const char *)fence->ring->name; | 
 | 1124 | } | 
 | 1125 |  | 
 | 1126 | static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence) | 
 | 1127 | { | 
 | 1128 | 	return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags); | 
 | 1129 | } | 
 | 1130 |  | 
 | 1131 | struct amdgpu_wait_cb { | 
 | 1132 | 	struct fence_cb base; | 
 | 1133 | 	struct task_struct *task; | 
 | 1134 | }; | 
 | 1135 |  | 
 | 1136 | static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb) | 
 | 1137 | { | 
 | 1138 | 	struct amdgpu_wait_cb *wait = | 
 | 1139 | 		container_of(cb, struct amdgpu_wait_cb, base); | 
 | 1140 | 	wake_up_process(wait->task); | 
 | 1141 | } | 
 | 1142 |  | 
 | 1143 | static signed long amdgpu_fence_default_wait(struct fence *f, bool intr, | 
 | 1144 | 					     signed long t) | 
 | 1145 | { | 
 | 1146 | 	struct amdgpu_fence *fence = to_amdgpu_fence(f); | 
 | 1147 | 	struct amdgpu_device *adev = fence->ring->adev; | 
 | 1148 | 	struct amdgpu_wait_cb cb; | 
 | 1149 |  | 
 | 1150 | 	cb.task = current; | 
 | 1151 |  | 
 | 1152 | 	if (fence_add_callback(f, &cb.base, amdgpu_fence_wait_cb)) | 
 | 1153 | 		return t; | 
 | 1154 |  | 
 | 1155 | 	while (t > 0) { | 
 | 1156 | 		if (intr) | 
 | 1157 | 			set_current_state(TASK_INTERRUPTIBLE); | 
 | 1158 | 		else | 
 | 1159 | 			set_current_state(TASK_UNINTERRUPTIBLE); | 
 | 1160 |  | 
 | 1161 | 		/* | 
 | 1162 | 		 * amdgpu_test_signaled must be called after | 
 | 1163 | 		 * set_current_state to prevent a race with wake_up_process | 
 | 1164 | 		 */ | 
 | 1165 | 		if (amdgpu_test_signaled(fence)) | 
 | 1166 | 			break; | 
 | 1167 |  | 
 | 1168 | 		if (adev->needs_reset) { | 
 | 1169 | 			t = -EDEADLK; | 
 | 1170 | 			break; | 
 | 1171 | 		} | 
 | 1172 |  | 
 | 1173 | 		t = schedule_timeout(t); | 
 | 1174 |  | 
 | 1175 | 		if (t > 0 && intr && signal_pending(current)) | 
 | 1176 | 			t = -ERESTARTSYS; | 
 | 1177 | 	} | 
 | 1178 |  | 
 | 1179 | 	__set_current_state(TASK_RUNNING); | 
 | 1180 | 	fence_remove_callback(f, &cb.base); | 
 | 1181 |  | 
 | 1182 | 	return t; | 
 | 1183 | } | 
 | 1184 |  | 
 | 1185 | const struct fence_ops amdgpu_fence_ops = { | 
 | 1186 | 	.get_driver_name = amdgpu_fence_get_driver_name, | 
 | 1187 | 	.get_timeline_name = amdgpu_fence_get_timeline_name, | 
 | 1188 | 	.enable_signaling = amdgpu_fence_enable_signaling, | 
 | 1189 | 	.signaled = amdgpu_fence_is_signaled, | 
 | 1190 | 	.wait = amdgpu_fence_default_wait, | 
 | 1191 | 	.release = NULL, | 
 | 1192 | }; |