devtime.c revision 964:306b6856a5a0
1/*
2 * Copyright (c) 2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <linux/module.h>
30#include <linux/config.h>
31#include <linux/moduleparam.h>
32#include <linux/init.h>
33#include <linux/fs.h>
34#include <asm/uaccess.h>
35#include <linux/kernel.h>
36#include <asm/io.h>
37#include <asm/page.h>
38#include <linux/netdevice.h>
39
40#ifdef __i386__
41#include <asm/processor.h>
42#include <asm/msr.h>
43#endif
44
45#define DRIVER_AUTHOR "Ali Saidi"
46#define DRIVER_DESC   "Interface to time uncacachable read and writes to device registers"
47#define DRIVER_VER    "0.1"
48
49static unsigned long devCnt, devSum, devSsq;
50static char *dataAddr = NULL;
51static int count = 0;
52
53static inline uint32_t cycleCounter(uint32_t dep);
54
55static int __init devtime_start(void)
56{
57    uint64_t addr;
58        uint32_t t1, t2;
59    uint32_t trash;
60    int x;
61
62    struct net_device *dev;
63
64
65    printk("Devtime Driver Version %s Loaded...\n", DRIVER_VER);
66
67    if ((dataAddr != 0) && (count != 0))
68    {
69        addr = simple_strtoull(dataAddr, NULL, 0);
70
71        devSum = 0;
72        devCnt = count;
73
74        addr = ioremap(addr, PAGE_SIZE);
75        /**
76         * Make sure that the remapping actually worked. On alpha we have
77         * linear addressing, so its not a problem. But it can fail in x86
78         * if physical memory is mapped to this address.
79         */
80        if (addr)
81        {
82            printk("Preparing to read %#llx %d times.\n", addr, count);
83
84            t1 = cycleCounter(trash);
85            for (x=0; x < count; x++)
86            {
87                trash = readl(addr);
88                t2 = cycleCounter(trash);
89                devSum += t2 - t1;
90                t1 = t2;
91            }
92
93            /**
94             * Unmap the address.
95             */
96            iounmap(addr);
97
98            printk("Read Address %#llx %ld times. Average latency %ld.\n", addr, devCnt, devSum/devCnt);
99        }
100        else
101            printk("Unable to remap address. Please try again later.\n");
102    } else {
103        dev = dev_get_by_name("eth0");
104        if (dev)
105        {
106            printk("Eth0: MemStart: %#lx MemEnd: %#lx I/O Addr: %#lx\n", dev->mem_start,
107                    dev->mem_end, dev->base_addr);
108            dev_put(dev);
109        }
110        dev = 0;
111        dev = dev_get_by_name("eth1");
112        if (dev)
113        {
114            printk("Eth1: MemStart: %#lx MemEnd: %#lx I/O Addr: %#lx\n", dev->mem_start,
115                    dev->mem_end, dev->base_addr);
116            dev_put(dev);
117        }
118
119
120        printk("Required information not supplied.\n");
121    }
122
123    return 0;
124}
125
126#ifdef __i386__
127
128static inline uint32_t cycleCounter(uint32_t dep)
129{
130    uint32_t time;
131    cpuid_eax(0);
132    rdtscl(time);
133    cpuid_eax(0);
134    return time;
135}
136
137#elif __alpha__
138
139inline uint32_t cycleCounter(uint32_t dep)
140{
141    uint32_t res;
142    asm volatile ("rpcc %0, %1" : "=r"(res) : "r" (dep) : "memory");
143    return res;
144}
145#else
146#error Architecture NOT SUPPORTE
147#endif
148
149static void __exit devtime_end(void) {
150    printk("Devtime Driver Version %s Unloaded...\n", DRIVER_VER);
151}
152
153
154module_init(devtime_start);
155module_exit(devtime_end);
156
157MODULE_LICENSE("BSD");
158MODULE_AUTHOR(DRIVER_AUTHOR);
159MODULE_DESCRIPTION(DRIVER_DESC);
160module_param(dataAddr, charp, 0);
161module_param(count, int, 0);
162