Home | History | Annotate | Line # | Download | only in radeon
      1  1.2  riastrad /*	$NetBSD: radeon_semaphore.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2011 Christian Knig.
      5  1.1  riastrad  * All Rights Reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      8  1.1  riastrad  * copy of this software and associated documentation files (the
      9  1.1  riastrad  * "Software"), to deal in the Software without restriction, including
     10  1.1  riastrad  * without limitation the rights to use, copy, modify, merge, publish,
     11  1.1  riastrad  * distribute, sub license, and/or sell copies of the Software, and to
     12  1.1  riastrad  * permit persons to whom the Software is furnished to do so, subject to
     13  1.1  riastrad  * the following conditions:
     14  1.1  riastrad  *
     15  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     18  1.1  riastrad  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     19  1.1  riastrad  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     20  1.1  riastrad  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     21  1.1  riastrad  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     22  1.1  riastrad  *
     23  1.1  riastrad  * The above copyright notice and this permission notice (including the
     24  1.1  riastrad  * next paragraph) shall be included in all copies or substantial portions
     25  1.1  riastrad  * of the Software.
     26  1.1  riastrad  *
     27  1.1  riastrad  */
     28  1.1  riastrad /*
     29  1.1  riastrad  * Authors:
     30  1.1  riastrad  *    Christian Knig <deathsimple (at) vodafone.de>
     31  1.1  riastrad  */
     32  1.3  riastrad 
     33  1.2  riastrad #include <sys/cdefs.h>
     34  1.2  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_semaphore.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $");
     35  1.2  riastrad 
     36  1.1  riastrad #include "radeon.h"
     37  1.1  riastrad #include "radeon_trace.h"
     38  1.1  riastrad 
     39  1.1  riastrad int radeon_semaphore_create(struct radeon_device *rdev,
     40  1.1  riastrad 			    struct radeon_semaphore **semaphore)
     41  1.1  riastrad {
     42  1.2  riastrad 	int r;
     43  1.1  riastrad 
     44  1.1  riastrad 	*semaphore = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL);
     45  1.1  riastrad 	if (*semaphore == NULL) {
     46  1.1  riastrad 		return -ENOMEM;
     47  1.1  riastrad 	}
     48  1.2  riastrad 	r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo,
     49  1.2  riastrad 			     &(*semaphore)->sa_bo, 8, 8);
     50  1.1  riastrad 	if (r) {
     51  1.1  riastrad 		kfree(*semaphore);
     52  1.1  riastrad 		*semaphore = NULL;
     53  1.1  riastrad 		return r;
     54  1.1  riastrad 	}
     55  1.1  riastrad 	(*semaphore)->waiters = 0;
     56  1.1  riastrad 	(*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo);
     57  1.1  riastrad 
     58  1.2  riastrad 	*((uint64_t *)radeon_sa_bo_cpu_addr((*semaphore)->sa_bo)) = 0;
     59  1.1  riastrad 
     60  1.1  riastrad 	return 0;
     61  1.1  riastrad }
     62  1.1  riastrad 
     63  1.1  riastrad bool radeon_semaphore_emit_signal(struct radeon_device *rdev, int ridx,
     64  1.3  riastrad 				  struct radeon_semaphore *semaphore)
     65  1.1  riastrad {
     66  1.1  riastrad 	struct radeon_ring *ring = &rdev->ring[ridx];
     67  1.1  riastrad 
     68  1.1  riastrad 	trace_radeon_semaphore_signale(ridx, semaphore);
     69  1.1  riastrad 
     70  1.1  riastrad 	if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, false)) {
     71  1.1  riastrad 		--semaphore->waiters;
     72  1.1  riastrad 
     73  1.1  riastrad 		/* for debugging lockup only, used by sysfs debug files */
     74  1.1  riastrad 		ring->last_semaphore_signal_addr = semaphore->gpu_addr;
     75  1.1  riastrad 		return true;
     76  1.1  riastrad 	}
     77  1.1  riastrad 	return false;
     78  1.1  riastrad }
     79  1.1  riastrad 
     80  1.1  riastrad bool radeon_semaphore_emit_wait(struct radeon_device *rdev, int ridx,
     81  1.3  riastrad 				struct radeon_semaphore *semaphore)
     82  1.1  riastrad {
     83  1.1  riastrad 	struct radeon_ring *ring = &rdev->ring[ridx];
     84  1.1  riastrad 
     85  1.1  riastrad 	trace_radeon_semaphore_wait(ridx, semaphore);
     86  1.1  riastrad 
     87  1.1  riastrad 	if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, true)) {
     88  1.1  riastrad 		++semaphore->waiters;
     89  1.1  riastrad 
     90  1.1  riastrad 		/* for debugging lockup only, used by sysfs debug files */
     91  1.1  riastrad 		ring->last_semaphore_wait_addr = semaphore->gpu_addr;
     92  1.1  riastrad 		return true;
     93  1.1  riastrad 	}
     94  1.1  riastrad 	return false;
     95  1.1  riastrad }
     96  1.1  riastrad 
     97  1.1  riastrad void radeon_semaphore_free(struct radeon_device *rdev,
     98  1.1  riastrad 			   struct radeon_semaphore **semaphore,
     99  1.1  riastrad 			   struct radeon_fence *fence)
    100  1.1  riastrad {
    101  1.1  riastrad 	if (semaphore == NULL || *semaphore == NULL) {
    102  1.1  riastrad 		return;
    103  1.1  riastrad 	}
    104  1.1  riastrad 	if ((*semaphore)->waiters > 0) {
    105  1.1  riastrad 		dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
    106  1.1  riastrad 			" hardware lockup imminent!\n", *semaphore);
    107  1.1  riastrad 	}
    108  1.1  riastrad 	radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence);
    109  1.1  riastrad 	kfree(*semaphore);
    110  1.1  riastrad 	*semaphore = NULL;
    111  1.1  riastrad }
    112