cpu_powerpc.c 13.9 KB
Newer Older
P
Prerna Saxena 已提交
1
/*
L
Li Zhang 已提交
2
 * cpu_powerpc.c: CPU driver for PowerPC CPUs
P
Prerna Saxena 已提交
3
 *
L
Li Zhang 已提交
4
 * Copyright (C) IBM Corporation, 2010
P
Prerna Saxena 已提交
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
P
Prerna Saxena 已提交
19 20 21 22
 *
 * Authors:
 *      Anton Blanchard <anton@au.ibm.com>
 *      Prerna Saxena <prerna@linux.vnet.ibm.com>
L
Li Zhang 已提交
23
 *      Li Zhang <zhlcindy@linux.vnet.ibm.com>
P
Prerna Saxena 已提交
24 25 26
 */

#include <config.h>
L
Li Zhang 已提交
27
#include <stdint.h>
P
Prerna Saxena 已提交
28

29
#include "virlog.h"
30
#include "viralloc.h"
31
#include "virutil.h"
P
Prerna Saxena 已提交
32 33
#include "cpu.h"

L
Li Zhang 已提交
34
#include "cpu_map.h"
35
#include "virbuffer.h"
P
Prerna Saxena 已提交
36 37 38

#define VIR_FROM_THIS VIR_FROM_CPU

39
static const virArch archs[] = { VIR_ARCH_PPC64 };
P
Prerna Saxena 已提交
40

L
Li Zhang 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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
struct cpuPowerPC {
    const char *name;
    const char *vendor;
    uint32_t pvr;
};

static const struct cpuPowerPC cpu_defs[] = {
    {"POWER7", "IBM", 0x003f0200},
    {"POWER7_v2.1", "IBM", 0x003f0201},
    {"POWER7_v2.3", "IBM", 0x003f0203},
    {NULL, NULL, 0xffffffff}
};


struct ppc_vendor {
    char *name;
    struct ppc_vendor *next;
};

struct ppc_model {
    char *name;
    const struct ppc_vendor *vendor;
    union cpuData *data;
    struct ppc_model *next;
};

struct ppc_map {
    struct ppc_vendor *vendors;
    struct ppc_model *models;
};

static int
ConvertModelVendorFromPVR(char ***model,
                          char ***vendor,
                          uint32_t pvr)
{
    int i;

    for (i = 0; cpu_defs[i].name; i++) {
        if (cpu_defs[i].pvr == pvr) {
            **model = strdup(cpu_defs[i].name);
            **vendor = strdup(cpu_defs[i].vendor);
            return 0;
        }
    }

    virReportError(VIR_ERR_INTERNAL_ERROR,
                   "%s", _("Missing the definition of this model"));
    return -1;
}

static int
ConvertPVRFromModel(const char *model,
                    uint32_t *pvr)
{
    int i;

    for (i = 0; cpu_defs[i].name; i++) {
        if (STREQ(cpu_defs[i].name, model)) {
            *pvr = cpu_defs[i].pvr;
            return 0;
        }
    }

    virReportError(VIR_ERR_INTERNAL_ERROR,
                   "%s", _("Missing the definition of this model"));
    return -1;
}

static int
cpuMatch(const union cpuData *data,
         char **cpu_model,
         char **cpu_vendor,
         const struct ppc_model *model)
{
    int ret = 0;

    ret = ConvertModelVendorFromPVR(&cpu_model, &cpu_vendor, data->ppc.pvr);

    if (STREQ(model->name, *cpu_model) &&
        STREQ(model->vendor->name, *cpu_vendor))
        ret = 1;

    return ret;
}


static struct ppc_model *
ppcModelNew(void)
{
    struct ppc_model *model;

    if (VIR_ALLOC(model) < 0)
        return NULL;

    if (VIR_ALLOC(model->data) < 0) {
        VIR_FREE(model);
        return NULL;
    }

    return model;
}

static void
ppcModelFree(struct ppc_model *model)
{
    if (model == NULL)
        return;

    VIR_FREE(model->name);

    VIR_FREE(model->data);

    VIR_FREE(model);
}

static struct ppc_model *
ppcModelFind(const struct ppc_map *map,
             const char *name)
{
    struct ppc_model *model;

    model = map->models;
    while (model != NULL) {
        if (STREQ(model->name, name))
            return model;

        model = model->next;
    }

    return NULL;
}

