Home | History | Annotate | Line # | Download | only in nouveau
      1  1.6  riastrad /*	$NetBSD: nouveau_nvif.c,v 1.6 2021/12/18 23:45:32 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2014 Red Hat Inc.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     14  1.1  riastrad  * all copies or substantial portions of the Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  *
     24  1.1  riastrad  * Authors: Ben Skeggs <bskeggs (at) redhat.com>
     25  1.1  riastrad  */
     26  1.1  riastrad 
     27  1.1  riastrad /*******************************************************************************
     28  1.1  riastrad  * NVIF client driver - NVKM directly linked
     29  1.1  riastrad  ******************************************************************************/
     30  1.1  riastrad 
     31  1.1  riastrad #include <sys/cdefs.h>
     32  1.6  riastrad __KERNEL_RCSID(0, "$NetBSD: nouveau_nvif.c,v 1.6 2021/12/18 23:45:32 riastradh Exp $");
     33  1.1  riastrad 
     34  1.1  riastrad #include <core/client.h>
     35  1.1  riastrad #include <core/notify.h>
     36  1.1  riastrad #include <core/ioctl.h>
     37  1.1  riastrad 
     38  1.1  riastrad #include <nvif/client.h>
     39  1.1  riastrad #include <nvif/driver.h>
     40  1.1  riastrad #include <nvif/notify.h>
     41  1.1  riastrad #include <nvif/event.h>
     42  1.1  riastrad #include <nvif/ioctl.h>
     43  1.1  riastrad 
     44  1.6  riastrad #include "nouveau_drv.h"
     45  1.1  riastrad #include "nouveau_usif.h"
     46  1.1  riastrad 
     47  1.3  riastrad #ifdef __NetBSD__
     48  1.3  riastrad #define	__iomem	__nvif_iomem
     49  1.3  riastrad static int
     50  1.3  riastrad nvkm_client_map(void *priv, bus_space_tag_t tag, u64 busaddr, u32 size,
     51  1.3  riastrad     bus_space_handle_t *handlep, void __iomem **ptrp)
     52  1.3  riastrad {
     53  1.4  riastrad 	struct nvkm_client *client = nvxx_client(priv);
     54  1.3  riastrad 	int ret;
     55  1.3  riastrad 
     56  1.4  riastrad 	if (tag == client->mmiot &&
     57  1.4  riastrad 	    client->mmioaddr <= busaddr &&
     58  1.4  riastrad 	    busaddr - client->mmioaddr <= client->mmiosz) {
     59  1.4  riastrad 		const bus_size_t offset = busaddr - client->mmioaddr;
     60  1.4  riastrad 		if (size > client->mmiosz - offset) {
     61  1.4  riastrad 			DRM_ERROR("Invalid register access\n");
     62  1.4  riastrad 			return -EFAULT;
     63  1.4  riastrad 		}
     64  1.4  riastrad 		ret = -bus_space_subregion(client->mmiot, client->mmioh,
     65  1.4  riastrad 		    offset, size, handlep);
     66  1.4  riastrad 		if (ret)
     67  1.4  riastrad 			return ret;
     68  1.4  riastrad 		*ptrp = bus_space_vaddr(tag, *handlep);
     69  1.4  riastrad 		return 0;
     70  1.4  riastrad 	}
     71  1.4  riastrad 
     72  1.3  riastrad 	ret = -bus_space_map(tag, busaddr, size, BUS_SPACE_MAP_LINEAR,
     73  1.3  riastrad 	    handlep);
     74  1.3  riastrad 	if (ret)
     75  1.3  riastrad 		return ret;
     76  1.3  riastrad 	*ptrp = bus_space_vaddr(tag, *handlep);
     77  1.3  riastrad 	return 0;
     78  1.3  riastrad }
     79  1.3  riastrad 
     80  1.3  riastrad static void
     81  1.3  riastrad nvkm_client_unmap(void *priv, bus_space_tag_t tag, bus_space_handle_t handle,
     82  1.4  riastrad     bus_addr_t busaddr, void __iomem *ptr, u32 size)
     83  1.3  riastrad {
     84  1.4  riastrad 	struct nvkm_client *client = nvxx_client(priv);
     85  1.4  riastrad 
     86  1.4  riastrad 	KASSERTMSG(ptr == bus_space_vaddr(tag, handle),
     87  1.4  riastrad 	    "nvkm_client ptr %p != %p [bus_space_vaddr(%p, %"PRIxVADDR")]",
     88  1.4  riastrad 	    ptr, bus_space_vaddr(tag, handle), tag, handle);
     89  1.4  riastrad 
     90  1.4  riastrad 	if (tag == client->mmiot &&
     91  1.4  riastrad 	    client->mmioaddr <= busaddr &&
     92  1.4  riastrad 	    busaddr - client->mmioaddr <= client->mmiosz) {
     93  1.5  pgoyette 		__diagused const bus_size_t offset = busaddr - client->mmioaddr;
     94  1.4  riastrad 		KASSERT(size <= client->mmiosz - offset);
     95  1.4  riastrad 		/* Nothing to do to release a subregion.  */
     96  1.4  riastrad 		return;
     97  1.4  riastrad 	}
     98  1.3  riastrad 
     99  1.3  riastrad 	bus_space_unmap(tag, handle, size);
    100  1.3  riastrad }
    101  1.3  riastrad #else
    102  1.1  riastrad static void
    103  1.1  riastrad nvkm_client_unmap(void *priv, void __iomem *ptr, u32 size)
    104  1.1  riastrad {
    105  1.1  riastrad 	iounmap(ptr);
    106  1.1  riastrad }
    107  1.1  riastrad 
    108  1.1  riastrad static void __iomem *
    109  1.1  riastrad nvkm_client_map(void *priv, u64 handle, u32 size)
    110  1.1  riastrad {
    111  1.1  riastrad 	return ioremap(handle, size);
    112  1.1  riastrad }
    113  1.3  riastrad #endif
    114  1.1  riastrad 
    115  1.1  riastrad static int
    116  1.1  riastrad nvkm_client_ioctl(void *priv, bool super, void *data, u32 size, void **hack)
    117  1.1  riastrad {
    118  1.1  riastrad 	return nvkm_ioctl(priv, super, data, size, hack);
    119  1.1  riastrad }
    120  1.1  riastrad 
    121  1.1  riastrad static int
    122  1.1  riastrad nvkm_client_resume(void *priv)
    123  1.1  riastrad {
    124  1.6  riastrad 	struct nvkm_client *client = priv;
    125  1.6  riastrad 	return nvkm_object_init(&client->object);
    126  1.1  riastrad }
    127  1.1  riastrad 
    128  1.1  riastrad static int
    129  1.1  riastrad nvkm_client_suspend(void *priv)
    130  1.1  riastrad {
    131  1.1  riastrad 	struct nvkm_client *client = priv;
    132  1.6  riastrad 	return nvkm_object_fini(&client->object, true);
    133  1.1  riastrad }
    134  1.1  riastrad 
    135  1.1  riastrad static int
    136  1.1  riastrad nvkm_client_ntfy(const void *header, u32 length, const void *data, u32 size)
    137  1.1  riastrad {
    138  1.1  riastrad 	const union {
    139  1.1  riastrad 		struct nvif_notify_req_v0 v0;
    140  1.1  riastrad 	} *args = header;
    141  1.1  riastrad 	u8 route;
    142  1.1  riastrad 
    143  1.1  riastrad 	if (length == sizeof(args->v0) && args->v0.version == 0) {
    144  1.1  riastrad 		route = args->v0.route;
    145  1.1  riastrad 	} else {
    146  1.1  riastrad 		WARN_ON(1);
    147  1.1  riastrad 		return NVKM_NOTIFY_DROP;
    148  1.1  riastrad 	}
    149  1.1  riastrad 
    150  1.1  riastrad 	switch (route) {
    151  1.1  riastrad 	case NVDRM_NOTIFY_NVIF:
    152  1.1  riastrad 		return nvif_notify(header, length, data, size);
    153  1.1  riastrad 	case NVDRM_NOTIFY_USIF:
    154  1.1  riastrad 		return usif_notify(header, length, data, size);
    155  1.1  riastrad 	default:
    156  1.1  riastrad 		WARN_ON(1);
    157  1.1  riastrad 		break;
    158  1.1  riastrad 	}
    159  1.1  riastrad 
    160  1.1  riastrad 	return NVKM_NOTIFY_DROP;
    161  1.1  riastrad }
    162  1.1  riastrad 
    163  1.1  riastrad static int
    164  1.1  riastrad nvkm_client_driver_init(const char *name, u64 device, const char *cfg,
    165  1.1  riastrad 			const char *dbg, void **ppriv)
    166  1.1  riastrad {
    167  1.6  riastrad 	return nvkm_client_new(name, device, cfg, dbg, nvkm_client_ntfy,
    168  1.6  riastrad 			       (struct nvkm_client **)ppriv);
    169  1.1  riastrad }
    170  1.1  riastrad 
    171  1.1  riastrad const struct nvif_driver
    172  1.1  riastrad nvif_driver_nvkm = {
    173  1.1  riastrad 	.name = "nvkm",
    174  1.1  riastrad 	.init = nvkm_client_driver_init,
    175  1.1  riastrad 	.suspend = nvkm_client_suspend,
    176  1.1  riastrad 	.resume = nvkm_client_resume,
    177  1.1  riastrad 	.ioctl = nvkm_client_ioctl,
    178  1.1  riastrad 	.map = nvkm_client_map,
    179  1.1  riastrad 	.unmap = nvkm_client_unmap,
    180  1.1  riastrad 	.keep = false,
    181  1.1  riastrad };
    182