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->base);
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_ctor(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 				iobj->suspend[i / 4] = nv_ro32(iobj, i);
97 		}
98 		mutex_unlock(&imem->subdev.mutex);
99 		if (ret)
100 			return ret;
101 	}
102 
103 	return nvkm_subdev_fini(&imem->subdev, suspend);
104 }
105 
106 int
107 _nvkm_instmem_init(struct nvkm_object *object)
108 {
109 	struct nvkm_instmem *imem = (void *)object;
110 	struct nvkm_instobj *iobj;
111 	int ret, i;
112 
113 	ret = nvkm_subdev_init(&imem->subdev);
114 	if (ret)
115 		return ret;
116 
117 	mutex_lock(&imem->subdev.mutex);
118 	list_for_each_entry(iobj, &imem->list, head) {
119 		if (iobj->suspend) {
120 			for (i = 0; i < iobj->size; i += 4)
121 				nv_wo32(iobj, i, iobj->suspend[i / 4]);
122 			vfree(iobj->suspend);
123 			iobj->suspend = NULL;
124 		}
125 	}
126 	mutex_unlock(&imem->subdev.mutex);
127 	return 0;
128 }
129 
130 int
131 nvkm_instmem_create_(struct nvkm_object *parent, struct nvkm_object *engine,
132 		     struct nvkm_oclass *oclass, int length, void **pobject)
133 {
134 	struct nvkm_instmem *imem;
135 	int ret;
136 
137 	ret = nvkm_subdev_create_(parent, engine, oclass, 0, "INSTMEM",
138 				  "instmem", length, pobject);
139 	imem = *pobject;
140 	if (ret)
141 		return ret;
142 
143 	INIT_LIST_HEAD(&imem->list);
144 	imem->alloc = nvkm_instmem_alloc;
145 	return 0;
146 }
147