Linux Audio

Check our new training course

Yocto distribution development and maintenance

Need a Yocto distribution for your embedded project?
Loading...
Note: File does not exist in v3.1.
  1/*
  2 * Copyright 2013 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
 25#include "nouveau_sysfs.h"
 26
 27#include <core/object.h>
 28#include <core/class.h>
 29
 30static inline struct drm_device *
 31drm_device(struct device *d)
 32{
 33	return dev_get_drvdata(d);
 34}
 35
 36#define snappendf(p,r,f,a...) do {                                             \
 37	snprintf(p, r, f, ##a);                                                \
 38	r -= strlen(p);                                                        \
 39	p += strlen(p);                                                        \
 40} while(0)
 41
 42static ssize_t
 43nouveau_sysfs_pstate_get(struct device *d, struct device_attribute *a, char *b)
 44{
 45	struct nouveau_sysfs *sysfs = nouveau_sysfs(drm_device(d));
 46	struct nv_control_pstate_info info;
 47	size_t cnt = PAGE_SIZE;
 48	char *buf = b;
 49	int ret, i;
 50
 51	ret = nv_exec(sysfs->ctrl, NV_CONTROL_PSTATE_INFO, &info, sizeof(info));
 52	if (ret)
 53		return ret;
 54
 55	for (i = 0; i < info.count + 1; i++) {
 56		const s32 state = i < info.count ? i :
 57			NV_CONTROL_PSTATE_ATTR_STATE_CURRENT;
 58		struct nv_control_pstate_attr attr = {
 59			.state = state,
 60			.index = 0,
 61		};
 62
 63		ret = nv_exec(sysfs->ctrl, NV_CONTROL_PSTATE_ATTR,
 64			     &attr, sizeof(attr));
 65		if (ret)
 66			return ret;
 67
 68		if (i < info.count)
 69			snappendf(buf, cnt, "%02x:", attr.state);
 70		else
 71			snappendf(buf, cnt, "--:");
 72
 73		attr.index = 0;
 74		do {
 75			attr.state = state;
 76			ret = nv_exec(sysfs->ctrl, NV_CONTROL_PSTATE_ATTR,
 77				     &attr, sizeof(attr));
 78			if (ret)
 79				return ret;
 80
 81			snappendf(buf, cnt, " %s %d", attr.name, attr.min);
 82			if (attr.min != attr.max)
 83				snappendf(buf, cnt, "-%d", attr.max);
 84			snappendf(buf, cnt, " %s", attr.unit);
 85		} while (attr.index);
 86
 87		if ((state >= 0 && info.pstate == state) ||
 88		    (state <  0 && info.ustate < 0))
 89			snappendf(buf, cnt, " *");
 90		snappendf(buf, cnt, "\n");
 91	}
 92
 93	return strlen(b);
 94}
 95
 96static ssize_t
 97nouveau_sysfs_pstate_set(struct device *d, struct device_attribute *a,
 98			 const char *buf, size_t count)
 99{
100	struct nouveau_sysfs *sysfs = nouveau_sysfs(drm_device(d));
101	struct nv_control_pstate_user args;
102	long value, ret;
103	char *tmp;
104
105	if ((tmp = strchr(buf, '\n')))
106		*tmp = '\0';
107
108	if (!strcasecmp(buf, "none"))
109		args.state = NV_CONTROL_PSTATE_USER_STATE_UNKNOWN;
110	else
111	if (!strcasecmp(buf, "auto"))
112		args.state = NV_CONTROL_PSTATE_USER_STATE_PERFMON;
113	else {
114		ret = kstrtol(buf, 16, &value);
115		if (ret)
116			return ret;
117		args.state = value;
118	}
119
120	ret = nv_exec(sysfs->ctrl, NV_CONTROL_PSTATE_USER, &args, sizeof(args));
121	if (ret < 0)
122		return ret;
123
124	return count;
125}
126
127static DEVICE_ATTR(pstate, S_IRUGO | S_IWUSR,
128		   nouveau_sysfs_pstate_get, nouveau_sysfs_pstate_set);
129
130void
131nouveau_sysfs_fini(struct drm_device *dev)
132{
133	struct nouveau_sysfs *sysfs = nouveau_sysfs(dev);
134	struct nouveau_drm *drm = nouveau_drm(dev);
135	struct nouveau_device *device = nv_device(drm->device);
136
137	if (sysfs->ctrl) {
138		device_remove_file(nv_device_base(device), &dev_attr_pstate);
139		nouveau_object_del(nv_object(drm), NVDRM_DEVICE, NVDRM_CONTROL);
140	}
141
142	drm->sysfs = NULL;
143	kfree(sysfs);
144}
145
146int
147nouveau_sysfs_init(struct drm_device *dev)
148{
149	struct nouveau_drm *drm = nouveau_drm(dev);
150	struct nouveau_device *device = nv_device(drm->device);
151	struct nouveau_sysfs *sysfs;
152	int ret;
153
154	sysfs = drm->sysfs = kzalloc(sizeof(*sysfs), GFP_KERNEL);
155	if (!sysfs)
156		return -ENOMEM;
157
158	ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE, NVDRM_CONTROL,
159				 NV_CONTROL_CLASS, NULL, 0, &sysfs->ctrl);
160	if (ret == 0)
161		device_create_file(nv_device_base(device), &dev_attr_pstate);
162
163	return 0;
164}