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/engine.h>
27 
28 /******************************************************************************
29  * instmem object base implementation
30  *****************************************************************************/
31 
32 void
33 _nvkm_instobj_dtor(struct nvkm_object *object)
34 {
35 	struct nvkm_instmem *imem = nvkm_instmem(object);
36 	struct nvkm_instobj *iobj = (void *)object;
37 
38 	mutex_lock(&nv_subdev(imem)->mutex);
39 	list_del(&iobj->head);
40 	mutex_unlock(&nv_subdev(imem)->mutex);
41 
42 	return nvkm_object_destroy(&iobj->object);
43 }
44 
45 int
46 nvkm_instobj_create_(struct nvkm_object *parent, struct nvkm_object *engine,
47 		     struct nvkm_oclass *oclass, int length, void **pobject)
48 {
49 	struct nvkm_instmem *imem = nvkm_instmem(parent);
50 	struct nvkm_instobj *iobj;
51 	int ret;
52 
53 	ret = nvkm_object_create_(parent, engine, oclass, NV_MEMOBJ_CLASS,
54 				  length, pobject);
55 	iobj = *pobject;
56 	if (ret)
57 		return ret;
58 
59 	mutex_lock(&imem->subdev.mutex);
60 	list_add(&iobj->head, &imem->list);
61 	mutex_unlock(&imem->subdev.mutex);
62 	return 0;
63 }
64 
65 /******************************************************************************
66  * instmem subdev base implementation
67  *****************************************************************************/
68 
69 static int
70 nvkm_instmem_alloc(struct nvkm_instmem *imem, struct nvkm_object *parent,
71 		   u32 size, u32 align, struct nvkm_object **pobject)
72 {
73 	struct nvkm_instmem_impl *impl = (void *)imem->subdev.object.oclass;
74 	struct nvkm_instobj_args args = { .size = size, .align = align };
75 	return nvkm_object_old(parent, &parent->engine->subdev.object,
76 				impl->instobj, &args, sizeof(args), pobject);
77 }
78 
79 int
80 _nvkm_instmem_fini(struct nvkm_object *object, bool suspend)
81 {
82 	struct nvkm_instmem *imem = (void *)object;
83 	struct nvkm_instobj *iobj;
84 	int i, ret = 0;
85 
86 	if (suspend) {
87 		mutex_lock(&imem->subdev.mutex);
88 		list_for_each_entry(iobj, &imem->list, head) {
89 			iobj->suspend = vmalloc(iobj->size);
90 			if (!iobj->suspend) {
91 				ret = -ENOMEM;
92 				break;
93 			}
94 
95 			for (i = 0; i < iobj->size; i += 4) {
96 				nvkm_object_rd32(&iobj->object, i, (u32 *)
97 						 &iobj->suspend[i/4]);
98 			}
99 		}
100 		mutex_unlock(&imem->subdev.mutex);
101 		if (ret)
102 			return ret;
103 	}
104 
105 	return nvkm_subdev_fini_old(&imem->subdev, suspend);
106 }
107 
108 int
109 _nvkm_instmem_init(struct nvkm_object *object)
110 {
111 	struct nvkm_instmem *imem = (void *)object;
112 	struct nvkm_instobj *iobj;
113 	int ret, i;
114 
115 	ret = nvkm_subdev_init_old(&imem->subdev);
116 	if (ret)
117 		return ret;
118 
119 	mutex_lock(&imem->subdev.mutex);
120 	list_for_each_entry(iobj, &imem->list, head) {
121 		if (iobj->suspend) {
122 			for (i = 0; i < iobj->size; i += 4) {
123 				nvkm_object_wr32(&iobj->object, i, *(u32 *)
124 						 &iobj->suspend[i/4]);
125 			}
126 			vfree(iobj->suspend);
127 			iobj->suspend = NULL;
128 		}
129 	}
130 	mutex_unlock(&imem->subdev.mutex);
131 	return 0;
132 }
133 
134 int
135 nvkm_instmem_create_(struct nvkm_object *parent, struct nvkm_object *engine,
136 		     struct nvkm_oclass *oclass, int length, void **pobject)
137 {
138 	struct nvkm_instmem *imem;
139 	int ret;
140 
141 	ret = nvkm_subdev_create_(parent, engine, oclass, 0, "INSTMEM",
142 				  "instmem", length, pobject);
143 	imem = *pobject;
144 	if (ret)
145 		return ret;
146 
147 	INIT_LIST_HEAD(&imem->list);
148 	imem->alloc = nvkm_instmem_alloc;
149 	return 0;
150 }
151