1 /*- 2 * Copyright 2003 Eric Anholt 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 * 23 * $FreeBSD: src/sys/dev/drm2/drm_sysctl.c,v 1.1 2012/05/22 11:07:44 kib Exp $ 24 */ 25 26 /** @file drm_sysctl.c 27 * Implementation of various sysctls for controlling DRM behavior and reporting 28 * debug information. 29 */ 30 31 #include <sys/conf.h> 32 #include <sys/sysctl.h> 33 #include <sys/types.h> 34 35 #include <drm/drmP.h> 36 37 SYSCTL_NODE(_hw, OID_AUTO, dri, CTLFLAG_RD, 0, "DRI Graphics"); 38 SYSCTL_INT(_hw_dri, OID_AUTO, debug, CTLFLAG_RW, &drm_debug, 0, 39 "Enable debugging output"); 40 SYSCTL_INT(_hw_dri, OID_AUTO, notyet, CTLFLAG_RW, &drm_notyet_flag, 0, 41 "Enable notyet reminders"); 42 SYSCTL_INT(_hw_dri, OID_AUTO, vblank_offdelay, CTLFLAG_RW, 43 &drm_vblank_offdelay, 0, "Delay until vblank irq auto-disable"); 44 SYSCTL_INT(_hw_dri, OID_AUTO, timestamp_precision, CTLFLAG_RW, 45 &drm_timestamp_precision, 0, "Max. error on timestamps"); 46 47 static int drm_name_info DRM_SYSCTL_HANDLER_ARGS; 48 static int drm_vm_info DRM_SYSCTL_HANDLER_ARGS; 49 static int drm_clients_info DRM_SYSCTL_HANDLER_ARGS; 50 static int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS; 51 52 struct drm_sysctl_list { 53 const char *name; 54 int (*f) DRM_SYSCTL_HANDLER_ARGS; 55 } drm_sysctl_list[] = { 56 {"name", drm_name_info}, 57 {"vm", drm_vm_info}, 58 {"clients", drm_clients_info}, 59 {"bufs", drm_bufs_info}, 60 }; 61 #define DRM_SYSCTL_ENTRIES NELEM(drm_sysctl_list) 62 63 int drm_sysctl_init(struct drm_device *dev) 64 { 65 struct drm_sysctl_info *info; 66 struct sysctl_oid *oid; 67 struct sysctl_oid *top; 68 int i, unit; 69 70 info = kmalloc(sizeof *info, M_DRM, M_WAITOK | M_ZERO); 71 if ( !info ) 72 return 1; 73 dev->sysctl = info; 74 75 unit = device_get_unit(dev->dev); 76 if (unit > 9) 77 return 1; 78 79 /* Add the hw.dri.x for our device */ 80 info->name[0] = '0' + unit; 81 info->name[1] = 0; 82 top = SYSCTL_ADD_NODE(&info->ctx, &SYSCTL_NODE_CHILDREN(_hw, dri), 83 OID_AUTO, info->name, CTLFLAG_RW, NULL, NULL); 84 if (!top) 85 return 1; 86 87 for (i = 0; i < DRM_SYSCTL_ENTRIES; i++) { 88 oid = SYSCTL_ADD_OID(&info->ctx, 89 SYSCTL_CHILDREN(top), 90 OID_AUTO, 91 drm_sysctl_list[i].name, 92 CTLTYPE_STRING | CTLFLAG_RD, 93 dev, 94 0, 95 drm_sysctl_list[i].f, 96 "A", 97 NULL); 98 if (!oid) 99 return 1; 100 } 101 if (dev->driver->sysctl_init != NULL) 102 dev->driver->sysctl_init(dev, &info->ctx, top); 103 104 return (0); 105 } 106 107 int drm_sysctl_cleanup(struct drm_device *dev) 108 { 109 int error; 110 111 error = sysctl_ctx_free(&dev->sysctl->ctx); 112 drm_free(dev->sysctl, M_DRM); 113 dev->sysctl = NULL; 114 if (dev->driver->sysctl_cleanup != NULL) 115 dev->driver->sysctl_cleanup(dev); 116 117 return (error); 118 } 119 120 #define DRM_SYSCTL_PRINT(fmt, arg...) \ 121 do { \ 122 ksnprintf(buf, sizeof(buf), fmt, ##arg); \ 123 retcode = SYSCTL_OUT(req, buf, strlen(buf)); \ 124 if (retcode) \ 125 goto done; \ 126 } while (0) 127 128 static int drm_name_info DRM_SYSCTL_HANDLER_ARGS 129 { 130 struct drm_device *dev = arg1; 131 char buf[128]; 132 int retcode; 133 int hasunique = 0; 134 135 DRM_SYSCTL_PRINT("%s 0x%x", dev->driver->name, dev2udev(dev->devnode)); 136 137 DRM_LOCK(dev); 138 if (dev->unique) { 139 ksnprintf(buf, sizeof(buf), " %s", dev->unique); 140 hasunique = 1; 141 } 142 DRM_UNLOCK(dev); 143 144 if (hasunique) 145 SYSCTL_OUT(req, buf, strlen(buf)); 146 147 SYSCTL_OUT(req, "", 1); 148 149 done: 150 return retcode; 151 } 152 153 /** 154 * Called when "/proc/dri/.../vm" is read. 155 * 156 * Prints information about all mappings in drm_device::maplist. 157 */ 158 static int drm_vm_info DRM_SYSCTL_HANDLER_ARGS 159 { 160 char buf[128]; 161 int retcode; 162 struct drm_device *dev = arg1; 163 struct drm_local_map *map; 164 struct drm_map_list *r_list; 165 166 /* Hardcoded from _DRM_FRAME_BUFFER, 167 _DRM_REGISTERS, _DRM_SHM, _DRM_AGP, and 168 _DRM_SCATTER_GATHER and _DRM_CONSISTENT */ 169 const char *types[] = { "FB", "REG", "SHM", "AGP", "SG", "PCI" }; 170 const char *type; 171 int i; 172 173 DRM_LOCK(dev); 174 DRM_SYSCTL_PRINT("\nslot offset size " 175 "type flags address handle mtrr\n"); 176 i = 0; 177 list_for_each_entry(r_list, &dev->maplist, head) { 178 map = r_list->map; 179 if (!map) 180 continue; 181 if (map->type < 0 || map->type > 5) 182 type = "??"; 183 else 184 type = types[map->type]; 185 186 DRM_SYSCTL_PRINT("%4d 0x%016llx 0x%08lx %4.4s 0x%02x 0x%08lx ", 187 i, 188 (unsigned long long)map->offset, 189 map->size, type, map->flags, 190 (unsigned long) r_list->user_token); 191 if (map->mtrr < 0) 192 DRM_SYSCTL_PRINT("none\n"); 193 else 194 DRM_SYSCTL_PRINT("%4d\n", map->mtrr); 195 i++; 196 197 } 198 SYSCTL_OUT(req, "", 1); 199 DRM_UNLOCK(dev); 200 201 done: 202 return 0; 203 } 204 205 static int drm_bufs_info DRM_SYSCTL_HANDLER_ARGS 206 { 207 struct drm_device *dev = arg1; 208 drm_device_dma_t *dma = dev->dma; 209 drm_device_dma_t tempdma; 210 int *templists; 211 int i; 212 char buf[128]; 213 int retcode; 214 215 /* We can't hold the locks around DRM_SYSCTL_PRINT, so make a temporary 216 * copy of the whole structure and the relevant data from buflist. 217 */ 218 DRM_LOCK(dev); 219 if (dma == NULL) { 220 DRM_UNLOCK(dev); 221 return 0; 222 } 223 spin_lock(&dev->dma_lock); 224 tempdma = *dma; 225 templists = kmalloc(sizeof(int) * dma->buf_count, M_DRM, 226 M_WAITOK | M_NULLOK); 227 for (i = 0; i < dma->buf_count; i++) 228 templists[i] = dma->buflist[i]->list; 229 dma = &tempdma; 230 spin_unlock(&dev->dma_lock); 231 DRM_UNLOCK(dev); 232 233 DRM_SYSCTL_PRINT("\n o size count free segs pages kB\n"); 234 for (i = 0; i <= DRM_MAX_ORDER; i++) { 235 if (dma->bufs[i].buf_count) 236 DRM_SYSCTL_PRINT("%2d %8d %5d %5d %5d %5d\n", 237 i, 238 dma->bufs[i].buf_size, 239 dma->bufs[i].buf_count, 240 dma->bufs[i].seg_count, 241 dma->bufs[i].seg_count 242 *(1 << dma->bufs[i].page_order), 243 (dma->bufs[i].seg_count 244 * (1 << dma->bufs[i].page_order)) 245 * (int)PAGE_SIZE / 1024); 246 } 247 DRM_SYSCTL_PRINT("\n"); 248 for (i = 0; i < dma->buf_count; i++) { 249 if (i && !(i%32)) DRM_SYSCTL_PRINT("\n"); 250 DRM_SYSCTL_PRINT(" %d", templists[i]); 251 } 252 DRM_SYSCTL_PRINT("\n"); 253 254 SYSCTL_OUT(req, "", 1); 255 done: 256 drm_free(templists, M_DRM); 257 return retcode; 258 } 259 260 static int drm_clients_info DRM_SYSCTL_HANDLER_ARGS 261 { 262 struct drm_device *dev = arg1; 263 struct drm_file *priv, *tempprivs; 264 char buf[128]; 265 int retcode; 266 int privcount, i; 267 268 DRM_LOCK(dev); 269 270 privcount = 0; 271 list_for_each_entry(priv, &dev->filelist, lhead) 272 privcount++; 273 274 tempprivs = kmalloc(sizeof(struct drm_file) * privcount, M_DRM, 275 M_WAITOK | M_NULLOK); 276 if (tempprivs == NULL) { 277 DRM_UNLOCK(dev); 278 return ENOMEM; 279 } 280 i = 0; 281 list_for_each_entry(priv, &dev->filelist, lhead) 282 tempprivs[i++] = *priv; 283 284 DRM_UNLOCK(dev); 285 286 DRM_SYSCTL_PRINT( 287 "\na dev pid uid magic ioctls\n"); 288 for (i = 0; i < privcount; i++) { 289 priv = &tempprivs[i]; 290 DRM_SYSCTL_PRINT("%c %-12s %5d %5d %10u %10lu\n", 291 priv->authenticated ? 'y' : 'n', 292 devtoname(priv->dev->devnode), 293 priv->pid, 294 priv->uid, 295 priv->magic, 296 priv->ioctl_count); 297 } 298 299 SYSCTL_OUT(req, "", 1); 300 done: 301 drm_free(tempprivs, M_DRM); 302 return retcode; 303 } 304