namespace CPUMonitor { partial class frmCPU { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.serSerialPort = new System.IO.Ports.SerialPort(this.components); this.tmrCPUTimer = new System.Windows.Forms.Timer(this.components); this.cmbComPort = new System.Windows.Forms.ComboBox(); this.pcCPUUsage = new System.Diagnostics.PerformanceCounter(); this.lblCPU = new System.Windows.Forms.Label(); this.nicoNotifyIcon = new System.Windows.Forms.NotifyIcon(this.components); this.btnMinimizeToTray = new System.Windows.Forms.Button(); this.btnExit = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pcCPUUsage)).BeginInit(); this.SuspendLayout(); // // tmrCPUTimer // this.tmrCPUTimer.Enabled = true; this.tmrCPUTimer.Interval = 1000; this.tmrCPUTimer.Tick += new System.EventHandler(this.tmrCPUTimer_Tick); // // cmbComPort // this.cmbComPort.FormattingEnabled = true; this.cmbComPort.Location = new System.Drawing.Point(48, 12); this.cmbComPort.Name = "cmbComPort"; this.cmbComPort.Size = new System.Drawing.Size(156, 21); this.cmbComPort.TabIndex = 0; this.cmbComPort.SelectedIndexChanged += new System.EventHandler(this.cbPort_SelectedIndexChanged); // // pcCPUUsage // this.pcCPUUsage.CategoryName = "Processor"; this.pcCPUUsage.CounterName = "% Processor Time"; this.pcCPUUsage.InstanceName = "_Total"; // // lblCPU // this.lblCPU.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lblCPU.Location = new System.Drawing.Point(44, 36); this.lblCPU.Name = "lblCPU"; this.lblCPU.Size = new System.Drawing.Size(160, 28); this.lblCPU.TabIndex = 1; this.lblCPU.Text = "0%"; this.lblCPU.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // nicoNotifyIcon // this.nicoNotifyIcon.Text = "CPU Usage Monitor"; this.nicoNotifyIcon.Visible = true; // // btnMinimizeToTray // this.btnMinimizeToTray.Location = new System.Drawing.Point(12, 67); this.btnMinimizeToTray.Name = "btnMinimizeToTray"; this.btnMinimizeToTray.Size = new System.Drawing.Size(111, 28); this.btnMinimizeToTray.TabIndex = 2; this.btnMinimizeToTray.Text = "Minimize to Tray"; this.btnMinimizeToTray.UseVisualStyleBackColor = true; this.btnMinimizeToTray.Click += new System.EventHandler(this.btnMinimizeToTray_Click); // // btnExit // this.btnExit.Location = new System.Drawing.Point(126, 67); this.btnExit.Name = "btnExit"; this.btnExit.Size = new System.Drawing.Size(111, 28); this.btnExit.TabIndex = 3; this.btnExit.Text = "Exit"; this.btnExit.UseVisualStyleBackColor = true; this.btnExit.Click += new System.EventHandler(this.btnExit_Click); // // frmCPU // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(249, 106); this.Controls.Add(this.btnExit); this.Controls.Add(this.btnMinimizeToTray); this.Controls.Add(this.lblCPU); this.Controls.Add(this.cmbComPort); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "frmCPU"; this.Text = "CPU Usage Monitor"; this.WindowState = System.Windows.Forms.FormWindowState.Minimized; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.pcCPUUsage)).EndInit(); this.ResumeLayout(false); } #endregion private System.IO.Ports.SerialPort serSerialPort; private System.Windows.Forms.Timer tmrCPUTimer; private System.Windows.Forms.ComboBox cmbComPort; private System.Diagnostics.PerformanceCounter pcCPUUsage; private System.Windows.Forms.Label lblCPU; private System.Windows.Forms.NotifyIcon nicoNotifyIcon; private System.Windows.Forms.Button btnMinimizeToTray; private System.Windows.Forms.Button btnExit; } } id='n73' href='#n73'>73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Copyright (C) IBM Corp. 2005
 *
 * Authors: Jimi Xenidis <jimix@watson.ibm.com>
 */

