olpc_ofw.c 2.8 KB

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