devtime.c revision 996:439a9c897004
1955SN/A/*
2955SN/A * Copyright (c) 2004 The Regents of The University of Michigan
31762SN/A * All rights reserved.
4955SN/A *
5955SN/A * Redistribution and use in source and binary forms, with or without
6955SN/A * modification, are permitted provided that the following conditions are
7955SN/A * met: redistributions of source code must retain the above copyright
8955SN/A * notice, this list of conditions and the following disclaimer;
9955SN/A * redistributions in binary form must reproduce the above copyright
10955SN/A * notice, this list of conditions and the following disclaimer in the
11955SN/A * documentation and/or other materials provided with the distribution;
12955SN/A * neither the name of the copyright holders nor the names of its
13955SN/A * contributors may be used to endorse or promote products derived from
14955SN/A * this software without specific prior written permission.
15955SN/A *
16955SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17955SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18955SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19955SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20955SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21955SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22955SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23955SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24955SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25955SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26955SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27955SN/A */
282665Ssaidi@eecs.umich.edu
294762Snate@binkert.org#include <linux/module.h>
30955SN/A#include <linux/config.h>
315522Snate@binkert.org#include <linux/moduleparam.h>
326143Snate@binkert.org#include <linux/init.h>
334762Snate@binkert.org#include <linux/fs.h>
345522Snate@binkert.org#include <asm/uaccess.h>
35955SN/A#include <linux/kernel.h>
365522Snate@binkert.org#include <asm/io.h>
37955SN/A#include <asm/page.h>
385522Snate@binkert.org#include <linux/netdevice.h>
394202Sbinkertn@umich.edu
405742Snate@binkert.org#ifdef __i386__
41955SN/A#include <asm/processor.h>
424381Sbinkertn@umich.edu#include <asm/msr.h>
434381Sbinkertn@umich.edu#endif
448334Snate@binkert.org
45955SN/A#define DRIVER_AUTHOR "Ali Saidi"
46955SN/A#define DRIVER_DESC   "Interface to time uncacachable read and writes to device registers"
474202Sbinkertn@umich.edu#define DRIVER_VER    "0.1"
48955SN/A
494382Sbinkertn@umich.edustatic char *dataAddr = NULL;
504382Sbinkertn@umich.edustatic int count = 0;
514382Sbinkertn@umich.edu
526654Snate@binkert.orgstatic inline uint32_t cycleCounter(uint32_t dep);
535517Snate@binkert.org
547674Snate@binkert.orgstatic int __init devtime_start(void)
557674Snate@binkert.org{
566143Snate@binkert.org    uint64_t addr;
576143Snate@binkert.org    uint32_t t1, t2;
586143Snate@binkert.org    uint32_t trash;
598233Snate@binkert.org    int x;
608233Snate@binkert.org    uint32_t *times;
618233Snate@binkert.org    uint32_t num = 0;
628233Snate@binkert.org    struct net_device *dev;
638233Snate@binkert.org
648334Snate@binkert.org    printk("Devtime Driver Version %s Loaded...\n", DRIVER_VER);
658334Snate@binkert.org
668233Snate@binkert.org    if (dataAddr != 0 && count != 0) {
678233Snate@binkert.org        addr = simple_strtoull(dataAddr, NULL, 0);
688233Snate@binkert.org
698233Snate@binkert.org        addr = ioremap(addr, PAGE_SIZE);
708233Snate@binkert.org        /**
718233Snate@binkert.org         * Make sure that the remapping actually worked. On alpha we have
726143Snate@binkert.org         * linear addressing, so its not a problem. But it can fail in x86
738233Snate@binkert.org         * if physical memory is mapped to this address.
748233Snate@binkert.org         */
758233Snate@binkert.org        times = kmalloc(sizeof(uint32_t) * count, GFP_USER);
766143Snate@binkert.org        if (!times) {
776143Snate@binkert.org            printk("Could not allocate memory... Try again later.\n");
786143Snate@binkert.org            return -1;
796143Snate@binkert.org        }
808233Snate@binkert.org
818233Snate@binkert.org        if (addr) {
828233Snate@binkert.org            printk("Preparing to read %#llx %d times.\n", addr, count);
836143Snate@binkert.org
848233Snate@binkert.org            t1 = cycleCounter(trash);
858233Snate@binkert.org            for (x = 0; x < count; x++) {
868233Snate@binkert.org                trash = readl(addr);
878233Snate@binkert.org                t2 = cycleCounter(trash);
886143Snate@binkert.org                times[num++] = t2 - t1;
896143Snate@binkert.org                t1 = t2;
906143Snate@binkert.org            }
914762Snate@binkert.org
926143Snate@binkert.org            /**
938233Snate@binkert.org             * Unmap the address.
948233Snate@binkert.org             */
958233Snate@binkert.org            iounmap(addr);
968233Snate@binkert.org
978233Snate@binkert.org            printk("Measurements:\n");
986143Snate@binkert.org            for (x = 0; x < count; x++) {
998233Snate@binkert.org                printk("%d ", times[x]);
1008233Snate@binkert.org                if (((x + 1) % 10) == 0)
1018233Snate@binkert.org                    printk("\n");
1028233Snate@binkert.org            }
1036143Snate@binkert.org            printk("\nDone.\n");
1046143Snate@binkert.org        } else {
1056143Snate@binkert.org            printk("Unable to remap address. Please try again later.\n");
1066143Snate@binkert.org        }
1076143Snate@binkert.org    } else {
1086143Snate@binkert.org        dev = dev_get_by_name("eth0");
1096143Snate@binkert.org        if (dev) {
1106143Snate@binkert.org            printk("Eth0: MemStart: %#lx MemEnd: %#lx I/O Addr: %#lx\n",
1116143Snate@binkert.org                   dev->mem_start, dev->mem_end, dev->base_addr);
1127065Snate@binkert.org            dev_put(dev);
1136143Snate@binkert.org        }
1148233Snate@binkert.org        dev = 0;
1158233Snate@binkert.org        dev = dev_get_by_name("eth1");
1168233Snate@binkert.org        if (dev) {
1178233Snate@binkert.org            printk("Eth1: MemStart: %#lx MemEnd: %#lx I/O Addr: %#lx\n",
1188233Snate@binkert.org                   dev->mem_start, dev->mem_end, dev->base_addr);
1198233Snate@binkert.org            dev_put(dev);
1208233Snate@binkert.org        }
1218233Snate@binkert.org
1228233Snate@binkert.org        printk("Required information not supplied.\n");
1238233Snate@binkert.org    }
1248233Snate@binkert.org
1258233Snate@binkert.org    return 0;
1268233Snate@binkert.org}
1278233Snate@binkert.org
1288233Snate@binkert.org#ifdef __i386__
1298233Snate@binkert.org
1308233Snate@binkert.orgstatic inline uint32_t cycleCounter(uint32_t dep)
1318233Snate@binkert.org{
1328233Snate@binkert.org    uint32_t time;
1338233Snate@binkert.org    cpuid_eax(0);
1348233Snate@binkert.org    rdtscl(time);
1358233Snate@binkert.org    cpuid_eax(0);
1368233Snate@binkert.org    return time;
1378233Snate@binkert.org}
1388233Snate@binkert.org
1398233Snate@binkert.org#elif __alpha__
1408233Snate@binkert.org
1418233Snate@binkert.orginline uint32_t cycleCounter(uint32_t dep)
1428233Snate@binkert.org{
1438233Snate@binkert.org    uint32_t res;
1448233Snate@binkert.org    asm volatile ("rpcc %0, %1" : "=r"(res) : "r" (dep) : "memory");
1456143Snate@binkert.org    return res;
1466143Snate@binkert.org}
1476143Snate@binkert.org#else
1486143Snate@binkert.org#error Architecture NOT SUPPORTE
1496143Snate@binkert.org#endif
1506143Snate@binkert.org
1516143Snate@binkert.orgstatic void __exit devtime_end(void)
1526143Snate@binkert.org{
1536143Snate@binkert.org    printk("Devtime Driver Version %s Unloaded...\n", DRIVER_VER);
1548233Snate@binkert.org}
1558233Snate@binkert.org
1568233Snate@binkert.org
1576143Snate@binkert.orgmodule_init(devtime_start);
1586143Snate@binkert.orgmodule_exit(devtime_end);
1596143Snate@binkert.org
1606143Snate@binkert.orgMODULE_LICENSE("Dual BSD/GPL");
1616143Snate@binkert.orgMODULE_AUTHOR(DRIVER_AUTHOR);
1626143Snate@binkert.orgMODULE_DESCRIPTION(DRIVER_DESC);
1635522Snate@binkert.orgmodule_param(dataAddr, charp, 0);
1646143Snate@binkert.orgmodule_param(count, int, 0);
1656143Snate@binkert.org