#include <xen/config.h>
#include <xen/types.h>
#include <xen/mm.h>
#include <asm/cache.h>
#include <xen/init.h>
#include "tce.h"
#include "iommu.h"
#include "dart.h"
#include "oftree.h"
#include "of-devtree.h"

#undef DEBUG
#ifdef DEBUG
#define DBG(fmt...) printk(fmt)
static int dbg_after;
#define DBG_SET_AFTER dbg_after = 1;
#define DBG_AFTER(fmt...) if (dbg_after) DBG(fmt)
#else
#define DBG(fmt...)
#define DBG_SET_AFTER
#define DBG_AFTER(fmt...)
#endif

/* Max size of 512 pages */
#define U3_LOG_MAX_PAGES 9

#define DART_DEF_BASE   0xf8033000UL
#define DART_NONE 0
#define DART_U3 3
#define DART_U4 4
#define DART_WRITE 0x1
#define DART_READ 0x2

static ulong dummy_page;
static ulong dart_entries;
static struct dart_ops *dops;
static u32 *dart_table;

union dart_entry {
    u32 de_word;
    struct {
        u32 de_v:1;             /* valid */
        u32 de_rp:1;             /* read protected */
        u32 de_wp:1;             /* write protected */
        u32 _de_res:5;
        u32 de_ppn:24;         /* 24 bit Physical Page Number
                                 * representing address [28:51] */
    } de_bits;
};

struct dma_window {
    u32 dw_liobn;
    u32 dw_base_hi;
    u64 dw_base;
    u64 dw_size;
};

struct dart_info {
    struct dma_window di_window;
    ulong di_base;
    int di_model;
};

static u32 dart_encode(int perm, ulong rpn)
{
    union dart_entry e;

    e.de_word = 0;
    e.de_bits.de_v = 1;
    e.de_bits.de_ppn = rpn;

    /* protect the page */
    e.de_bits.de_rp = 1;
    e.de_bits.de_wp = 1;
    if (perm & DART_READ) {
        e.de_bits.de_rp = 0;
    }
    if (perm & DART_WRITE) {
        e.de_bits.de_wp = 0;
    }
    return e.de_word;
}

static void dart_fill(ulong index, int perm, ulong rpg, ulong num_pg)
{
    u32 volatile *entry = dart_table + index;
    ulong i = 0;
    ulong last_flush = 0;

    while (1) {
        entry[i] = dart_encode(perm, rpg);
        ++i;
        ++rpg;
        if (i == num_pg) break;

        if ((((ulong)&entry[i]) % cpu_caches.dline_size) == 0) {
            last_flush = (ulong)&entry[i - 1];
            dcbst(last_flush);
        }
    }
    dcbst((ulong) &entry[i - 1]);
}

static void dart_clear(ulong index, ulong num_pg)
{
    u32 *entry = dart_table + index;
    ulong i = 0;
    ulong rpg = dummy_page;
    ulong last_flush = 0;

    while (1) {
        entry[i] = dart_encode(DART_READ | DART_WRITE, rpg);
        ++i;
        if (i == num_pg) break;

        if ((((ulong)&entry[i]) % cpu_caches.dline_size) == 0) {
            last_flush = (ulong)&entry[i - 1];
            dcbst(last_flush);
        }
    }
    dcbst((ulong)&entry[i - 1]);
}

static int dart_put(ulong ioba, union tce tce)
{
    ulong index = ioba >> PAGE_SHIFT;

    if (index > dart_entries) {
        return -1;
    }

    if (tce.tce_bits.tce_vlps  != 0 || tce.tce_bits.tce_lpx != 0) {
        panic("no support for large TCEs\n");
    }

    if (tce.tce_bits.tce_read == 0 &&
        tce.tce_bits.tce_write == 0) {
        /* the TCE table is inited by the domain by a bunch of 0
         * perminssion puts.  We are only interesting in debugging the
         * ones after the first put */
        DBG_AFTER(">DART[0x%lx] clear\n", index);
        dart_clear(index, 1);
    } else {
        unsigned perm = 0;

        if (tce.tce_bits.tce_read)
            perm |= DART_READ;
        if (tce.tce_bits.tce_write)
            perm |= DART_WRITE;

        DBG("<DART[0x%lx]: ioba: 0x%lx perm:%x[%c%c] rpn:0x%lx\n",
            index, ioba, perm,
            (perm & DART_READ) ? 'R' : '-',
            (perm & DART_WRITE) ? 'W' : '-',
            (ulong)tce.tce_bits.tce_rpn);
        DBG_SET_AFTER;

        dart_fill(index, perm, tce.tce_bits.tce_rpn, 1);
    }
    dops->do_inv_entry(tce.tce_bits.tce_rpn);
    
    return 0;
}

