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 <core/subdev.h>
25#include <core/device.h>
26#include <core/option.h>
27
28struct nvkm_subdev *
29nvkm_subdev(void *obj, int idx)
30{
31	struct nvkm_object *object = nv_object(obj);
32	while (object && !nv_iclass(object, NV_SUBDEV_CLASS))
33		object = object->parent;
34	if (object == NULL || nv_subidx(nv_subdev(object)) != idx)
35		object = nv_device(obj)->subdev[idx];
36	return object ? nv_subdev(object) : NULL;
37}
38
39void
40nvkm_subdev_reset(struct nvkm_object *subdev)
41{
42	nv_trace(subdev, "resetting...\n");
43	nv_ofuncs(subdev)->fini(subdev, false);
44	nv_debug(subdev, "reset\n");
45}
46
47int
48nvkm_subdev_init(struct nvkm_subdev *subdev)
49{
50	int ret = nvkm_object_init(&subdev->object);
51	if (ret)
52		return ret;
53
54	nvkm_subdev_reset(&subdev->object);
55	return 0;
56}
57
58int
59_nvkm_subdev_init(struct nvkm_object *object)
60{
61	return nvkm_subdev_init(nv_subdev(object));
62}
63
64int
65nvkm_subdev_fini(struct nvkm_subdev *subdev, bool suspend)
66{
67	if (subdev->unit) {
68		nv_mask(subdev, 0x000200, subdev->unit, 0x00000000);
69		nv_mask(subdev, 0x000200, subdev->unit, subdev->unit);
70	}
71
72	return nvkm_object_fini(&subdev->object, suspend);
73}
74
75int
76_nvkm_subdev_fini(struct nvkm_object *object, bool suspend)
77{
78	return nvkm_subdev_fini(nv_subdev(object), suspend);
79}
80
81void
82nvkm_subdev_destroy(struct nvkm_subdev *subdev)
83{
84	int subidx = nv_hclass(subdev) & 0xff;
85	nv_device(subdev)->subdev[subidx] = NULL;
86	nvkm_object_destroy(&subdev->object);
87}
88
89void
90_nvkm_subdev_dtor(struct nvkm_object *object)
91{
92	nvkm_subdev_destroy(nv_subdev(object));
93}
94
95int
96nvkm_subdev_create_(struct nvkm_object *parent, struct nvkm_object *engine,
97		    struct nvkm_oclass *oclass, u32 pclass,
98		    const char *subname, const char *sysname,
99		    int size, void **pobject)
100{
101	struct nvkm_subdev *subdev;
102	int ret;
103
104	ret = nvkm_object_create_(parent, engine, oclass, pclass |
105				  NV_SUBDEV_CLASS, size, pobject);
106	subdev = *pobject;
107	if (ret)
108		return ret;
109
110	__mutex_init(&subdev->mutex, subname, &oclass->lock_class_key);
111	subdev->name = subname;
112
113	if (parent) {
114		struct nvkm_device *device = nv_device(parent);
115		subdev->debug = nvkm_dbgopt(device->dbgopt, subname);
116		subdev->mmio  = nv_subdev(device)->mmio;
117	}
118
119	return 0;
120}
121