devices.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* devices.c: Initial scan of the prom device tree for important
  2. * Sparc device nodes which we need to find.
  3. *
  4. * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
  5. */
  6. #include <linux/config.h>
  7. #include <linux/kernel.h>
  8. #include <linux/threads.h>
  9. #include <linux/init.h>
  10. #include <linux/ioport.h>
  11. #include <linux/string.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/errno.h>
  14. #include <linux/bootmem.h>
  15. #include <asm/page.h>
  16. #include <asm/oplib.h>
  17. #include <asm/system.h>
  18. #include <asm/smp.h>
  19. #include <asm/spitfire.h>
  20. #include <asm/timer.h>
  21. #include <asm/cpudata.h>
  22. #include <asm/vdev.h>
  23. #include <asm/irq.h>
  24. /* Used to synchronize acceses to NatSemi SUPER I/O chip configure
  25. * operations in asm/ns87303.h
  26. */
  27. DEFINE_SPINLOCK(ns87303_lock);
  28. extern void cpu_probe(void);
  29. extern void central_probe(void);
  30. u32 sun4v_vdev_devhandle;
  31. int sun4v_vdev_root;
  32. struct vdev_intmap {
  33. unsigned int phys;
  34. unsigned int irq;
  35. unsigned int cnode;
  36. unsigned int cinterrupt;
  37. };
  38. struct vdev_intmask {
  39. unsigned int phys;
  40. unsigned int interrupt;
  41. unsigned int __unused;
  42. };
  43. static struct vdev_intmap *vdev_intmap;
  44. static int vdev_num_intmap;
  45. static struct vdev_intmask vdev_intmask;
  46. static void __init sun4v_virtual_device_probe(void)
  47. {
  48. struct linux_prom64_registers regs;
  49. struct vdev_intmap *ip;
  50. int node, sz, err;
  51. if (tlb_type != hypervisor)
  52. return;
  53. node = prom_getchild(prom_root_node);
  54. node = prom_searchsiblings(node, "virtual-devices");
  55. if (!node) {
  56. prom_printf("SUN4V: Fatal error, no virtual-devices node.\n");
  57. prom_halt();
  58. }
  59. sun4v_vdev_root = node;
  60. prom_getproperty(node, "reg", (char *)&regs, sizeof(regs));
  61. sun4v_vdev_devhandle = (regs.phys_addr >> 32UL) & 0x0fffffff;
  62. sz = prom_getproplen(node, "interrupt-map");
  63. if (sz <= 0) {
  64. prom_printf("SUN4V: Error, no vdev interrupt-map.\n");
  65. prom_halt();
  66. }
  67. if ((sz % sizeof(*ip)) != 0) {
  68. prom_printf("SUN4V: Bogus interrupt-map property size %d\n",
  69. sz);
  70. prom_halt();
  71. }
  72. vdev_intmap = ip = alloc_bootmem_low_pages(sz);
  73. if (!vdev_intmap) {
  74. prom_printf("SUN4V: Error, cannot allocate vdev_intmap.\n");
  75. prom_halt();
  76. }
  77. err = prom_getproperty(node, "interrupt-map", (char *) ip, sz);
  78. if (err == -1) {
  79. prom_printf("SUN4V: Fatal error, no vdev interrupt-map.\n");
  80. prom_halt();
  81. }
  82. if (err != sz) {
  83. prom_printf("SUN4V: Inconsistent interrupt-map size, "
  84. "proplen(%d) vs getprop(%d).\n", sz,err);
  85. prom_halt();
  86. }
  87. vdev_num_intmap = err / sizeof(*ip);
  88. err = prom_getproperty(node, "interrupt-map-mask",
  89. (char *) &vdev_intmask,
  90. sizeof(vdev_intmask));
  91. if (err <= 0) {
  92. prom_printf("SUN4V: Fatal error, no vdev "
  93. "interrupt-map-mask.\n");
  94. prom_halt();
  95. }
  96. if (err % sizeof(vdev_intmask)) {
  97. prom_printf("SUN4V: Bogus interrupt-map-mask "
  98. "property size %d\n", err);
  99. prom_halt();
  100. }
  101. printk("SUN4V: virtual-devices devhandle[%x]\n",
  102. sun4v_vdev_devhandle);
  103. }
  104. unsigned int sun4v_vdev_device_interrupt(unsigned int dev_node)
  105. {
  106. unsigned int irq, reg;
  107. int err, i;
  108. err = prom_getproperty(dev_node, "interrupts",
  109. (char *) &irq, sizeof(irq));
  110. if (err <= 0) {
  111. printk("VDEV: Cannot get \"interrupts\" "
  112. "property for OBP node %x\n", dev_node);
  113. return 0;
  114. }
  115. err = prom_getproperty(dev_node, "reg",
  116. (char *) &reg, sizeof(reg));
  117. if (err <= 0) {
  118. printk("VDEV: Cannot get \"reg\" "
  119. "property for OBP node %x\n", dev_node);
  120. return 0;
  121. }
  122. for (i = 0; i < vdev_num_intmap; i++) {
  123. if (vdev_intmap[i].phys == (reg & vdev_intmask.phys) &&
  124. vdev_intmap[i].irq == (irq & vdev_intmask.interrupt)) {
  125. irq = vdev_intmap[i].cinterrupt;
  126. break;
  127. }
  128. }
  129. if (i == vdev_num_intmap) {
  130. printk("VDEV: No matching interrupt map entry "
  131. "for OBP node %x\n", dev_node);
  132. return 0;
  133. }
  134. return sun4v_build_irq(sun4v_vdev_devhandle, irq, 5, 0);
  135. }
  136. static const char *cpu_mid_prop(void)
  137. {
  138. if (tlb_type == spitfire)
  139. return "upa-portid";
  140. return "portid";
  141. }
  142. static int get_cpu_mid(int prom_node)
  143. {
  144. if (tlb_type == hypervisor) {
  145. struct linux_prom64_registers reg;
  146. if (prom_getproplen(prom_node, "cpuid") == 4)
  147. return prom_getintdefault(prom_node, "cpuid", 0);
  148. prom_getproperty(prom_node, "reg", (char *) &reg, sizeof(reg));
  149. return (reg.phys_addr >> 32) & 0x0fffffffUL;
  150. } else {
  151. const char *prop_name = cpu_mid_prop();
  152. return prom_getintdefault(prom_node, prop_name, 0);
  153. }
  154. }
  155. static int check_cpu_node(int nd, int *cur_inst,
  156. int (*compare)(int, int, void *), void *compare_arg,
  157. int *prom_node, int *mid)
  158. {
  159. char node_str[128];
  160. prom_getstring(nd, "device_type", node_str, sizeof(node_str));
  161. if (strcmp(node_str, "cpu"))
  162. return -ENODEV;
  163. if (!compare(nd, *cur_inst, compare_arg)) {
  164. if (prom_node)
  165. *prom_node = nd;
  166. if (mid)
  167. *mid = get_cpu_mid(nd);
  168. return 0;
  169. }
  170. (*cur_inst)++;
  171. return -ENODEV;
  172. }
  173. static int __cpu_find_by(int (*compare)(int, int, void *), void *compare_arg,
  174. int *prom_node, int *mid)
  175. {
  176. int nd, cur_inst, err;
  177. nd = prom_root_node;
  178. cur_inst = 0;
  179. err = check_cpu_node(nd, &cur_inst,
  180. compare, compare_arg,
  181. prom_node, mid);
  182. if (err == 0)
  183. return 0;
  184. nd = prom_getchild(nd);
  185. while ((nd = prom_getsibling(nd)) != 0) {
  186. err = check_cpu_node(nd, &cur_inst,
  187. compare, compare_arg,
  188. prom_node, mid);
  189. if (err == 0)
  190. return 0;
  191. }
  192. return -ENODEV;
  193. }
  194. static int cpu_instance_compare(int nd, int instance, void *_arg)
  195. {
  196. int desired_instance = (int) (long) _arg;
  197. if (instance == desired_instance)
  198. return 0;
  199. return -ENODEV;
  200. }
  201. int cpu_find_by_instance(int instance, int *prom_node, int *mid)
  202. {
  203. return __cpu_find_by(cpu_instance_compare, (void *)(long)instance,
  204. prom_node, mid);
  205. }
  206. static int cpu_mid_compare(int nd, int instance, void *_arg)
  207. {
  208. int desired_mid = (int) (long) _arg;
  209. int this_mid;
  210. this_mid = get_cpu_mid(nd);
  211. if (this_mid == desired_mid)
  212. return 0;
  213. return -ENODEV;
  214. }
  215. int cpu_find_by_mid(int mid, int *prom_node)
  216. {
  217. return __cpu_find_by(cpu_mid_compare, (void *)(long)mid,
  218. prom_node, NULL);
  219. }
  220. void __init device_scan(void)
  221. {
  222. /* FIX ME FAST... -DaveM */
  223. ioport_resource.end = 0xffffffffffffffffUL;
  224. prom_printf("Booting Linux...\n");
  225. #ifndef CONFIG_SMP
  226. {
  227. int err, cpu_node, def;
  228. err = cpu_find_by_instance(0, &cpu_node, NULL);
  229. if (err) {
  230. prom_printf("No cpu nodes, cannot continue\n");
  231. prom_halt();
  232. }
  233. cpu_data(0).clock_tick = prom_getintdefault(cpu_node,
  234. "clock-frequency",
  235. 0);
  236. def = ((tlb_type == hypervisor) ?
  237. (8 * 1024) :
  238. (16 * 1024));
  239. cpu_data(0).dcache_size = prom_getintdefault(cpu_node,
  240. "dcache-size",
  241. def);
  242. def = 32;
  243. cpu_data(0).dcache_line_size =
  244. prom_getintdefault(cpu_node, "dcache-line-size",
  245. def);
  246. def = 16 * 1024;
  247. cpu_data(0).icache_size = prom_getintdefault(cpu_node,
  248. "icache-size",
  249. def);
  250. def = 32;
  251. cpu_data(0).icache_line_size =
  252. prom_getintdefault(cpu_node, "icache-line-size",
  253. def);
  254. def = ((tlb_type == hypervisor) ?
  255. (3 * 1024 * 1024) :
  256. (4 * 1024 * 1024));
  257. cpu_data(0).ecache_size = prom_getintdefault(cpu_node,
  258. "ecache-size",
  259. def);
  260. def = 64;
  261. cpu_data(0).ecache_line_size =
  262. prom_getintdefault(cpu_node, "ecache-line-size",
  263. def);
  264. printk("CPU[0]: Caches "
  265. "D[sz(%d):line_sz(%d)] "
  266. "I[sz(%d):line_sz(%d)] "
  267. "E[sz(%d):line_sz(%d)]\n",
  268. cpu_data(0).dcache_size, cpu_data(0).dcache_line_size,
  269. cpu_data(0).icache_size, cpu_data(0).icache_line_size,
  270. cpu_data(0).ecache_size, cpu_data(0).ecache_line_size);
  271. }
  272. #endif
  273. sun4v_virtual_device_probe();
  274. central_probe();
  275. cpu_probe();
  276. }