static struct ppc_vendor *
ppcVendorFind(const struct ppc_map *map,
              const char *name)
{
    struct ppc_vendor *vendor;

    vendor = map->vendors;
    while (vendor) {
        if (STREQ(vendor->name, name))
            return vendor;

        vendor = vendor->next;
    }

    return NULL;
}

static void
ppcVendorFree(struct ppc_vendor *vendor)
{
    if (!vendor)
        return;

    VIR_FREE(vendor->name);
    VIR_FREE(vendor);
}

static int
ppcVendorLoad(xmlXPathContextPtr ctxt,
              struct ppc_map *map)
{
    struct ppc_vendor *vendor = NULL;

207 208 209 210
    if (VIR_ALLOC(vendor) < 0) {
        virReportOOMError();
        return -1;
    }
L
Li Zhang 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224

    vendor->name = virXPathString("string(@name)", ctxt);
    if (!vendor->name) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU vendor name"));
        goto ignore;
    }

    if (ppcVendorFind(map, vendor->name)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("CPU vendor %s already defined"), vendor->name);
        goto ignore;
    }

225
    if (!map->vendors) {
L
Li Zhang 已提交
226
        map->vendors = vendor;
227
    } else {
L
Li Zhang 已提交
228 229 230 231
        vendor->next = map->vendors;
        map->vendors = vendor;
    }

232 233
cleanup:
    return 0;
L
Li Zhang 已提交
234 235 236

ignore:
    ppcVendorFree(vendor);
237
    goto cleanup;
L
Li Zhang 已提交
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
}

static int
ppcModelLoad(xmlXPathContextPtr ctxt,
             struct ppc_map *map)
{
    xmlNodePtr *nodes = NULL;
    struct ppc_model *model;
    char *vendor = NULL;
    int ret = -1;

    if (!(model = ppcModelNew()))
        goto no_memory;

    model->name = virXPathString("string(@name)", ctxt);
    if (model->name == NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Missing CPU model name"));
        goto ignore;
    }

    ret = ConvertPVRFromModel(model->name, &model->data->ppc.pvr);
    if (ret < 0)
       goto ignore;


    if (virXPathBoolean("boolean(./vendor)", ctxt)) {
        vendor = virXPathString("string(./vendor/@name)", ctxt);
        if (!vendor) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid vendor element in CPU model %s"),
                           model->name);
            goto ignore;
        }

        if (!(model->vendor = ppcVendorFind(map, vendor))) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unknown vendor %s referenced by CPU model %s"),
                           vendor, model->name);
            goto ignore;
        }
    }

    if (map->models == NULL)
        map->models = model;
    else {
        model->next = map->models;
        map->models = model;
    }

    ret = 0;

out:
    VIR_FREE(vendor);
    VIR_FREE(nodes);
    return ret;

no_memory:
    virReportOOMError();

ignore:
    ppcModelFree(model);
    goto out;
}

static int
ppcMapLoadCallback(enum cpuMapElement element,
                   xmlXPathContextPtr ctxt,
                   void *data)
{
    struct ppc_map *map = data;

    switch (element) {
    case CPU_MAP_ELEMENT_VENDOR:
        return ppcVendorLoad(ctxt, map);
    case CPU_MAP_ELEMENT_MODEL:
        return ppcModelLoad(ctxt, map);
    default:
        break;
    }

    return 0;
}

static void
ppcMapFree(struct ppc_map *map)
{
    if (map == NULL)
        return;

    while (map->models != NULL) {
        struct ppc_model *model = map->models;
        map->models = model->next;
        ppcModelFree(model);
    }

    while (map->vendors != NULL) {
        struct ppc_vendor *vendor = map->vendors;
        map->vendors = vendor->next;
        ppcVendorFree(vendor);
    }

    VIR_FREE(map);
}

static struct ppc_map *
ppcLoadMap(void)
{
    struct ppc_map *map;

    if (VIR_ALLOC(map) < 0) {
        virReportOOMError();
        return NULL;
    }

    if (cpuMapLoad("ppc64", ppcMapLoadCallback, map) < 0)
        goto error;

    return map;

error:
    ppcMapFree(map);
    return NULL;
}

