1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24 #include "priv.h"
25 
26 #include <core/client.h>
27 #include <engine/fifo.h>
28 
29 #include <nvif/class.h>
30 
31 static int
nvkm_dma_oclass_new(struct nvkm_device * device,const struct nvkm_oclass * oclass,void * data,u32 size,struct nvkm_object ** pobject)32 nvkm_dma_oclass_new(struct nvkm_device *device,
33 		    const struct nvkm_oclass *oclass, void *data, u32 size,
34 		    struct nvkm_object **pobject)
35 {
36 	struct nvkm_dma *dma = nvkm_dma(oclass->engine);
37 	struct nvkm_dmaobj *dmaobj = NULL;
38 	int ret;
39 
40 	ret = dma->func->class_new(dma, oclass, data, size, &dmaobj);
41 	if (dmaobj)
42 		*pobject = &dmaobj->object;
43 	return ret;
44 }
45 
46 static const struct nvkm_device_oclass
47 nvkm_dma_oclass_base = {
48 	.ctor = nvkm_dma_oclass_new,
49 };
50 
51 static int
nvkm_dma_oclass_fifo_new(const struct nvkm_oclass * oclass,void * data,u32 size,struct nvkm_object ** pobject)52 nvkm_dma_oclass_fifo_new(const struct nvkm_oclass *oclass, void *data, u32 size,
53 			 struct nvkm_object **pobject)
54 {
55 	return nvkm_dma_oclass_new(oclass->engine->subdev.device,
56 				   oclass, data, size, pobject);
57 }
58 
59 static const struct nvkm_sclass
60 nvkm_dma_sclass[] = {
61 	{ 0, 0, NV_DMA_FROM_MEMORY, NULL, nvkm_dma_oclass_fifo_new },
62 	{ 0, 0, NV_DMA_TO_MEMORY, NULL, nvkm_dma_oclass_fifo_new },
63 	{ 0, 0, NV_DMA_IN_MEMORY, NULL, nvkm_dma_oclass_fifo_new },
64 };
65 
66 static int
nvkm_dma_oclass_base_get(struct nvkm_oclass * sclass,int index,const struct nvkm_device_oclass ** class)67 nvkm_dma_oclass_base_get(struct nvkm_oclass *sclass, int index,
68 			 const struct nvkm_device_oclass **class)
69 {
70 	const int count = ARRAY_SIZE(nvkm_dma_sclass);
71 	if (index < count) {
72 		const struct nvkm_sclass *oclass = &nvkm_dma_sclass[index];
73 		sclass->base = oclass[0];
74 		sclass->engn = oclass;
75 		*class = &nvkm_dma_oclass_base;
76 		return index;
77 	}
78 	return count;
79 }
80 
81 static int
nvkm_dma_oclass_fifo_get(struct nvkm_oclass * oclass,int index)82 nvkm_dma_oclass_fifo_get(struct nvkm_oclass *oclass, int index)
83 {
84 	const int count = ARRAY_SIZE(nvkm_dma_sclass);
85 	if (index < count) {
86 		oclass->base = nvkm_dma_sclass[index];
87 		return index;
88 	}
89 	return count;
90 }
91 
92 static void *
nvkm_dma_dtor(struct nvkm_engine * engine)93 nvkm_dma_dtor(struct nvkm_engine *engine)
94 {
95 	return nvkm_dma(engine);
96 }
97 
98 static const struct nvkm_engine_func
99 nvkm_dma = {
100 	.dtor = nvkm_dma_dtor,
101 	.base.sclass = nvkm_dma_oclass_base_get,
102 	.fifo.sclass = nvkm_dma_oclass_fifo_get,
103 };
104 
105 int
nvkm_dma_new_(const struct nvkm_dma_func * func,struct nvkm_device * device,int index,struct nvkm_dma ** pdma)106 nvkm_dma_new_(const struct nvkm_dma_func *func, struct nvkm_device *device,
107 	      int index, struct nvkm_dma **pdma)
108 {
109 	struct nvkm_dma *dma;
110 
111 	if (!(dma = *pdma = kzalloc(sizeof(*dma), GFP_KERNEL)))
112 		return -ENOMEM;
113 	dma->func = func;
114 
115 	return nvkm_engine_ctor(&nvkm_dma, device, index, true, &dma->engine);
116 }
117