als.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2016
  4. */
  5. #include <linux/kernel.h>
  6. #include <asm/processor.h>
  7. #include <asm/facility.h>
  8. #include <asm/lowcore.h>
  9. #include <asm/sclp.h>
  10. /*
  11. * The code within this file will be called very early. It may _not_
  12. * access anything within the bss section, since that is not cleared
  13. * yet and may contain data (e.g. initrd) that must be saved by other
  14. * code.
  15. * For temporary objects the stack (16k) should be used.
  16. */
  17. static unsigned long als[] = { FACILITIES_ALS };
  18. static void u16_to_hex(char *str, u16 val)
  19. {
  20. int i, num;
  21. for (i = 1; i <= 4; i++) {
  22. num = (val >> (16 - 4 * i)) & 0xf;
  23. if (num >= 10)
  24. num += 7;
  25. *str++ = '0' + num;
  26. }
  27. *str = '\0';
  28. }
  29. static void print_machine_type(void)
  30. {
  31. static char mach_str[80] = "Detected machine-type number: ";
  32. char type_str[5];
  33. struct cpuid id;
  34. get_cpu_id(&id);
  35. u16_to_hex(type_str, id.machine);
  36. strcat(mach_str, type_str);
  37. strcat(mach_str, "\n");
  38. sclp_early_printk(mach_str);
  39. }
  40. static void u16_to_decimal(char *str, u16 val)
  41. {
  42. int div = 1;
  43. while (div * 10 <= val)
  44. div *= 10;
  45. while (div) {
  46. *str++ = '0' + val / div;
  47. val %= div;
  48. div /= 10;
  49. }
  50. *str = '\0';
  51. }
  52. static void print_missing_facilities(void)
  53. {
  54. static char als_str[80] = "Missing facilities: ";
  55. unsigned long val;
  56. char val_str[6];
  57. int i, j, first;
  58. first = 1;
  59. for (i = 0; i < ARRAY_SIZE(als); i++) {
  60. val = ~S390_lowcore.stfle_fac_list[i] & als[i];
  61. for (j = 0; j < BITS_PER_LONG; j++) {
  62. if (!(val & (1UL << (BITS_PER_LONG - 1 - j))))
  63. continue;
  64. if (!first)
  65. strcat(als_str, ",");
  66. /*
  67. * Make sure we stay within one line. Consider that
  68. * each facility bit adds up to five characters and
  69. * z/VM adds a four character prefix.
  70. */
  71. if (strlen(als_str) > 70) {
  72. strcat(als_str, "\n");
  73. sclp_early_printk(als_str);
  74. *als_str = '\0';
  75. }
  76. u16_to_decimal(val_str, i * BITS_PER_LONG + j);
  77. strcat(als_str, val_str);
  78. first = 0;
  79. }
  80. }
  81. strcat(als_str, "\n");
  82. sclp_early_printk(als_str);
  83. sclp_early_printk("See Principles of Operations for facility bits\n");
  84. }
  85. static void facility_mismatch(void)
  86. {
  87. sclp_early_printk("The Linux kernel requires more recent processor hardware\n");
  88. print_machine_type();
  89. print_missing_facilities();
  90. disabled_wait(0x8badcccc);
  91. }
  92. void verify_facilities(void)
  93. {
  94. int i;
  95. for (i = 0; i < ARRAY_SIZE(S390_lowcore.stfle_fac_list); i++)
  96. S390_lowcore.stfle_fac_list[i] = 0;
  97. asm volatile(
  98. " stfl 0(0)\n"
  99. : "=m" (S390_lowcore.stfl_fac_list));
  100. S390_lowcore.stfle_fac_list[0] = (u64)S390_lowcore.stfl_fac_list << 32;
  101. if (S390_lowcore.stfl_fac_list & 0x01000000) {
  102. register unsigned long reg0 asm("0") = ARRAY_SIZE(als) - 1;
  103. asm volatile(".insn s,0xb2b00000,0(%1)" /* stfle */
  104. : "+d" (reg0)
  105. : "a" (&S390_lowcore.stfle_fac_list)
  106. : "memory", "cc");
  107. }
  108. for (i = 0; i < ARRAY_SIZE(als); i++) {
  109. if ((S390_lowcore.stfle_fac_list[i] & als[i]) != als[i])
  110. facility_mismatch();
  111. }
  112. }