| 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 |  | 
 | 167 | 		amdgpu_irq_put(adev, fence->ring->fence_drv.irq_src, | 
 | 168 | 				fence->ring->fence_drv.irq_type); | 
 | 169 | 		__remove_wait_queue(&adev->fence_queue, &fence->fence_wake); | 
 | 170 | 		fence_put(&fence->base); | 
 | 171 | 	} else | 
 | 172 | 		FENCE_TRACE(&fence->base, "pending\n"); | 
 | 173 | 	return 0; | 
 | 174 | } | 
 | 175 |  | 
 | 176 | /** | 
 | 177 |  * amdgpu_fence_activity - check for fence activity | 
 | 178 |  * | 
 | 179 |  * @ring: pointer to struct amdgpu_ring | 
 | 180 |  * | 
 | 181 |  * Checks the current fence value and calculates the last | 
 | 182 |  * signalled fence value. Returns true if activity occured | 
 | 183 |  * on the ring, and the fence_queue should be waken up. | 
 | 184 |  */ | 
 | 185 | static bool amdgpu_fence_activity(struct amdgpu_ring *ring) | 
 | 186 | { | 
 | 187 | 	uint64_t seq, last_seq, last_emitted; | 
 | 188 | 	unsigned count_loop = 0; | 
 | 189 | 	bool wake = false; | 
 | 190 |  | 
 | 191 | 	/* Note there is a scenario here for an infinite loop but it's | 
 | 192 | 	 * very unlikely to happen. For it to happen, the current polling | 
 | 193 | 	 * process need to be interrupted by another process and another | 
 | 194 | 	 * process needs to update the last_seq btw the atomic read and | 
 | 195 | 	 * xchg of the current process. | 
 | 196 | 	 * | 
 | 197 | 	 * 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] | 198 | 	 * continuously new fence signaled ie amdgpu_fence_read needs | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 199 | 	 * to return a different value each time for both the currently | 
 | 200 | 	 * polling process and the other process that xchg the last_seq | 
 | 201 | 	 * btw atomic read and xchg of the current process. And the | 
 | 202 | 	 * value the other process set as last seq must be higher than | 
 | 203 | 	 * the seq value we just read. Which means that current process | 
| Jammy Zhou | 86c2b79 | 2015-05-13 22:52:42 +0800 | [diff] [blame] | 204 | 	 * need to be interrupted after amdgpu_fence_read and before | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 205 | 	 * atomic xchg. | 
 | 206 | 	 * | 
 | 207 | 	 * To be even more safe we count the number of time we loop and | 
 | 208 | 	 * we bail after 10 loop just accepting the fact that we might | 
 | 209 | 	 * have temporarly set the last_seq not to the true real last | 
 | 210 | 	 * seq but to an older one. | 
 | 211 | 	 */ | 
 | 212 | 	last_seq = atomic64_read(&ring->fence_drv.last_seq); | 
 | 213 | 	do { | 
 | 214 | 		last_emitted = ring->fence_drv.sync_seq[ring->idx]; | 
 | 215 | 		seq = amdgpu_fence_read(ring); | 
 | 216 | 		seq |= last_seq & 0xffffffff00000000LL; | 
 | 217 | 		if (seq < last_seq) { | 
 | 218 | 			seq &= 0xffffffff; | 
 | 219 | 			seq |= last_emitted & 0xffffffff00000000LL; | 
 | 220 | 		} | 
 | 221 |  | 
 | 222 | 		if (seq <= last_seq || seq > last_emitted) { | 
 | 223 | 			break; | 
 | 224 | 		} | 
 | 225 | 		/* If we loop over we don't want to return without | 
 | 226 | 		 * checking if a fence is signaled as it means that the | 
 | 227 | 		 * seq we just read is different from the previous on. | 
 | 228 | 		 */ | 
 | 229 | 		wake = true; | 
 | 230 | 		last_seq = seq; | 
 | 231 | 		if ((count_loop++) > 10) { | 
 | 232 | 			/* We looped over too many time leave with the | 
 | 233 | 			 * fact that we might have set an older fence | 
 | 234 | 			 * seq then the current real last seq as signaled | 
 | 235 | 			 * by the hw. | 
 | 236 | 			 */ | 
 | 237 | 			break; | 
 | 238 | 		} | 
 | 239 | 	} while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq); | 
 | 240 |  | 
 | 241 | 	if (seq < last_emitted) | 
 | 242 | 		amdgpu_fence_schedule_check(ring); | 
 | 243 |  | 
 | 244 | 	return wake; | 
 | 245 | } | 
 | 246 |  | 
 | 247 | /** | 
 | 248 |  * amdgpu_fence_check_lockup - check for hardware lockup | 
 | 249 |  * | 
 | 250 |  * @work: delayed work item | 
 | 251 |  * | 
 | 252 |  * Checks for fence activity and if there is none probe | 
 | 253 |  * the hardware if a lockup occured. | 
 | 254 |  */ | 
 | 255 | static void amdgpu_fence_check_lockup(struct work_struct *work) | 
 | 256 | { | 
 | 257 | 	struct amdgpu_fence_driver *fence_drv; | 
 | 258 | 	struct amdgpu_ring *ring; | 
 | 259 |  | 
 | 260 | 	fence_drv = container_of(work, struct amdgpu_fence_driver, | 
 | 261 | 				lockup_work.work); | 
 | 262 | 	ring = fence_drv->ring; | 
 | 263 |  | 
 | 264 | 	if (!down_read_trylock(&ring->adev->exclusive_lock)) { | 
 | 265 | 		/* just reschedule the check if a reset is going on */ | 
 | 266 | 		amdgpu_fence_schedule_check(ring); | 
 | 267 | 		return; | 
 | 268 | 	} | 
 | 269 |  | 
 | 270 | 	if (fence_drv->delayed_irq && ring->adev->ddev->irq_enabled) { | 
 | 271 | 		fence_drv->delayed_irq = false; | 
 | 272 | 		amdgpu_irq_update(ring->adev, fence_drv->irq_src, | 
 | 273 | 				fence_drv->irq_type); | 
 | 274 | 	} | 
 | 275 |  | 
 | 276 | 	if (amdgpu_fence_activity(ring)) | 
 | 277 | 		wake_up_all(&ring->adev->fence_queue); | 
 | 278 | 	else if (amdgpu_ring_is_lockup(ring)) { | 
 | 279 | 		/* good news we believe it's a lockup */ | 
 | 280 | 		dev_warn(ring->adev->dev, "GPU lockup (current fence id " | 
 | 281 | 			"0x%016llx last fence id 0x%016llx on ring %d)\n", | 
 | 282 | 			(uint64_t)atomic64_read(&fence_drv->last_seq), | 
 | 283 | 			fence_drv->sync_seq[ring->idx], ring->idx); | 
 | 284 |  | 
 | 285 | 		/* remember that we need an reset */ | 
 | 286 | 		ring->adev->needs_reset = true; | 
 | 287 | 		wake_up_all(&ring->adev->fence_queue); | 
 | 288 | 	} | 
 | 289 | 	up_read(&ring->adev->exclusive_lock); | 
 | 290 | } | 
 | 291 |  | 
 | 292 | /** | 
 | 293 |  * amdgpu_fence_process - process a fence | 
 | 294 |  * | 
 | 295 |  * @adev: amdgpu_device pointer | 
 | 296 |  * @ring: ring index the fence is associated with | 
 | 297 |  * | 
 | 298 |  * Checks the current fence value and wakes the fence queue | 
 | 299 |  * if the sequence number has increased (all asics). | 
 | 300 |  */ | 
 | 301 | void amdgpu_fence_process(struct amdgpu_ring *ring) | 
 | 302 | { | 
 | 303 | 	uint64_t seq, last_seq, last_emitted; | 
 | 304 | 	unsigned count_loop = 0; | 
 | 305 | 	bool wake = false; | 
 | 306 |  | 
 | 307 | 	/* Note there is a scenario here for an infinite loop but it's | 
 | 308 | 	 * very unlikely to happen. For it to happen, the current polling | 
 | 309 | 	 * process need to be interrupted by another process and another | 
 | 310 | 	 * process needs to update the last_seq btw the atomic read and | 
 | 311 | 	 * xchg of the current process. | 
 | 312 | 	 * | 
 | 313 | 	 * More over for this to go in infinite loop there need to be | 
 | 314 | 	 * continuously new fence signaled ie amdgpu_fence_read needs | 
 | 315 | 	 * to return a different value each time for both the currently | 
 | 316 | 	 * polling process and the other process that xchg the last_seq | 
 | 317 | 	 * btw atomic read and xchg of the current process. And the | 
 | 318 | 	 * value the other process set as last seq must be higher than | 
 | 319 | 	 * the seq value we just read. Which means that current process | 
 | 320 | 	 * need to be interrupted after amdgpu_fence_read and before | 
 | 321 | 	 * atomic xchg. | 
 | 322 | 	 * | 
 | 323 | 	 * To be even more safe we count the number of time we loop and | 
 | 324 | 	 * we bail after 10 loop just accepting the fact that we might | 
 | 325 | 	 * have temporarly set the last_seq not to the true real last | 
 | 326 | 	 * seq but to an older one. | 
 | 327 | 	 */ | 
 | 328 | 	last_seq = atomic64_read(&ring->fence_drv.last_seq); | 
 | 329 | 	do { | 
 | 330 | 		last_emitted = ring->fence_drv.sync_seq[ring->idx]; | 
 | 331 | 		seq = amdgpu_fence_read(ring); | 
 | 332 | 		seq |= last_seq & 0xffffffff00000000LL; | 
 | 333 | 		if (seq < last_seq) { | 
 | 334 | 			seq &= 0xffffffff; | 
 | 335 | 			seq |= last_emitted & 0xffffffff00000000LL; | 
 | 336 | 		} | 
 | 337 |  | 
 | 338 | 		if (seq <= last_seq || seq > last_emitted) { | 
 | 339 | 			break; | 
 | 340 | 		} | 
 | 341 | 		/* If we loop over we don't want to return without | 
 | 342 | 		 * checking if a fence is signaled as it means that the | 
 | 343 | 		 * seq we just read is different from the previous on. | 
 | 344 | 		 */ | 
 | 345 | 		wake = true; | 
 | 346 | 		last_seq = seq; | 
 | 347 | 		if ((count_loop++) > 10) { | 
 | 348 | 			/* We looped over too many time leave with the | 
 | 349 | 			 * fact that we might have set an older fence | 
 | 350 | 			 * seq then the current real last seq as signaled | 
 | 351 | 			 * by the hw. | 
 | 352 | 			 */ | 
 | 353 | 			break; | 
 | 354 | 		} | 
 | 355 | 	} while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq); | 
 | 356 |  | 
 | 357 | 	if (wake) | 
 | 358 | 		wake_up_all(&ring->adev->fence_queue); | 
 | 359 | } | 
 | 360 |  | 
 | 361 | /** | 
 | 362 |  * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled | 
 | 363 |  * | 
 | 364 |  * @ring: ring the fence is associated with | 
 | 365 |  * @seq: sequence number | 
 | 366 |  * | 
 | 367 |  * Check if the last signaled fence sequnce number is >= the requested | 
 | 368 |  * sequence number (all asics). | 
 | 369 |  * Returns true if the fence has signaled (current fence value | 
 | 370 |  * is >= requested value) or false if it has not (current fence | 
 | 371 |  * value is < the requested value.  Helper function for | 
 | 372 |  * amdgpu_fence_signaled(). | 
 | 373 |  */ | 
 | 374 | static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq) | 
 | 375 | { | 
 | 376 | 	if (atomic64_read(&ring->fence_drv.last_seq) >= seq) | 
 | 377 | 		return true; | 
 | 378 |  | 
 | 379 | 	/* poll new last sequence at least once */ | 
 | 380 | 	amdgpu_fence_process(ring); | 
 | 381 | 	if (atomic64_read(&ring->fence_drv.last_seq) >= seq) | 
 | 382 | 		return true; | 
 | 383 |  | 
 | 384 | 	return false; | 
 | 385 | } | 
 | 386 |  | 
 | 387 | static bool amdgpu_fence_is_signaled(struct fence *f) | 
 | 388 | { | 
 | 389 | 	struct amdgpu_fence *fence = to_amdgpu_fence(f); | 
 | 390 | 	struct amdgpu_ring *ring = fence->ring; | 
 | 391 | 	struct amdgpu_device *adev = ring->adev; | 
 | 392 |  | 
 | 393 | 	if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) | 
 | 394 | 		return true; | 
 | 395 |  | 
 | 396 | 	if (down_read_trylock(&adev->exclusive_lock)) { | 
 | 397 | 		amdgpu_fence_process(ring); | 
 | 398 | 		up_read(&adev->exclusive_lock); | 
 | 399 |  | 
 | 400 | 		if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) | 
 | 401 | 			return true; | 
 | 402 | 	} | 
 | 403 | 	return false; | 
 | 404 | } | 
 | 405 |  | 
 | 406 | /** | 
 | 407 |  * amdgpu_fence_enable_signaling - enable signalling on fence | 
 | 408 |  * @fence: fence | 
 | 409 |  * | 
 | 410 |  * This function is called with fence_queue lock held, and adds a callback | 
 | 411 |  * to fence_queue that checks if this fence is signaled, and if so it | 
 | 412 |  * signals the fence and removes itself. | 
 | 413 |  */ | 
 | 414 | static bool amdgpu_fence_enable_signaling(struct fence *f) | 
 | 415 | { | 
 | 416 | 	struct amdgpu_fence *fence = to_amdgpu_fence(f); | 
 | 417 | 	struct amdgpu_ring *ring = fence->ring; | 
 | 418 | 	struct amdgpu_device *adev = ring->adev; | 
 | 419 |  | 
 | 420 | 	if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) | 
 | 421 | 		return false; | 
 | 422 |  | 
 | 423 | 	if (down_read_trylock(&adev->exclusive_lock)) { | 
 | 424 | 		amdgpu_irq_get(adev, ring->fence_drv.irq_src, | 
 | 425 | 			ring->fence_drv.irq_type); | 
 | 426 | 		if (amdgpu_fence_activity(ring)) | 
 | 427 | 			wake_up_all_locked(&adev->fence_queue); | 
 | 428 |  | 
 | 429 | 		/* did fence get signaled after we enabled the sw irq? */ | 
 | 430 | 		if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) { | 
 | 431 | 			amdgpu_irq_put(adev, ring->fence_drv.irq_src, | 
 | 432 | 				ring->fence_drv.irq_type); | 
 | 433 | 			up_read(&adev->exclusive_lock); | 
 | 434 | 			return false; | 
 | 435 | 		} | 
 | 436 |  | 
 | 437 | 		up_read(&adev->exclusive_lock); | 
 | 438 | 	} else { | 
 | 439 | 		/* we're probably in a lockup, lets not fiddle too much */ | 
 | 440 | 		if (amdgpu_irq_get_delayed(adev, ring->fence_drv.irq_src, | 
 | 441 | 			ring->fence_drv.irq_type)) | 
 | 442 | 			ring->fence_drv.delayed_irq = true; | 
 | 443 | 		amdgpu_fence_schedule_check(ring); | 
 | 444 | 	} | 
 | 445 |  | 
 | 446 | 	fence->fence_wake.flags = 0; | 
 | 447 | 	fence->fence_wake.private = NULL; | 
 | 448 | 	fence->fence_wake.func = amdgpu_fence_check_signaled; | 
 | 449 | 	__add_wait_queue(&adev->fence_queue, &fence->fence_wake); | 
 | 450 | 	fence_get(f); | 
 | 451 | 	FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx); | 
 | 452 | 	return true; | 
 | 453 | } | 
 | 454 |  | 
 | 455 | /** | 
 | 456 |  * amdgpu_fence_signaled - check if a fence has signaled | 
 | 457 |  * | 
 | 458 |  * @fence: amdgpu fence object | 
 | 459 |  * | 
 | 460 |  * Check if the requested fence has signaled (all asics). | 
 | 461 |  * Returns true if the fence has signaled or false if it has not. | 
 | 462 |  */ | 
 | 463 | bool amdgpu_fence_signaled(struct amdgpu_fence *fence) | 
 | 464 | { | 
 | 465 | 	if (!fence) | 
 | 466 | 		return true; | 
 | 467 |  | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 468 | 	if (amdgpu_fence_seq_signaled(fence->ring, fence->seq)) { | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 469 | 		if (!fence_signal(&fence->base)) | 
 | 470 | 			FENCE_TRACE(&fence->base, "signaled from amdgpu_fence_signaled\n"); | 
 | 471 | 		return true; | 
 | 472 | 	} | 
 | 473 |  | 
 | 474 | 	return false; | 
 | 475 | } | 
 | 476 |  | 
 | 477 | /** | 
 | 478 |  * amdgpu_fence_any_seq_signaled - check if any sequence number is signaled | 
 | 479 |  * | 
 | 480 |  * @adev: amdgpu device pointer | 
 | 481 |  * @seq: sequence numbers | 
 | 482 |  * | 
 | 483 |  * Check if the last signaled fence sequnce number is >= the requested | 
 | 484 |  * sequence number (all asics). | 
 | 485 |  * Returns true if any has signaled (current value is >= requested value) | 
 | 486 |  * or false if it has not. Helper function for amdgpu_fence_wait_seq. | 
 | 487 |  */ | 
 | 488 | static bool amdgpu_fence_any_seq_signaled(struct amdgpu_device *adev, u64 *seq) | 
 | 489 | { | 
 | 490 | 	unsigned i; | 
 | 491 |  | 
 | 492 | 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 493 | 		if (!adev->rings[i] || !seq[i]) | 
 | 494 | 			continue; | 
 | 495 |  | 
 | 496 | 		if (amdgpu_fence_seq_signaled(adev->rings[i], seq[i])) | 
 | 497 | 			return true; | 
 | 498 | 	} | 
 | 499 |  | 
 | 500 | 	return false; | 
 | 501 | } | 
 | 502 |  | 
 | 503 | /** | 
 | 504 |  * amdgpu_fence_wait_seq_timeout - wait for a specific sequence numbers | 
 | 505 |  * | 
 | 506 |  * @adev: amdgpu device pointer | 
 | 507 |  * @target_seq: sequence number(s) we want to wait for | 
 | 508 |  * @intr: use interruptable sleep | 
 | 509 |  * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait | 
 | 510 |  * | 
 | 511 |  * Wait for the requested sequence number(s) to be written by any ring | 
 | 512 |  * (all asics).  Sequnce number array is indexed by ring id. | 
 | 513 |  * @intr selects whether to use interruptable (true) or non-interruptable | 
 | 514 |  * (false) sleep when waiting for the sequence number.  Helper function | 
 | 515 |  * for amdgpu_fence_wait_*(). | 
 | 516 |  * Returns remaining time if the sequence number has passed, 0 when | 
 | 517 |  * the wait timeout, or an error for all other cases. | 
 | 518 |  * -EDEADLK is returned when a GPU lockup has been detected. | 
 | 519 |  */ | 
 | 520 | long amdgpu_fence_wait_seq_timeout(struct amdgpu_device *adev, u64 *target_seq, | 
 | 521 | 				   bool intr, long timeout) | 
 | 522 | { | 
 | 523 | 	uint64_t last_seq[AMDGPU_MAX_RINGS]; | 
 | 524 | 	bool signaled; | 
| monk.liu | 332300b | 2015-06-08 14:48:15 +0800 | [diff] [blame^] | 525 | 	int i; | 
 | 526 | 	long r; | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 527 |  | 
| Jack Xiao | 25f45e6 | 2015-06-04 12:18:27 +0800 | [diff] [blame] | 528 | 	if (timeout == 0) { | 
 | 529 | 		return amdgpu_fence_any_seq_signaled(adev, target_seq); | 
 | 530 | 	} | 
 | 531 |  | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 532 | 	while (!amdgpu_fence_any_seq_signaled(adev, target_seq)) { | 
 | 533 |  | 
 | 534 | 		/* Save current sequence values, used to check for GPU lockups */ | 
 | 535 | 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 536 | 			struct amdgpu_ring *ring = adev->rings[i]; | 
 | 537 |  | 
 | 538 | 			if (!ring || !target_seq[i]) | 
 | 539 | 				continue; | 
 | 540 |  | 
 | 541 | 			last_seq[i] = atomic64_read(&ring->fence_drv.last_seq); | 
 | 542 | 			trace_amdgpu_fence_wait_begin(adev->ddev, i, target_seq[i]); | 
 | 543 | 			amdgpu_irq_get(adev, ring->fence_drv.irq_src, | 
 | 544 | 				       ring->fence_drv.irq_type); | 
 | 545 | 		} | 
 | 546 |  | 
 | 547 | 		if (intr) { | 
 | 548 | 			r = wait_event_interruptible_timeout(adev->fence_queue, ( | 
 | 549 | 				(signaled = amdgpu_fence_any_seq_signaled(adev, target_seq)) | 
 | 550 | 				 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT); | 
 | 551 | 		} else { | 
 | 552 | 			r = wait_event_timeout(adev->fence_queue, ( | 
 | 553 | 				(signaled = amdgpu_fence_any_seq_signaled(adev, target_seq)) | 
 | 554 | 				 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT); | 
 | 555 | 		} | 
 | 556 |  | 
 | 557 | 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 558 | 			struct amdgpu_ring *ring = adev->rings[i]; | 
 | 559 |  | 
 | 560 | 			if (!ring || !target_seq[i]) | 
 | 561 | 				continue; | 
 | 562 |  | 
 | 563 | 			amdgpu_irq_put(adev, ring->fence_drv.irq_src, | 
 | 564 | 				       ring->fence_drv.irq_type); | 
 | 565 | 			trace_amdgpu_fence_wait_end(adev->ddev, i, target_seq[i]); | 
 | 566 | 		} | 
 | 567 |  | 
 | 568 | 		if (unlikely(r < 0)) | 
 | 569 | 			return r; | 
 | 570 |  | 
 | 571 | 		if (unlikely(!signaled)) { | 
 | 572 |  | 
 | 573 | 			if (adev->needs_reset) | 
 | 574 | 				return -EDEADLK; | 
 | 575 |  | 
 | 576 | 			/* we were interrupted for some reason and fence | 
 | 577 | 			 * isn't signaled yet, resume waiting */ | 
 | 578 | 			if (r) | 
 | 579 | 				continue; | 
 | 580 |  | 
 | 581 | 			for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 582 | 				struct amdgpu_ring *ring = adev->rings[i]; | 
 | 583 |  | 
 | 584 | 				if (!ring || !target_seq[i]) | 
 | 585 | 					continue; | 
 | 586 |  | 
 | 587 | 				if (last_seq[i] != atomic64_read(&ring->fence_drv.last_seq)) | 
 | 588 | 					break; | 
 | 589 | 			} | 
 | 590 |  | 
 | 591 | 			if (i != AMDGPU_MAX_RINGS) | 
 | 592 | 				continue; | 
 | 593 |  | 
 | 594 | 			for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 595 | 				if (!adev->rings[i] || !target_seq[i]) | 
 | 596 | 					continue; | 
 | 597 |  | 
 | 598 | 				if (amdgpu_ring_is_lockup(adev->rings[i])) | 
 | 599 | 					break; | 
 | 600 | 			} | 
 | 601 |  | 
 | 602 | 			if (i < AMDGPU_MAX_RINGS) { | 
 | 603 | 				/* good news we believe it's a lockup */ | 
 | 604 | 				dev_warn(adev->dev, "GPU lockup (waiting for " | 
 | 605 | 					 "0x%016llx last fence id 0x%016llx on" | 
 | 606 | 					 " ring %d)\n", | 
 | 607 | 					 target_seq[i], last_seq[i], i); | 
 | 608 |  | 
 | 609 | 				/* remember that we need an reset */ | 
 | 610 | 				adev->needs_reset = true; | 
 | 611 | 				wake_up_all(&adev->fence_queue); | 
 | 612 | 				return -EDEADLK; | 
 | 613 | 			} | 
 | 614 |  | 
 | 615 | 			if (timeout < MAX_SCHEDULE_TIMEOUT) { | 
 | 616 | 				timeout -= AMDGPU_FENCE_JIFFIES_TIMEOUT; | 
 | 617 | 				if (timeout <= 0) { | 
 | 618 | 					return 0; | 
 | 619 | 				} | 
 | 620 | 			} | 
 | 621 | 		} | 
 | 622 | 	} | 
 | 623 | 	return timeout; | 
 | 624 | } | 
 | 625 |  | 
 | 626 | /** | 
 | 627 |  * amdgpu_fence_wait - wait for a fence to signal | 
 | 628 |  * | 
 | 629 |  * @fence: amdgpu fence object | 
 | 630 |  * @intr: use interruptable sleep | 
 | 631 |  * | 
 | 632 |  * Wait for the requested fence to signal (all asics). | 
 | 633 |  * @intr selects whether to use interruptable (true) or non-interruptable | 
 | 634 |  * (false) sleep when waiting for the fence. | 
 | 635 |  * Returns 0 if the fence has passed, error for all other cases. | 
 | 636 |  */ | 
 | 637 | int amdgpu_fence_wait(struct amdgpu_fence *fence, bool intr) | 
 | 638 | { | 
 | 639 | 	uint64_t seq[AMDGPU_MAX_RINGS] = {}; | 
 | 640 | 	long r; | 
 | 641 |  | 
 | 642 | 	seq[fence->ring->idx] = fence->seq; | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 643 | 	r = amdgpu_fence_wait_seq_timeout(fence->ring->adev, seq, intr, MAX_SCHEDULE_TIMEOUT); | 
 | 644 | 	if (r < 0) { | 
 | 645 | 		return r; | 
 | 646 | 	} | 
 | 647 |  | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 648 | 	r = fence_signal(&fence->base); | 
 | 649 | 	if (!r) | 
 | 650 | 		FENCE_TRACE(&fence->base, "signaled from fence_wait\n"); | 
 | 651 | 	return 0; | 
 | 652 | } | 
 | 653 |  | 
 | 654 | /** | 
 | 655 |  * amdgpu_fence_wait_any - wait for a fence to signal on any ring | 
 | 656 |  * | 
 | 657 |  * @adev: amdgpu device pointer | 
 | 658 |  * @fences: amdgpu fence object(s) | 
 | 659 |  * @intr: use interruptable sleep | 
 | 660 |  * | 
 | 661 |  * Wait for any requested fence to signal (all asics).  Fence | 
 | 662 |  * array is indexed by ring id.  @intr selects whether to use | 
 | 663 |  * interruptable (true) or non-interruptable (false) sleep when | 
 | 664 |  * waiting for the fences. Used by the suballocator. | 
 | 665 |  * Returns 0 if any fence has passed, error for all other cases. | 
 | 666 |  */ | 
 | 667 | int amdgpu_fence_wait_any(struct amdgpu_device *adev, | 
 | 668 | 			  struct amdgpu_fence **fences, | 
 | 669 | 			  bool intr) | 
 | 670 | { | 
 | 671 | 	uint64_t seq[AMDGPU_MAX_RINGS]; | 
 | 672 | 	unsigned i, num_rings = 0; | 
 | 673 | 	long r; | 
 | 674 |  | 
 | 675 | 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 676 | 		seq[i] = 0; | 
 | 677 |  | 
 | 678 | 		if (!fences[i]) { | 
 | 679 | 			continue; | 
 | 680 | 		} | 
 | 681 |  | 
 | 682 | 		seq[i] = fences[i]->seq; | 
 | 683 | 		++num_rings; | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 684 | 	} | 
 | 685 |  | 
 | 686 | 	/* nothing to wait for ? */ | 
 | 687 | 	if (num_rings == 0) | 
 | 688 | 		return -ENOENT; | 
 | 689 |  | 
 | 690 | 	r = amdgpu_fence_wait_seq_timeout(adev, seq, intr, MAX_SCHEDULE_TIMEOUT); | 
 | 691 | 	if (r < 0) { | 
 | 692 | 		return r; | 
 | 693 | 	} | 
 | 694 | 	return 0; | 
 | 695 | } | 
 | 696 |  | 
 | 697 | /** | 
 | 698 |  * amdgpu_fence_wait_next - wait for the next fence to signal | 
 | 699 |  * | 
 | 700 |  * @adev: amdgpu device pointer | 
 | 701 |  * @ring: ring index the fence is associated with | 
 | 702 |  * | 
 | 703 |  * Wait for the next fence on the requested ring to signal (all asics). | 
 | 704 |  * Returns 0 if the next fence has passed, error for all other cases. | 
 | 705 |  * Caller must hold ring lock. | 
 | 706 |  */ | 
 | 707 | int amdgpu_fence_wait_next(struct amdgpu_ring *ring) | 
 | 708 | { | 
 | 709 | 	uint64_t seq[AMDGPU_MAX_RINGS] = {}; | 
 | 710 | 	long r; | 
 | 711 |  | 
 | 712 | 	seq[ring->idx] = atomic64_read(&ring->fence_drv.last_seq) + 1ULL; | 
 | 713 | 	if (seq[ring->idx] >= ring->fence_drv.sync_seq[ring->idx]) { | 
 | 714 | 		/* nothing to wait for, last_seq is | 
 | 715 | 		   already the last emited fence */ | 
 | 716 | 		return -ENOENT; | 
 | 717 | 	} | 
 | 718 | 	r = amdgpu_fence_wait_seq_timeout(ring->adev, seq, false, MAX_SCHEDULE_TIMEOUT); | 
 | 719 | 	if (r < 0) | 
 | 720 | 		return r; | 
 | 721 | 	return 0; | 
 | 722 | } | 
 | 723 |  | 
 | 724 | /** | 
 | 725 |  * amdgpu_fence_wait_empty - wait for all fences to signal | 
 | 726 |  * | 
 | 727 |  * @adev: amdgpu device pointer | 
 | 728 |  * @ring: ring index the fence is associated with | 
 | 729 |  * | 
 | 730 |  * Wait for all fences on the requested ring to signal (all asics). | 
 | 731 |  * Returns 0 if the fences have passed, error for all other cases. | 
 | 732 |  * Caller must hold ring lock. | 
 | 733 |  */ | 
 | 734 | int amdgpu_fence_wait_empty(struct amdgpu_ring *ring) | 
 | 735 | { | 
 | 736 | 	struct amdgpu_device *adev = ring->adev; | 
 | 737 | 	uint64_t seq[AMDGPU_MAX_RINGS] = {}; | 
 | 738 | 	long r; | 
 | 739 |  | 
 | 740 | 	seq[ring->idx] = ring->fence_drv.sync_seq[ring->idx]; | 
 | 741 | 	if (!seq[ring->idx]) | 
 | 742 | 		return 0; | 
 | 743 |  | 
 | 744 | 	r = amdgpu_fence_wait_seq_timeout(adev, seq, false, MAX_SCHEDULE_TIMEOUT); | 
 | 745 | 	if (r < 0) { | 
 | 746 | 		if (r == -EDEADLK) | 
 | 747 | 			return -EDEADLK; | 
 | 748 |  | 
 | 749 | 		dev_err(adev->dev, "error waiting for ring[%d] to become idle (%ld)\n", | 
 | 750 | 			ring->idx, r); | 
 | 751 | 	} | 
 | 752 | 	return 0; | 
 | 753 | } | 
 | 754 |  | 
 | 755 | /** | 
 | 756 |  * amdgpu_fence_ref - take a ref on a fence | 
 | 757 |  * | 
 | 758 |  * @fence: amdgpu fence object | 
 | 759 |  * | 
 | 760 |  * Take a reference on a fence (all asics). | 
 | 761 |  * Returns the fence. | 
 | 762 |  */ | 
 | 763 | struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence) | 
 | 764 | { | 
 | 765 | 	fence_get(&fence->base); | 
 | 766 | 	return fence; | 
 | 767 | } | 
 | 768 |  | 
 | 769 | /** | 
 | 770 |  * amdgpu_fence_unref - remove a ref on a fence | 
 | 771 |  * | 
 | 772 |  * @fence: amdgpu fence object | 
 | 773 |  * | 
 | 774 |  * Remove a reference on a fence (all asics). | 
 | 775 |  */ | 
 | 776 | void amdgpu_fence_unref(struct amdgpu_fence **fence) | 
 | 777 | { | 
 | 778 | 	struct amdgpu_fence *tmp = *fence; | 
 | 779 |  | 
 | 780 | 	*fence = NULL; | 
 | 781 | 	if (tmp) | 
 | 782 | 		fence_put(&tmp->base); | 
 | 783 | } | 
 | 784 |  | 
 | 785 | /** | 
 | 786 |  * amdgpu_fence_count_emitted - get the count of emitted fences | 
 | 787 |  * | 
 | 788 |  * @ring: ring the fence is associated with | 
 | 789 |  * | 
 | 790 |  * Get the number of fences emitted on the requested ring (all asics). | 
 | 791 |  * Returns the number of emitted fences on the ring.  Used by the | 
 | 792 |  * dynpm code to ring track activity. | 
 | 793 |  */ | 
 | 794 | unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring) | 
 | 795 | { | 
 | 796 | 	uint64_t emitted; | 
 | 797 |  | 
 | 798 | 	/* We are not protected by ring lock when reading the last sequence | 
 | 799 | 	 * but it's ok to report slightly wrong fence count here. | 
 | 800 | 	 */ | 
 | 801 | 	amdgpu_fence_process(ring); | 
 | 802 | 	emitted = ring->fence_drv.sync_seq[ring->idx] | 
 | 803 | 		- atomic64_read(&ring->fence_drv.last_seq); | 
 | 804 | 	/* to avoid 32bits warp around */ | 
 | 805 | 	if (emitted > 0x10000000) | 
 | 806 | 		emitted = 0x10000000; | 
 | 807 |  | 
 | 808 | 	return (unsigned)emitted; | 
 | 809 | } | 
 | 810 |  | 
 | 811 | /** | 
 | 812 |  * amdgpu_fence_need_sync - do we need a semaphore | 
 | 813 |  * | 
 | 814 |  * @fence: amdgpu fence object | 
 | 815 |  * @dst_ring: which ring to check against | 
 | 816 |  * | 
 | 817 |  * Check if the fence needs to be synced against another ring | 
 | 818 |  * (all asics).  If so, we need to emit a semaphore. | 
 | 819 |  * Returns true if we need to sync with another ring, false if | 
 | 820 |  * not. | 
 | 821 |  */ | 
 | 822 | bool amdgpu_fence_need_sync(struct amdgpu_fence *fence, | 
 | 823 | 			    struct amdgpu_ring *dst_ring) | 
 | 824 | { | 
 | 825 | 	struct amdgpu_fence_driver *fdrv; | 
 | 826 |  | 
 | 827 | 	if (!fence) | 
 | 828 | 		return false; | 
 | 829 |  | 
 | 830 | 	if (fence->ring == dst_ring) | 
 | 831 | 		return false; | 
 | 832 |  | 
 | 833 | 	/* we are protected by the ring mutex */ | 
 | 834 | 	fdrv = &dst_ring->fence_drv; | 
 | 835 | 	if (fence->seq <= fdrv->sync_seq[fence->ring->idx]) | 
 | 836 | 		return false; | 
 | 837 |  | 
 | 838 | 	return true; | 
 | 839 | } | 
 | 840 |  | 
 | 841 | /** | 
 | 842 |  * amdgpu_fence_note_sync - record the sync point | 
 | 843 |  * | 
 | 844 |  * @fence: amdgpu fence object | 
 | 845 |  * @dst_ring: which ring to check against | 
 | 846 |  * | 
 | 847 |  * Note the sequence number at which point the fence will | 
 | 848 |  * be synced with the requested ring (all asics). | 
 | 849 |  */ | 
 | 850 | void amdgpu_fence_note_sync(struct amdgpu_fence *fence, | 
 | 851 | 			    struct amdgpu_ring *dst_ring) | 
 | 852 | { | 
 | 853 | 	struct amdgpu_fence_driver *dst, *src; | 
 | 854 | 	unsigned i; | 
 | 855 |  | 
 | 856 | 	if (!fence) | 
 | 857 | 		return; | 
 | 858 |  | 
 | 859 | 	if (fence->ring == dst_ring) | 
 | 860 | 		return; | 
 | 861 |  | 
 | 862 | 	/* we are protected by the ring mutex */ | 
 | 863 | 	src = &fence->ring->fence_drv; | 
 | 864 | 	dst = &dst_ring->fence_drv; | 
 | 865 | 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 866 | 		if (i == dst_ring->idx) | 
 | 867 | 			continue; | 
 | 868 |  | 
 | 869 | 		dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]); | 
 | 870 | 	} | 
 | 871 | } | 
 | 872 |  | 
 | 873 | /** | 
 | 874 |  * amdgpu_fence_driver_start_ring - make the fence driver | 
 | 875 |  * ready for use on the requested ring. | 
 | 876 |  * | 
 | 877 |  * @ring: ring to start the fence driver on | 
 | 878 |  * @irq_src: interrupt source to use for this ring | 
 | 879 |  * @irq_type: interrupt type to use for this ring | 
 | 880 |  * | 
 | 881 |  * Make the fence driver ready for processing (all asics). | 
 | 882 |  * Not all asics have all rings, so each asic will only | 
 | 883 |  * start the fence driver on the rings it has. | 
 | 884 |  * Returns 0 for success, errors for failure. | 
 | 885 |  */ | 
 | 886 | int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring, | 
 | 887 | 				   struct amdgpu_irq_src *irq_src, | 
 | 888 | 				   unsigned irq_type) | 
 | 889 | { | 
 | 890 | 	struct amdgpu_device *adev = ring->adev; | 
 | 891 | 	uint64_t index; | 
 | 892 |  | 
 | 893 | 	if (ring != &adev->uvd.ring) { | 
 | 894 | 		ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs]; | 
 | 895 | 		ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4); | 
 | 896 | 	} else { | 
 | 897 | 		/* put fence directly behind firmware */ | 
 | 898 | 		index = ALIGN(adev->uvd.fw->size, 8); | 
 | 899 | 		ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index; | 
 | 900 | 		ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index; | 
 | 901 | 	} | 
 | 902 | 	amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq)); | 
 | 903 | 	ring->fence_drv.initialized = true; | 
 | 904 | 	ring->fence_drv.irq_src = irq_src; | 
 | 905 | 	ring->fence_drv.irq_type = irq_type; | 
 | 906 | 	dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, " | 
 | 907 | 		 "cpu addr 0x%p\n", ring->idx, | 
 | 908 | 		 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr); | 
 | 909 | 	return 0; | 
 | 910 | } | 
 | 911 |  | 
 | 912 | /** | 
 | 913 |  * amdgpu_fence_driver_init_ring - init the fence driver | 
 | 914 |  * for the requested ring. | 
 | 915 |  * | 
 | 916 |  * @ring: ring to init the fence driver on | 
 | 917 |  * | 
 | 918 |  * Init the fence driver for the requested ring (all asics). | 
 | 919 |  * Helper function for amdgpu_fence_driver_init(). | 
 | 920 |  */ | 
 | 921 | void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring) | 
 | 922 | { | 
 | 923 | 	int i; | 
 | 924 |  | 
 | 925 | 	ring->fence_drv.cpu_addr = NULL; | 
 | 926 | 	ring->fence_drv.gpu_addr = 0; | 
 | 927 | 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) | 
 | 928 | 		ring->fence_drv.sync_seq[i] = 0; | 
 | 929 |  | 
 | 930 | 	atomic64_set(&ring->fence_drv.last_seq, 0); | 
 | 931 | 	ring->fence_drv.initialized = false; | 
 | 932 |  | 
 | 933 | 	INIT_DELAYED_WORK(&ring->fence_drv.lockup_work, | 
 | 934 | 			amdgpu_fence_check_lockup); | 
 | 935 | 	ring->fence_drv.ring = ring; | 
 | 936 | } | 
 | 937 |  | 
 | 938 | /** | 
 | 939 |  * amdgpu_fence_driver_init - init the fence driver | 
 | 940 |  * for all possible rings. | 
 | 941 |  * | 
 | 942 |  * @adev: amdgpu device pointer | 
 | 943 |  * | 
 | 944 |  * Init the fence driver for all possible rings (all asics). | 
 | 945 |  * Not all asics have all rings, so each asic will only | 
 | 946 |  * start the fence driver on the rings it has using | 
 | 947 |  * amdgpu_fence_driver_start_ring(). | 
 | 948 |  * Returns 0 for success. | 
 | 949 |  */ | 
 | 950 | int amdgpu_fence_driver_init(struct amdgpu_device *adev) | 
 | 951 | { | 
 | 952 | 	init_waitqueue_head(&adev->fence_queue); | 
 | 953 | 	if (amdgpu_debugfs_fence_init(adev)) | 
 | 954 | 		dev_err(adev->dev, "fence debugfs file creation failed\n"); | 
 | 955 |  | 
 | 956 | 	return 0; | 
 | 957 | } | 
 | 958 |  | 
 | 959 | /** | 
 | 960 |  * amdgpu_fence_driver_fini - tear down the fence driver | 
 | 961 |  * for all possible rings. | 
 | 962 |  * | 
 | 963 |  * @adev: amdgpu device pointer | 
 | 964 |  * | 
 | 965 |  * Tear down the fence driver for all possible rings (all asics). | 
 | 966 |  */ | 
 | 967 | void amdgpu_fence_driver_fini(struct amdgpu_device *adev) | 
 | 968 | { | 
 | 969 | 	int i, r; | 
 | 970 |  | 
 | 971 | 	mutex_lock(&adev->ring_lock); | 
 | 972 | 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) { | 
 | 973 | 		struct amdgpu_ring *ring = adev->rings[i]; | 
 | 974 | 		if (!ring || !ring->fence_drv.initialized) | 
 | 975 | 			continue; | 
 | 976 | 		r = amdgpu_fence_wait_empty(ring); | 
 | 977 | 		if (r) { | 
 | 978 | 			/* no need to trigger GPU reset as we are unloading */ | 
 | 979 | 			amdgpu_fence_driver_force_completion(adev); | 
 | 980 | 		} | 
 | 981 | 		wake_up_all(&adev->fence_queue); | 
 | 982 | 		ring->fence_drv.initialized = false; | 
 | 983 | 	} | 
 | 984 | 	mutex_unlock(&adev->ring_lock); | 
 | 985 | } | 
 | 986 |  | 
 | 987 | /** | 
 | 988 |  * amdgpu_fence_driver_force_completion - force all fence waiter to complete | 
 | 989 |  * | 
 | 990 |  * @adev: amdgpu device pointer | 
 | 991 |  * | 
 | 992 |  * In case of GPU reset failure make sure no process keep waiting on fence | 
 | 993 |  * that will never complete. | 
 | 994 |  */ | 
 | 995 | void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev) | 
 | 996 | { | 
 | 997 | 	int i; | 
 | 998 |  | 
 | 999 | 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) { | 
 | 1000 | 		struct amdgpu_ring *ring = adev->rings[i]; | 
 | 1001 | 		if (!ring || !ring->fence_drv.initialized) | 
 | 1002 | 			continue; | 
 | 1003 |  | 
 | 1004 | 		amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]); | 
 | 1005 | 	} | 
 | 1006 | } | 
 | 1007 |  | 
 | 1008 |  | 
 | 1009 | /* | 
 | 1010 |  * Fence debugfs | 
 | 1011 |  */ | 
 | 1012 | #if defined(CONFIG_DEBUG_FS) | 
 | 1013 | static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data) | 
 | 1014 | { | 
 | 1015 | 	struct drm_info_node *node = (struct drm_info_node *)m->private; | 
 | 1016 | 	struct drm_device *dev = node->minor->dev; | 
 | 1017 | 	struct amdgpu_device *adev = dev->dev_private; | 
 | 1018 | 	int i, j; | 
 | 1019 |  | 
 | 1020 | 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | 
 | 1021 | 		struct amdgpu_ring *ring = adev->rings[i]; | 
 | 1022 | 		if (!ring || !ring->fence_drv.initialized) | 
 | 1023 | 			continue; | 
 | 1024 |  | 
 | 1025 | 		amdgpu_fence_process(ring); | 
 | 1026 |  | 
| Christian König | 344c19f | 2015-06-02 15:47:16 +0200 | [diff] [blame] | 1027 | 		seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name); | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1028 | 		seq_printf(m, "Last signaled fence 0x%016llx\n", | 
 | 1029 | 			   (unsigned long long)atomic64_read(&ring->fence_drv.last_seq)); | 
 | 1030 | 		seq_printf(m, "Last emitted        0x%016llx\n", | 
 | 1031 | 			   ring->fence_drv.sync_seq[i]); | 
 | 1032 |  | 
 | 1033 | 		for (j = 0; j < AMDGPU_MAX_RINGS; ++j) { | 
 | 1034 | 			struct amdgpu_ring *other = adev->rings[j]; | 
| Christian König | 344c19f | 2015-06-02 15:47:16 +0200 | [diff] [blame] | 1035 | 			if (i != j && other && other->fence_drv.initialized && | 
 | 1036 | 			    ring->fence_drv.sync_seq[j]) | 
| Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1037 | 				seq_printf(m, "Last sync to ring %d 0x%016llx\n", | 
 | 1038 | 					   j, ring->fence_drv.sync_seq[j]); | 
 | 1039 | 		} | 
 | 1040 | 	} | 
 | 1041 | 	return 0; | 
 | 1042 | } | 
 | 1043 |  | 
 | 1044 | static struct drm_info_list amdgpu_debugfs_fence_list[] = { | 
 | 1045 | 	{"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL}, | 
 | 1046 | }; | 
 | 1047 | #endif | 
 | 1048 |  | 
 | 1049 | int amdgpu_debugfs_fence_init(struct amdgpu_device *adev) | 
 | 1050 | { | 
 | 1051 | #if defined(CONFIG_DEBUG_FS) | 
 | 1052 | 	return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1); | 
 | 1053 | #else | 
 | 1054 | 	return 0; | 
 | 1055 | #endif | 
 | 1056 | } | 
 | 1057 |  | 
 | 1058 | static const char *amdgpu_fence_get_driver_name(struct fence *fence) | 
 | 1059 | { | 
 | 1060 | 	return "amdgpu"; | 
 | 1061 | } | 
 | 1062 |  | 
 | 1063 | static const char *amdgpu_fence_get_timeline_name(struct fence *f) | 
 | 1064 | { | 
 | 1065 | 	struct amdgpu_fence *fence = to_amdgpu_fence(f); | 
 | 1066 | 	return (const char *)fence->ring->name; | 
 | 1067 | } | 
 | 1068 |  | 
 | 1069 | static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence) | 
 | 1070 | { | 
 | 1071 | 	return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags); | 
 | 1072 | } | 
 | 1073 |  | 
 | 1074 | struct amdgpu_wait_cb { | 
 | 1075 | 	struct fence_cb base; | 
 | 1076 | 	struct task_struct *task; | 
 | 1077 | }; | 
 | 1078 |  | 
 | 1079 | static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb) | 
 | 1080 | { | 
 | 1081 | 	struct amdgpu_wait_cb *wait = | 
 | 1082 | 		container_of(cb, struct amdgpu_wait_cb, base); | 
 | 1083 | 	wake_up_process(wait->task); | 
 | 1084 | } | 
 | 1085 |  | 
 | 1086 | static signed long amdgpu_fence_default_wait(struct fence *f, bool intr, | 
 | 1087 | 					     signed long t) | 
 | 1088 | { | 
 | 1089 | 	struct amdgpu_fence *fence = to_amdgpu_fence(f); | 
 | 1090 | 	struct amdgpu_device *adev = fence->ring->adev; | 
 | 1091 | 	struct amdgpu_wait_cb cb; | 
 | 1092 |  | 
 | 1093 | 	cb.task = current; | 
 | 1094 |  | 
 | 1095 | 	if (fence_add_callback(f, &cb.base, amdgpu_fence_wait_cb)) | 
 | 1096 | 		return t; | 
 | 1097 |  | 
 | 1098 | 	while (t > 0) { | 
 | 1099 | 		if (intr) | 
 | 1100 | 			set_current_state(TASK_INTERRUPTIBLE); | 
 | 1101 | 		else | 
 | 1102 | 			set_current_state(TASK_UNINTERRUPTIBLE); | 
 | 1103 |  | 
 | 1104 | 		/* | 
 | 1105 | 		 * amdgpu_test_signaled must be called after | 
 | 1106 | 		 * set_current_state to prevent a race with wake_up_process | 
 | 1107 | 		 */ | 
 | 1108 | 		if (amdgpu_test_signaled(fence)) | 
 | 1109 | 			break; | 
 | 1110 |  | 
 | 1111 | 		if (adev->needs_reset) { | 
 | 1112 | 			t = -EDEADLK; | 
 | 1113 | 			break; | 
 | 1114 | 		} | 
 | 1115 |  | 
 | 1116 | 		t = schedule_timeout(t); | 
 | 1117 |  | 
 | 1118 | 		if (t > 0 && intr && signal_pending(current)) | 
 | 1119 | 			t = -ERESTARTSYS; | 
 | 1120 | 	} | 
 | 1121 |  | 
 | 1122 | 	__set_current_state(TASK_RUNNING); | 
 | 1123 | 	fence_remove_callback(f, &cb.base); | 
 | 1124 |  | 
 | 1125 | 	return t; | 
 | 1126 | } | 
 | 1127 |  | 
 | 1128 | const struct fence_ops amdgpu_fence_ops = { | 
 | 1129 | 	.get_driver_name = amdgpu_fence_get_driver_name, | 
 | 1130 | 	.get_timeline_name = amdgpu_fence_get_timeline_name, | 
 | 1131 | 	.enable_signaling = amdgpu_fence_enable_signaling, | 
 | 1132 | 	.signaled = amdgpu_fence_is_signaled, | 
 | 1133 | 	.wait = amdgpu_fence_default_wait, | 
 | 1134 | 	.release = NULL, | 
 | 1135 | }; |