Ben Skeggs | 26fdd78 | 2013-10-15 11:52:56 +1000 | [diff] [blame^] | 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 | |
| 30 | static inline struct drm_device * |
| 31 | drm_device(struct device *d) |
| 32 | { |
| 33 | return pci_get_drvdata(to_pci_dev(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 | |
| 42 | static ssize_t |
| 43 | nouveau_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 | |
| 96 | static ssize_t |
| 97 | nouveau_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 | |
| 127 | static DEVICE_ATTR(pstate, S_IRUGO | S_IWUSR, |
| 128 | nouveau_sysfs_pstate_get, nouveau_sysfs_pstate_set); |
| 129 | |
| 130 | void |
| 131 | nouveau_sysfs_fini(struct drm_device *dev) |
| 132 | { |
| 133 | struct nouveau_sysfs *sysfs = nouveau_sysfs(dev); |
| 134 | struct nouveau_drm *drm = nouveau_drm(dev); |
| 135 | |
| 136 | if (sysfs->ctrl) { |
| 137 | device_remove_file(&dev->pdev->dev, &dev_attr_pstate); |
| 138 | nouveau_object_del(nv_object(drm), NVDRM_DEVICE, NVDRM_CONTROL); |
| 139 | } |
| 140 | |
| 141 | drm->sysfs = NULL; |
| 142 | kfree(sysfs); |
| 143 | } |
| 144 | |
| 145 | int |
| 146 | nouveau_sysfs_init(struct drm_device *dev) |
| 147 | { |
| 148 | struct nouveau_drm *drm = nouveau_drm(dev); |
| 149 | struct nouveau_sysfs *sysfs; |
| 150 | int ret; |
| 151 | |
| 152 | sysfs = drm->sysfs = kzalloc(sizeof(*sysfs), GFP_KERNEL); |
| 153 | if (!sysfs) |
| 154 | return -ENOMEM; |
| 155 | |
| 156 | ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE, NVDRM_CONTROL, |
| 157 | NV_CONTROL_CLASS, NULL, 0, &sysfs->ctrl); |
| 158 | if (ret == 0) |
| 159 | device_create_file(&dev->pdev->dev, &dev_attr_pstate); |
| 160 | |
| 161 | return 0; |
| 162 | } |