qfw_cpu.c 1.6 KB

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