static int find_dart(struct dart_info *di)
{
    int rc;
    void *ofd_p;
    ofdn_t n;
    char compat[128];

    if (on_systemsim()) {
        DBG("%s: systemsim does not support a dart\n", __func__);
        return -1;
    }

    ofd_p = (void *)oftree;
    n = ofd_node_find(ofd_p, "/ht");
    if (n <= 0)
        return -1;

    /* get the defaults from the HT node model */
    rc = ofd_getprop(ofd_p, n, "compatible", compat, sizeof (compat));
    if (rc <= 0)
        return -1;

    if (ofd_strstr(compat, rc, "u4"))
        di->di_model = DART_U4;
    else if (ofd_strstr(compat, rc, "u3"))
        di->di_model = DART_U3;
    else {
        DBG("%s: not a U3 or U4\n", __func__);
        return -1;
    }
        
    di->di_base = DART_DEF_BASE;

    /* FIXME: this should actually be the HT reg value */
    di->di_window.dw_liobn = 0;
    di->di_window.dw_base_hi = 0;
    di->di_window.dw_base = 0;

    /* lets see if the devtree has more info */
    n = ofd_node_find(ofd_p, "/dart");
    if (n > 0) {
        ulong base;

        rc = ofd_getprop(ofd_p, n, "compatible", compat, sizeof (compat));
        if (rc > 0) {
            if (strstr(compat, "u4")) {
                di->di_model = DART_U4;
            }
        }

        rc = ofd_getprop(ofd_p, n, "reg", &base, sizeof (base));
        if (rc > 0) {
            di->di_base = base;
        }
    }
    return 0;
}

static int init_dart(void)
{
    ulong log_pgs;
    void *ofd_p;
    ofdn_t n;
    struct dart_info di;

    if (find_dart(&di))
        return 0;

    /* Max size of 512 pages == 2MB == 1<<21. That siz is good enough for U4 */
    log_pgs = U3_LOG_MAX_PAGES;
    dart_table = alloc_xenheap_pages(log_pgs);
    BUG_ON(dart_table == NULL);

    dart_entries = (1UL << (log_pgs + PAGE_SHIFT)) / sizeof (union dart_entry);
    di.di_window.dw_size = dart_entries << PAGE_SHIFT;

    /* Linux uses a dummy page, filling "empty" DART entries with a
       reference to this page to capture stray DMA's */
    dummy_page = (ulong)alloc_xenheap_pages(0);
    clear_page((void *)dummy_page);
    dummy_page >>= PAGE_SHIFT;

    printk("Initializing DART 0x%lx: tbl: %p[0x%lx] entries: 0x%lx\n",
           di.di_base, dart_table, 1UL << log_pgs, dart_entries);
           
    /* register this iommu */
    iommu_register(di.di_window.dw_liobn, dart_put);

    switch (di.di_model) {
    case DART_U3:
        dops = u3_init(di.di_base, (ulong)dart_table, 1UL << log_pgs);
        break;
    case DART_U4:
        dops = u4_init(di.di_base, (ulong)dart_table, 1UL << log_pgs);
        break;
    }

    dart_clear(0, dart_entries);
    dops->do_inv_all();

    /* fix up the devtree */
    ofd_p = (void *)oftree;
    n = ofd_node_find(ofd_p, "/ht");
    if (n > 0) {
        di.di_window.dw_size = dart_entries << PAGE_SHIFT;
        ofd_prop_add(ofd_p, n, "ibm,dma-window", &di.di_window,
                     sizeof (di.di_window));
    } else {
        panic("%s: no /ht node\n", __func__);
    }
    return 0;
}
__initcall(init_dart);