Linux-libre 3.10.80-gnu
[librecmc/linux-libre.git] / drivers / gpu / drm / nouveau / core / subdev / instmem / base.c
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
25 #include <subdev/instmem.h>
26
27 int
28 nouveau_instobj_create_(struct nouveau_object *parent,
29                         struct nouveau_object *engine,
30                         struct nouveau_oclass *oclass,
31                         int length, void **pobject)
32 {
33         struct nouveau_instmem *imem = (void *)engine;
34         struct nouveau_instobj *iobj;
35         int ret;
36
37         ret = nouveau_object_create_(parent, engine, oclass, NV_MEMOBJ_CLASS,
38                                      length, pobject);
39         iobj = *pobject;
40         if (ret)
41                 return ret;
42
43         mutex_lock(&imem->base.mutex);
44         list_add(&iobj->head, &imem->list);
45         mutex_unlock(&imem->base.mutex);
46         return 0;
47 }
48
49 void
50 nouveau_instobj_destroy(struct nouveau_instobj *iobj)
51 {
52         struct nouveau_subdev *subdev = nv_subdev(iobj->base.engine);
53
54         mutex_lock(&subdev->mutex);
55         list_del(&iobj->head);
56         mutex_unlock(&subdev->mutex);
57
58         return nouveau_object_destroy(&iobj->base);
59 }
60
61 void
62 _nouveau_instobj_dtor(struct nouveau_object *object)
63 {
64         struct nouveau_instobj *iobj = (void *)object;
65         return nouveau_instobj_destroy(iobj);
66 }
67
68 int
69 nouveau_instmem_create_(struct nouveau_object *parent,
70                         struct nouveau_object *engine,
71                         struct nouveau_oclass *oclass,
72                         int length, void **pobject)
73 {
74         struct nouveau_instmem *imem;
75         int ret;
76
77         ret = nouveau_subdev_create_(parent, engine, oclass, 0,
78                                      "INSTMEM", "instmem", length, pobject);
79         imem = *pobject;
80         if (ret)
81                 return ret;
82
83         INIT_LIST_HEAD(&imem->list);
84         return 0;
85 }
86
87 int
88 nouveau_instmem_init(struct nouveau_instmem *imem)
89 {
90         struct nouveau_instobj *iobj;
91         int ret, i;
92
93         ret = nouveau_subdev_init(&imem->base);
94         if (ret)
95                 return ret;
96
97         mutex_lock(&imem->base.mutex);
98
99         list_for_each_entry(iobj, &imem->list, head) {
100                 if (iobj->suspend) {
101                         for (i = 0; i < iobj->size; i += 4)
102                                 nv_wo32(iobj, i, iobj->suspend[i / 4]);
103                         vfree(iobj->suspend);
104                         iobj->suspend = NULL;
105                 }
106         }
107
108         mutex_unlock(&imem->base.mutex);
109
110         return 0;
111 }
112
113 int
114 nouveau_instmem_fini(struct nouveau_instmem *imem, bool suspend)
115 {
116         struct nouveau_instobj *iobj;
117         int i, ret = 0;
118
119         if (suspend) {
120                 mutex_lock(&imem->base.mutex);
121
122                 list_for_each_entry(iobj, &imem->list, head) {
123                         iobj->suspend = vmalloc(iobj->size);
124                         if (!iobj->suspend) {
125                                 ret = -ENOMEM;
126                                 break;
127                         }
128
129                         for (i = 0; i < iobj->size; i += 4)
130                                 iobj->suspend[i / 4] = nv_ro32(iobj, i);
131                 }
132
133                 mutex_unlock(&imem->base.mutex);
134
135                 if (ret)
136                         return ret;
137         }
138
139         return nouveau_subdev_fini(&imem->base, suspend);
140 }
141
142 int
143 _nouveau_instmem_init(struct nouveau_object *object)
144 {
145         struct nouveau_instmem *imem = (void *)object;
146         return nouveau_instmem_init(imem);
147 }
148
149 int
150 _nouveau_instmem_fini(struct nouveau_object *object, bool suspend)
151 {
152         struct nouveau_instmem *imem = (void *)object;
153         return nouveau_instmem_fini(imem, suspend);
154 }