Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (C) 2016 Red Hat
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  * Authors:
0023  * Rob Clark <robdclark@gmail.com>
0024  */
0025 
0026 #define DEBUG /* for pr_debug() */
0027 
0028 #include <linux/stdarg.h>
0029 
0030 #include <linux/io.h>
0031 #include <linux/moduleparam.h>
0032 #include <linux/seq_file.h>
0033 #include <linux/slab.h>
0034 
0035 #include <drm/drm.h>
0036 #include <drm/drm_drv.h>
0037 #include <drm/drm_print.h>
0038 
0039 /*
0040  * __drm_debug: Enable debug output.
0041  * Bitmask of DRM_UT_x. See include/drm/drm_print.h for details.
0042  */
0043 unsigned int __drm_debug;
0044 EXPORT_SYMBOL(__drm_debug);
0045 
0046 MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n"
0047 "\t\tBit 0 (0x01)  will enable CORE messages (drm core code)\n"
0048 "\t\tBit 1 (0x02)  will enable DRIVER messages (drm controller code)\n"
0049 "\t\tBit 2 (0x04)  will enable KMS messages (modesetting code)\n"
0050 "\t\tBit 3 (0x08)  will enable PRIME messages (prime code)\n"
0051 "\t\tBit 4 (0x10)  will enable ATOMIC messages (atomic code)\n"
0052 "\t\tBit 5 (0x20)  will enable VBL messages (vblank code)\n"
0053 "\t\tBit 7 (0x80)  will enable LEASE messages (leasing code)\n"
0054 "\t\tBit 8 (0x100) will enable DP messages (displayport code)");
0055 module_param_named(debug, __drm_debug, int, 0600);
0056 
0057 void __drm_puts_coredump(struct drm_printer *p, const char *str)
0058 {
0059     struct drm_print_iterator *iterator = p->arg;
0060     ssize_t len;
0061 
0062     if (!iterator->remain)
0063         return;
0064 
0065     if (iterator->offset < iterator->start) {
0066         ssize_t copy;
0067 
0068         len = strlen(str);
0069 
0070         if (iterator->offset + len <= iterator->start) {
0071             iterator->offset += len;
0072             return;
0073         }
0074 
0075         copy = len - (iterator->start - iterator->offset);
0076 
0077         if (copy > iterator->remain)
0078             copy = iterator->remain;
0079 
0080         /* Copy out the bit of the string that we need */
0081         memcpy(iterator->data,
0082             str + (iterator->start - iterator->offset), copy);
0083 
0084         iterator->offset = iterator->start + copy;
0085         iterator->remain -= copy;
0086     } else {
0087         ssize_t pos = iterator->offset - iterator->start;
0088 
0089         len = min_t(ssize_t, strlen(str), iterator->remain);
0090 
0091         memcpy(iterator->data + pos, str, len);
0092 
0093         iterator->offset += len;
0094         iterator->remain -= len;
0095     }
0096 }
0097 EXPORT_SYMBOL(__drm_puts_coredump);
0098 
0099 void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf)
0100 {
0101     struct drm_print_iterator *iterator = p->arg;
0102     size_t len;
0103     char *buf;
0104 
0105     if (!iterator->remain)
0106         return;
0107 
0108     /* Figure out how big the string will be */
0109     len = snprintf(NULL, 0, "%pV", vaf);
0110 
0111     /* This is the easiest path, we've already advanced beyond the offset */
0112     if (iterator->offset + len <= iterator->start) {
0113         iterator->offset += len;
0114         return;
0115     }
0116 
0117     /* Then check if we can directly copy into the target buffer */
0118     if ((iterator->offset >= iterator->start) && (len < iterator->remain)) {
0119         ssize_t pos = iterator->offset - iterator->start;
0120 
0121         snprintf(((char *) iterator->data) + pos,
0122             iterator->remain, "%pV", vaf);
0123 
0124         iterator->offset += len;
0125         iterator->remain -= len;
0126 
0127         return;
0128     }
0129 
0130     /*
0131      * Finally, hit the slow path and make a temporary string to copy over
0132      * using _drm_puts_coredump
0133      */
0134     buf = kmalloc(len + 1, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
0135     if (!buf)
0136         return;
0137 
0138     snprintf(buf, len + 1, "%pV", vaf);
0139     __drm_puts_coredump(p, (const char *) buf);
0140 
0141     kfree(buf);
0142 }
0143 EXPORT_SYMBOL(__drm_printfn_coredump);
0144 
0145 void __drm_puts_seq_file(struct drm_printer *p, const char *str)
0146 {
0147     seq_puts(p->arg, str);
0148 }
0149 EXPORT_SYMBOL(__drm_puts_seq_file);
0150 
0151 void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf)
0152 {
0153     seq_printf(p->arg, "%pV", vaf);
0154 }
0155 EXPORT_SYMBOL(__drm_printfn_seq_file);
0156 
0157 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf)
0158 {
0159     dev_info(p->arg, "[" DRM_NAME "] %pV", vaf);
0160 }
0161 EXPORT_SYMBOL(__drm_printfn_info);
0162 
0163 void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf)
0164 {
0165     pr_debug("%s %pV", p->prefix, vaf);
0166 }
0167 EXPORT_SYMBOL(__drm_printfn_debug);
0168 
0169 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
0170 {
0171     pr_err("*ERROR* %s %pV", p->prefix, vaf);
0172 }
0173 EXPORT_SYMBOL(__drm_printfn_err);
0174 
0175 /**
0176  * drm_puts - print a const string to a &drm_printer stream
0177  * @p: the &drm printer
0178  * @str: const string
0179  *
0180  * Allow &drm_printer types that have a constant string
0181  * option to use it.
0182  */
0183 void drm_puts(struct drm_printer *p, const char *str)
0184 {
0185     if (p->puts)
0186         p->puts(p, str);
0187     else
0188         drm_printf(p, "%s", str);
0189 }
0190 EXPORT_SYMBOL(drm_puts);
0191 
0192 /**
0193  * drm_printf - print to a &drm_printer stream
0194  * @p: the &drm_printer
0195  * @f: format string
0196  */
0197 void drm_printf(struct drm_printer *p, const char *f, ...)
0198 {
0199     va_list args;
0200 
0201     va_start(args, f);
0202     drm_vprintf(p, f, &args);
0203     va_end(args);
0204 }
0205 EXPORT_SYMBOL(drm_printf);
0206 
0207 /**
0208  * drm_print_bits - print bits to a &drm_printer stream
0209  *
0210  * Print bits (in flag fields for example) in human readable form.
0211  *
0212  * @p: the &drm_printer
0213  * @value: field value.
0214  * @bits: Array with bit names.
0215  * @nbits: Size of bit names array.
0216  */
0217 void drm_print_bits(struct drm_printer *p, unsigned long value,
0218             const char * const bits[], unsigned int nbits)
0219 {
0220     bool first = true;
0221     unsigned int i;
0222 
0223     if (WARN_ON_ONCE(nbits > BITS_PER_TYPE(value)))
0224         nbits = BITS_PER_TYPE(value);
0225 
0226     for_each_set_bit(i, &value, nbits) {
0227         if (WARN_ON_ONCE(!bits[i]))
0228             continue;
0229         drm_printf(p, "%s%s", first ? "" : ",",
0230                bits[i]);
0231         first = false;
0232     }
0233     if (first)
0234         drm_printf(p, "(none)");
0235 }
0236 EXPORT_SYMBOL(drm_print_bits);
0237 
0238 void drm_dev_printk(const struct device *dev, const char *level,
0239             const char *format, ...)
0240 {
0241     struct va_format vaf;
0242     va_list args;
0243 
0244     va_start(args, format);
0245     vaf.fmt = format;
0246     vaf.va = &args;
0247 
0248     if (dev)
0249         dev_printk(level, dev, "[" DRM_NAME ":%ps] %pV",
0250                __builtin_return_address(0), &vaf);
0251     else
0252         printk("%s" "[" DRM_NAME ":%ps] %pV",
0253                level, __builtin_return_address(0), &vaf);
0254 
0255     va_end(args);
0256 }
0257 EXPORT_SYMBOL(drm_dev_printk);
0258 
0259 void drm_dev_dbg(const struct device *dev, enum drm_debug_category category,
0260          const char *format, ...)
0261 {
0262     struct va_format vaf;
0263     va_list args;
0264 
0265     if (!drm_debug_enabled(category))
0266         return;
0267 
0268     va_start(args, format);
0269     vaf.fmt = format;
0270     vaf.va = &args;
0271 
0272     if (dev)
0273         dev_printk(KERN_DEBUG, dev, "[" DRM_NAME ":%ps] %pV",
0274                __builtin_return_address(0), &vaf);
0275     else
0276         printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
0277                __builtin_return_address(0), &vaf);
0278 
0279     va_end(args);
0280 }
0281 EXPORT_SYMBOL(drm_dev_dbg);
0282 
0283 void __drm_dbg(enum drm_debug_category category, const char *format, ...)
0284 {
0285     struct va_format vaf;
0286     va_list args;
0287 
0288     if (!drm_debug_enabled(category))
0289         return;
0290 
0291     va_start(args, format);
0292     vaf.fmt = format;
0293     vaf.va = &args;
0294 
0295     printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
0296            __builtin_return_address(0), &vaf);
0297 
0298     va_end(args);
0299 }
0300 EXPORT_SYMBOL(__drm_dbg);
0301 
0302 void __drm_err(const char *format, ...)
0303 {
0304     struct va_format vaf;
0305     va_list args;
0306 
0307     va_start(args, format);
0308     vaf.fmt = format;
0309     vaf.va = &args;
0310 
0311     printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
0312            __builtin_return_address(0), &vaf);
0313 
0314     va_end(args);
0315 }
0316 EXPORT_SYMBOL(__drm_err);
0317 
0318 /**
0319  * drm_print_regset32 - print the contents of registers to a
0320  * &drm_printer stream.
0321  *
0322  * @p: the &drm printer
0323  * @regset: the list of registers to print.
0324  *
0325  * Often in driver debug, it's useful to be able to either capture the
0326  * contents of registers in the steady state using debugfs or at
0327  * specific points during operation.  This lets the driver have a
0328  * single list of registers for both.
0329  */
0330 void drm_print_regset32(struct drm_printer *p, struct debugfs_regset32 *regset)
0331 {
0332     int namelen = 0;
0333     int i;
0334 
0335     for (i = 0; i < regset->nregs; i++)
0336         namelen = max(namelen, (int)strlen(regset->regs[i].name));
0337 
0338     for (i = 0; i < regset->nregs; i++) {
0339         drm_printf(p, "%*s = 0x%08x\n",
0340                namelen, regset->regs[i].name,
0341                readl(regset->base + regset->regs[i].offset));
0342     }
0343 }
0344 EXPORT_SYMBOL(drm_print_regset32);