Linux-libre 4.7.10-gnu
[librecmc/linux-libre.git] / drivers / gpu / drm / nouveau / nvkm / subdev / top / base.c
1 /*
2  * Copyright 2016 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 <bskeggs@redhat.com>
23  */
24 #include "priv.h"
25
26 struct nvkm_top_device *
27 nvkm_top_device_new(struct nvkm_top *top)
28 {
29         struct nvkm_top_device *info = kmalloc(sizeof(*info), GFP_KERNEL);
30         if (info) {
31                 info->index = NVKM_SUBDEV_NR;
32                 info->addr = 0;
33                 info->fault = -1;
34                 info->engine = -1;
35                 info->runlist = -1;
36                 info->reset = -1;
37                 info->intr = -1;
38                 list_add_tail(&info->head, &top->device);
39         }
40         return info;
41 }
42
43 u32
44 nvkm_top_reset(struct nvkm_top *top, enum nvkm_devidx index)
45 {
46         struct nvkm_top_device *info;
47
48         if (top) {
49                 list_for_each_entry(info, &top->device, head) {
50                         if (info->index == index && info->reset >= 0)
51                                 return BIT(info->reset);
52                 }
53         }
54
55         return 0;
56 }
57
58 u32
59 nvkm_top_intr(struct nvkm_top *top, u32 intr, u64 *psubdevs)
60 {
61         struct nvkm_top_device *info;
62         u64 subdevs = 0;
63         u32 handled = 0;
64
65         if (top) {
66                 list_for_each_entry(info, &top->device, head) {
67                         if (info->index != NVKM_SUBDEV_NR && info->intr >= 0) {
68                                 if (intr & BIT(info->intr)) {
69                                         subdevs |= BIT_ULL(info->index);
70                                         handled |= BIT(info->intr);
71                                 }
72                         }
73                 }
74         }
75
76         *psubdevs = subdevs;
77         return intr & ~handled;
78 }
79
80 enum nvkm_devidx
81 nvkm_top_fault(struct nvkm_top *top, int fault)
82 {
83         struct nvkm_top_device *info;
84
85         list_for_each_entry(info, &top->device, head) {
86                 if (info->fault == fault)
87                         return info->index;
88         }
89
90         return NVKM_SUBDEV_NR;
91 }
92
93 enum nvkm_devidx
94 nvkm_top_engine(struct nvkm_top *top, int index, int *runl, int *engn)
95 {
96         struct nvkm_top_device *info;
97         int n = 0;
98
99         list_for_each_entry(info, &top->device, head) {
100                 if (info->engine >= 0 && info->runlist >= 0 && n++ == index) {
101                         *runl = info->runlist;
102                         *engn = info->engine;
103                         return info->index;
104                 }
105         }
106
107         return -ENODEV;
108 }
109
110 static int
111 nvkm_top_oneinit(struct nvkm_subdev *subdev)
112 {
113         struct nvkm_top *top = nvkm_top(subdev);
114         return top->func->oneinit(top);
115 }
116
117 static void *
118 nvkm_top_dtor(struct nvkm_subdev *subdev)
119 {
120         struct nvkm_top *top = nvkm_top(subdev);
121         struct nvkm_top_device *info, *temp;
122
123         list_for_each_entry_safe(info, temp, &top->device, head) {
124                 list_del(&info->head);
125                 kfree(info);
126         }
127
128         return top;
129 }
130
131 static const struct nvkm_subdev_func
132 nvkm_top = {
133         .dtor = nvkm_top_dtor,
134         .oneinit = nvkm_top_oneinit,
135 };
136
137 int
138 nvkm_top_new_(const struct nvkm_top_func *func, struct nvkm_device *device,
139               int index, struct nvkm_top **ptop)
140 {
141         struct nvkm_top *top;
142         if (!(top = *ptop = kzalloc(sizeof(*top), GFP_KERNEL)))
143                 return -ENOMEM;
144         nvkm_subdev_ctor(&nvkm_top, device, index, &top->subdev);
145         top->func = func;
146         INIT_LIST_HEAD(&top->device);
147         return 0;
148 }