olpc_ofw.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/kernel.h>
  3. #include <linux/export.h>
  4. #include <linux/spinlock_types.h>
  5. #include <linux/init.h>
  6. #include <linux/pgtable.h>
  7. #include <asm/page.h>
  8. #include <asm/setup.h>
  9. #include <asm/io.h>
  10. #include <asm/cpufeature.h>
  11. #include <asm/special_insns.h>
  12. #include <asm/olpc_ofw.h>
  13. /* address of OFW callback interface; will be NULL if OFW isn't found */
  14. static int (*olpc_ofw_cif)(int *);
  15. /* page dir entry containing OFW's pgdir table; filled in by head_32.S */
  16. u32 olpc_ofw_pgd __initdata;
  17. static DEFINE_SPINLOCK(ofw_lock);
  18. #define MAXARGS 10
  19. void __init setup_olpc_ofw_pgd(void)
  20. {
  21. pgd_t *base, *ofw_pde;
  22. if (!olpc_ofw_cif)
  23. return;
  24. /* fetch OFW's PDE */
  25. base = early_ioremap(olpc_ofw_pgd, sizeof(olpc_ofw_pgd) * PTRS_PER_PGD);
  26. if (!base) {
  27. printk(KERN_ERR "failed to remap OFW's pgd - disabling OFW!\n");
  28. olpc_ofw_cif = NULL;
  29. return;
  30. }
  31. ofw_pde = &base[OLPC_OFW_PDE_NR];
  32. /* install OFW's PDE permanently into the kernel's pgtable */
  33. set_pgd(&swapper_pg_dir[OLPC_OFW_PDE_NR], *ofw_pde);
  34. /* implicit optimization barrier here due to uninline function return */
  35. early_iounmap(base, sizeof(olpc_ofw_pgd) * PTRS_PER_PGD);
  36. }
  37. int __olpc_ofw(const char *name, int nr_args, const void **args, int nr_res,
  38. void **res)
  39. {
  40. int ofw_args[MAXARGS + 3];
  41. unsigned long flags;
  42. int ret, i, *p;
  43. BUG_ON(nr_args + nr_res > MAXARGS);
  44. if (!olpc_ofw_cif)
  45. return -EIO;
  46. ofw_args[0] = (int)name;
  47. ofw_args[1] = nr_args;
  48. ofw_args[2] = nr_res;
  49. p = &ofw_args[3];
  50. for (i = 0; i < nr_args; i++, p++)
  51. *p = (int)args[i];
  52. /* call into ofw */
  53. spin_lock_irqsave(&ofw_lock, flags);
  54. ret = olpc_ofw_cif(ofw_args);
  55. spin_unlock_irqrestore(&ofw_lock, flags);
  56. if (!ret) {
  57. for (i = 0; i < nr_res; i++, p++)
  58. *((int *)res[i]) = *p;
  59. }
  60. return ret;
  61. }
  62. EXPORT_SYMBOL_GPL(__olpc_ofw);
  63. bool olpc_ofw_present(void)
  64. {
  65. return olpc_ofw_cif != NULL;
  66. }
  67. EXPORT_SYMBOL_GPL(olpc_ofw_present);
  68. /* OFW cif _should_ be above this address */
  69. #define OFW_MIN 0xff000000
  70. /* OFW starts on a 1MB boundary */
  71. #define OFW_BOUND (1<<20)
  72. void __init olpc_ofw_detect(void)
  73. {
  74. struct olpc_ofw_header *hdr = &boot_params.olpc_ofw_header;
  75. unsigned long start;
  76. /* ensure OFW booted us by checking for "OFW " string */
  77. if (hdr->ofw_magic != OLPC_OFW_SIG)
  78. return;
  79. olpc_ofw_cif = (int (*)(int *))hdr->cif_handler;
  80. if ((unsigned long)olpc_ofw_cif < OFW_MIN) {
  81. printk(KERN_ERR "OFW detected, but cif has invalid address 0x%lx - disabling.\n",
  82. (unsigned long)olpc_ofw_cif);
  83. olpc_ofw_cif = NULL;
  84. return;
  85. }
  86. /* determine where OFW starts in memory */
  87. start = round_down((unsigned long)olpc_ofw_cif, OFW_BOUND);
  88. printk(KERN_INFO "OFW detected in memory, cif @ 0x%lx (reserving top %ldMB)\n",
  89. (unsigned long)olpc_ofw_cif, (-start) >> 20);
  90. reserve_top_address(-start);
  91. }
  92. bool __init olpc_ofw_is_installed(void)
  93. {
  94. return olpc_ofw_cif != NULL;
  95. }