olpc_ofw.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. early_iounmap(base, sizeof(olpc_ofw_pgd) * PTRS_PER_PGD);
  31. }
  32. int __olpc_ofw(const char *name, int nr_args, void **args, int nr_res,
  33. void **res)
  34. {
  35. int ofw_args[MAXARGS + 3];
  36. unsigned long flags;
  37. int ret, i, *p;
  38. BUG_ON(nr_args + nr_res > MAXARGS);
  39. if (!olpc_ofw_cif)
  40. return -EIO;
  41. ofw_args[0] = (int)name;
  42. ofw_args[1] = nr_args;
  43. ofw_args[2] = nr_res;
  44. p = &ofw_args[3];
  45. for (i = 0; i < nr_args; i++, p++)
  46. *p = (int)args[i];
  47. /* call into ofw */
  48. spin_lock_irqsave(&ofw_lock, flags);
  49. ret = olpc_ofw_cif(ofw_args);
  50. spin_unlock_irqrestore(&ofw_lock, flags);
  51. if (!ret) {
  52. for (i = 0; i < nr_res; i++, p++)
  53. *((int *)res[i]) = *p;
  54. }
  55. return ret;
  56. }
  57. EXPORT_SYMBOL_GPL(__olpc_ofw);
  58. /* OFW cif _should_ be above this address */
  59. #define OFW_MIN 0xff000000
  60. /* OFW starts on a 1MB boundary */
  61. #define OFW_BOUND (1<<20)
  62. void __init olpc_ofw_detect(void)
  63. {
  64. struct olpc_ofw_header *hdr = &boot_params.olpc_ofw_header;
  65. unsigned long start;
  66. /* ensure OFW booted us by checking for "OFW " string */
  67. if (hdr->ofw_magic != OLPC_OFW_SIG)
  68. return;
  69. olpc_ofw_cif = (int (*)(int *))hdr->cif_handler;
  70. if ((unsigned long)olpc_ofw_cif < OFW_MIN) {
  71. printk(KERN_ERR "OFW detected, but cif has invalid address 0x%lx - disabling.\n",
  72. (unsigned long)olpc_ofw_cif);
  73. olpc_ofw_cif = NULL;
  74. return;
  75. }
  76. /* determine where OFW starts in memory */
  77. start = round_down((unsigned long)olpc_ofw_cif, OFW_BOUND);
  78. printk(KERN_INFO "OFW detected in memory, cif @ 0x%lx (reserving top %ldMB)\n",
  79. (unsigned long)olpc_ofw_cif, (-start) >> 20);
  80. reserve_top_address(-start);
  81. }