static virCPUCompareResult
364
ppcCompare(virCPUDefPtr host,
L
Li Zhang 已提交
365 366
           virCPUDefPtr cpu)
{
367 368 369
    if ((cpu->arch == VIR_ARCH_NONE || host->arch == cpu->arch) &&
        STREQ(host->model, cpu->model))
        return VIR_CPU_COMPARE_IDENTICAL;
L
Li Zhang 已提交
370

371
    return VIR_CPU_COMPARE_INCOMPATIBLE;
L
Li Zhang 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
}

static int
PowerPCDecode(virCPUDefPtr cpu,
          const union cpuData *data,
          const char **models,
          unsigned int nmodels,
          const char *preferred)
{
    int ret = -1;
    struct ppc_map *map;
    const struct ppc_model *candidate;
    virCPUDefPtr cpuCandidate;
    virCPUDefPtr cpuModel = NULL;
    char *cpu_vendor = NULL;
    char *cpu_model = NULL;
    unsigned int i;

    if (data == NULL || (map = ppcLoadMap()) == NULL)
        return -1;

    candidate = map->models;

    while (candidate != NULL) {
        bool allowed = (models == NULL);

        for (i = 0; i < nmodels; i++) {
            if (models && models[i] && STREQ(models[i], candidate->name)) {
                allowed = true;
                break;
            }
        }

        if (!allowed) {
            if (preferred && STREQ(candidate->name, preferred)) {
                if (cpu->fallback != VIR_CPU_FALLBACK_ALLOW) {
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                   _("CPU model %s is not supported by hypervisor"),
                                   preferred);
                    goto out;
                } else {
                    VIR_WARN("Preferred CPU model %s not allowed by"
                             " hypervisor; closest supported model will be"
                             " used", preferred);
                }
            } else {
                VIR_DEBUG("CPU model %s not allowed by hypervisor; ignoring",
                          candidate->name);
            }
            goto next;
        }

        if (VIR_ALLOC(cpuCandidate) < 0) {
            virReportOOMError();
            goto out;
        }

        cpuCandidate->model = strdup(candidate->name);
        cpuCandidate->vendor = strdup(candidate->vendor->name);

        if (preferred && STREQ(cpuCandidate->model, preferred)) {
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
            break;
        }

        ret = cpuMatch(data, &cpu_model, &cpu_vendor, candidate);
        if (ret < 0) {
            VIR_FREE(cpuCandidate);
            goto out;
442
        } else if (ret == 1) {
L
Li Zhang 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
            cpuCandidate->model = cpu_model;
            cpuCandidate->vendor = cpu_vendor;
            virCPUDefFree(cpuModel);
            cpuModel = cpuCandidate;
            break;
        }

        virCPUDefFree(cpuCandidate);

    next:
        candidate = candidate->next;
    }

    if (cpuModel == NULL) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Cannot find suitable CPU model for given data"));
        goto out;
    }

    cpu->model = cpuModel->model;
    cpu->vendor = cpuModel->vendor;
    VIR_FREE(cpuModel);

    ret = 0;

out:
    ppcMapFree(map);
    virCPUDefFree(cpuModel);

    return ret;
}


static void
PowerPCDataFree(union cpuData *data)
{
    if (data == NULL)
        return;

    VIR_FREE(data);
}

J
Jiri Denemark 已提交
485
#if defined(__powerpc__) || defined(__powerpc64__)
P
Prerna Saxena 已提交
486
static union cpuData *
J
Jiri Denemark 已提交
487
ppcNodeData(void)
P
Prerna Saxena 已提交
488 489 490 491 492 493 494 495
{
    union cpuData *data;

    if (VIR_ALLOC(data) < 0) {
        virReportOOMError();
        return NULL;
    }

J
Jiri Denemark 已提交
496 497
    asm("mfpvr %0"
        : "=r" (data->ppc.pvr));
L
Li Zhang 已提交
498

P
Prerna Saxena 已提交
499 500
    return data;
}
J
Jiri Denemark 已提交
501
#endif
P
Prerna Saxena 已提交
502 503

static int
L
Li Zhang 已提交
504 505
PowerPCUpdate(virCPUDefPtr guest ATTRIBUTE_UNUSED,
          const virCPUDefPtr host ATTRIBUTE_UNUSED)
P
Prerna Saxena 已提交
506
{
L
Li Zhang 已提交
507
   return 0;
P
Prerna Saxena 已提交
508
}
L
Li Zhang 已提交
509
static virCPUDefPtr
J
Jiri Denemark 已提交
510 511 512 513
ppcBaseline(virCPUDefPtr *cpus,
            unsigned int ncpus,
            const char **models,
            unsigned int nmodels)
