blob: 349bab1583ac69d150807817c6f66c1962f13396 [file] [log] [blame]
Simon Glass4c8243d2019-12-06 21:42:55 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2015 Google, Inc
4 */
5
6#include <common.h>
7#include <cpu.h>
8#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Simon Glass4c8243d2019-12-06 21:42:55 -070010#include <qfw.h>
11#include <dm/lists.h>
12#include <dm/uclass-internal.h>
13#include <dm/root.h>
14
15int qemu_cpu_fixup(void)
16{
17 int ret;
18 int cpu_num;
19 int cpu_online;
20 struct udevice *dev, *pdev;
21 struct cpu_platdata *plat;
22 char *cpu;
23
24 /* first we need to find '/cpus' */
25 for (device_find_first_child(dm_root(), &pdev);
26 pdev;
27 device_find_next_child(&pdev)) {
28 if (!strcmp(pdev->name, "cpus"))
29 break;
30 }
31 if (!pdev) {
32 printf("unable to find cpus device\n");
33 return -ENODEV;
34 }
35
36 /* calculate cpus that are already bound */
37 cpu_num = 0;
38 for (uclass_find_first_device(UCLASS_CPU, &dev);
39 dev;
40 uclass_find_next_device(&dev)) {
41 cpu_num++;
42 }
43
44 /* get actual cpu number */
45 cpu_online = qemu_fwcfg_online_cpus();
46 if (cpu_online < 0) {
47 printf("unable to get online cpu number: %d\n", cpu_online);
48 return cpu_online;
49 }
50
51 /* bind addtional cpus */
52 dev = NULL;
53 for (; cpu_num < cpu_online; cpu_num++) {
54 /*
55 * allocate device name here as device_bind_driver() does
56 * not copy device name, 8 bytes are enough for
57 * sizeof("cpu@") + 3 digits cpu number + '\0'
58 */
59 cpu = malloc(8);
60 if (!cpu) {
61 printf("unable to allocate device name\n");
62 return -ENOMEM;
63 }
64 sprintf(cpu, "cpu@%d", cpu_num);
65 ret = device_bind_driver(pdev, "cpu_qemu", cpu, &dev);
66 if (ret) {
67 printf("binding cpu@%d failed: %d\n", cpu_num, ret);
68 return ret;
69 }
70 plat = dev_get_parent_platdata(dev);
71 plat->cpu_id = cpu_num;
72 }
73 return 0;
74}