dm: core: Create a new header file for 'compat' features
[platform/kernel/u-boot.git] / arch / x86 / cpu / qfw_cpu.c
1 // 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>
9 #include <malloc.h>
10 #include <qfw.h>
11 #include <dm/lists.h>
12 #include <dm/uclass-internal.h>
13 #include <dm/root.h>
14
15 int 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 }