P
Prerna Saxena 已提交
514
{
L
Li Zhang 已提交
515
    struct ppc_map *map = NULL;
J
Jiri Denemark 已提交
516 517
    const struct ppc_model *model;
    const struct ppc_vendor *vendor = NULL;
L
Li Zhang 已提交
518
    virCPUDefPtr cpu = NULL;
J
Jiri Denemark 已提交
519 520 521 522
    unsigned int i;

    if (!(map = ppcLoadMap()))
        goto error;
L
Li Zhang 已提交
523

J
Jiri Denemark 已提交
524 525 526
    if (!(model = ppcModelFind(map, cpus[0]->model))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unknown CPU model %s"), cpus[0]->model);
L
Li Zhang 已提交
527 528 529
        goto error;
    }

J
Jiri Denemark 已提交
530 531 532 533
    if (!cpuModelIsAllowed(model->name, models, nmodels)) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("CPU model %s is not supported by hypervisor"),
                        model->name);
L
Li Zhang 已提交
534 535 536
        goto error;
    }

J
Jiri Denemark 已提交
537 538
    for (i = 0; i < ncpus; i++) {
        const struct ppc_vendor *vnd;
539

J
Jiri Denemark 已提交
540 541 542 543 544
        if (STRNEQ(cpus[i]->model, model->name)) {
            virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                           _("CPUs are incompatible"));
            goto error;
        }
L
Li Zhang 已提交
545

J
Jiri Denemark 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
        if (!cpus[i]->vendor)
            continue;

        if (!(vnd = ppcVendorFind(map, cpus[i]->vendor))) {
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("Unknown CPU vendor %s"), cpus[i]->vendor);
            goto error;
        }

        if (model->vendor) {
            if (model->vendor != vnd) {
                virReportError(VIR_ERR_OPERATION_FAILED,
                               _("CPU vendor %s of model %s differs from "
                                 "vendor %s"),
                               model->vendor->name, model->name,
                               vnd->name);
                goto error;
            }
        } else if (vendor) {
            if (vendor != vnd) {
                virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                               _("CPU vendors do not match"));
                goto error;
            }
        } else {
            vendor = vnd;
        }
L
Li Zhang 已提交
573 574
    }

J
Jiri Denemark 已提交
575 576 577
    if (VIR_ALLOC(cpu) < 0 ||
        !(cpu->model = strdup(model->name)))
        goto no_memory;
L
Li Zhang 已提交
578

J
Jiri Denemark 已提交
579 580 581 582 583
    if (vendor && !(cpu->vendor = strdup(vendor->name)))
        goto no_memory;

    cpu->type = VIR_CPU_TYPE_GUEST;
    cpu->match = VIR_CPU_MATCH_EXACT;
L
Li Zhang 已提交
584 585 586

cleanup:
    ppcMapFree(map);
P
Prerna Saxena 已提交
587

L
Li Zhang 已提交
588
    return cpu;
J
Jiri Denemark 已提交
589

L
Li Zhang 已提交
590 591 592 593 594 595
no_memory:
    virReportOOMError();
error:
    virCPUDefFree(cpu);
    cpu = NULL;
    goto cleanup;
P
Prerna Saxena 已提交
596 597 598 599 600 601
}

struct cpuArchDriver cpuDriverPowerPC = {
    .name = "ppc64",
    .arch = archs,
    .narch = ARRAY_CARDINALITY(archs),
602
    .compare    = ppcCompare,
P
Prerna Saxena 已提交
603 604 605
    .decode     = PowerPCDecode,
    .encode     = NULL,
    .free       = PowerPCDataFree,
J
Jiri Denemark 已提交
606 607 608 609 610
#if defined(__powerpc__) || defined(__powerpc64__)
    .nodeData   = ppcNodeData,
#else
    .nodeData   = NULL,
#endif
P
Prerna Saxena 已提交
611
    .guestData  = NULL,
J
Jiri Denemark 已提交
612
    .baseline   = ppcBaseline,
L
Li Zhang 已提交
613
    .update     = PowerPCUpdate,
P
Prerna Saxena 已提交
614 615
    .hasFeature = NULL,
};