olpc_ofw.c 2.7 KB

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