cpu_x86.c 1.9 KB

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