cpu_x86.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <cpu.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <asm/cpu.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. int cpu_x86_bind(struct udevice *dev)
  13. {
  14. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  15. struct cpuid_result res;
  16. plat->cpu_id = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  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(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(struct udevice *dev, char *buf, int size)
  33. {
  34. if (size < CPU_MAX_NAME_LEN)
  35. return -ENOSPC;
  36. cpu_get_name(buf);
  37. return 0;
  38. }
  39. static int cpu_x86_get_count(struct udevice *dev)
  40. {
  41. int node, cpu;
  42. int num = 0;
  43. node = fdt_path_offset(gd->fdt_blob, "/cpus");
  44. if (node < 0)
  45. return -ENOENT;
  46. for (cpu = fdt_first_subnode(gd->fdt_blob, node);
  47. cpu >= 0;
  48. cpu = fdt_next_subnode(gd->fdt_blob, cpu)) {
  49. const char *device_type;
  50. device_type = fdt_getprop(gd->fdt_blob, cpu,
  51. "device_type", NULL);
  52. if (!device_type)
  53. continue;
  54. if (strcmp(device_type, "cpu") == 0)
  55. num++;
  56. }
  57. return num;
  58. }
  59. static const struct cpu_ops cpu_x86_ops = {
  60. .get_desc = cpu_x86_get_desc,
  61. .get_count = cpu_x86_get_count,
  62. .get_vendor = cpu_x86_get_vendor,
  63. };
  64. static const struct udevice_id cpu_x86_ids[] = {
  65. { .compatible = "cpu-x86" },
  66. { }
  67. };
  68. U_BOOT_DRIVER(cpu_x86_drv) = {
  69. .name = "cpu_x86",
  70. .id = UCLASS_CPU,
  71. .of_match = cpu_x86_ids,
  72. .bind = cpu_x86_bind,
  73. .ops = &cpu_x86_ops,
  74. };