cpu_x86.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <cpu.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <asm/cpu.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. int cpu_x86_bind(struct udevice *dev)
  12. {
  13. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  14. struct cpuid_result res;
  15. plat->cpu_id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  16. "intel,apic-id", -1);
  17. plat->family = gd->arch.x86;
  18. res = cpuid(1);
  19. plat->id[0] = res.eax;
  20. plat->id[1] = res.edx;
  21. return 0;
  22. }
  23. int cpu_x86_get_vendor(const struct udevice *dev, char *buf, int size)
  24. {
  25. const char *vendor = cpu_vendor_name(gd->arch.x86_vendor);
  26. if (size < (strlen(vendor) + 1))
  27. return -ENOSPC;
  28. strcpy(buf, vendor);
  29. return 0;
  30. }
  31. int cpu_x86_get_desc(const struct udevice *dev, char *buf, int size)
  32. {
  33. char *ptr;
  34. if (size < CPU_MAX_NAME_LEN)
  35. return -ENOSPC;
  36. ptr = cpu_get_name(buf);
  37. if (ptr != buf)
  38. strcpy(buf, ptr);
  39. return 0;
  40. }
  41. int cpu_x86_get_count(const struct udevice *dev)
  42. {
  43. int node, cpu;
  44. int num = 0;
  45. node = fdt_path_offset(gd->fdt_blob, "/cpus");
  46. if (node < 0)
  47. return -ENOENT;
  48. for (cpu = fdt_first_subnode(gd->fdt_blob, node);
  49. cpu >= 0;
  50. cpu = fdt_next_subnode(gd->fdt_blob, cpu)) {
  51. const char *device_type;
  52. device_type = fdt_getprop(gd->fdt_blob, cpu,
  53. "device_type", NULL);
  54. if (!device_type)
  55. continue;
  56. if (strcmp(device_type, "cpu") == 0)
  57. num++;
  58. }
  59. return num;
  60. }
  61. static const struct cpu_ops cpu_x86_ops = {
  62. .get_desc = cpu_x86_get_desc,
  63. .get_count = cpu_x86_get_count,
  64. .get_vendor = cpu_x86_get_vendor,
  65. };
  66. static const struct udevice_id cpu_x86_ids[] = {
  67. { .compatible = "cpu-x86" },
  68. { }
  69. };
  70. U_BOOT_DRIVER(cpu_x86_drv) = {
  71. .name = "cpu_x86",
  72. .id = UCLASS_CPU,
  73. .of_match = cpu_x86_ids,
  74. .bind = cpu_x86_bind,
  75. .ops = &cpu_x86_ops,
  76. .flags = DM_FLAG_PRE_RELOC,
  77. };