platform_ps3.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2010 Nathan Whitehorn
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <sys/cdefs.h>
  29. __FBSDID("$FreeBSD$");
  30. #include <sys/param.h>
  31. #include <sys/systm.h>
  32. #include <sys/kernel.h>
  33. #include <sys/bus.h>
  34. #include <sys/pcpu.h>
  35. #include <sys/proc.h>
  36. #include <sys/reboot.h>
  37. #include <sys/smp.h>
  38. #include <vm/vm.h>
  39. #include <vm/pmap.h>
  40. #include <machine/bus.h>
  41. #include <machine/cpu.h>
  42. #include <machine/hid.h>
  43. #include <machine/platform.h>
  44. #include <machine/platformvar.h>
  45. #include <machine/smp.h>
  46. #include <machine/spr.h>
  47. #include <machine/vmparam.h>
  48. #include <dev/ofw/openfirm.h>
  49. #include "platform_if.h"
  50. #include "ps3-hvcall.h"
  51. #ifdef SMP
  52. extern void *ap_pcpu;
  53. #endif
  54. static int ps3_probe(platform_t);
  55. static int ps3_attach(platform_t);
  56. static void ps3_mem_regions(platform_t, struct mem_region *phys, int *physsz,
  57. struct mem_region *avail, int *availsz);
  58. static vm_offset_t ps3_real_maxaddr(platform_t);
  59. static u_long ps3_timebase_freq(platform_t, struct cpuref *cpuref);
  60. #ifdef SMP
  61. static int ps3_smp_first_cpu(platform_t, struct cpuref *cpuref);
  62. static int ps3_smp_next_cpu(platform_t, struct cpuref *cpuref);
  63. static int ps3_smp_get_bsp(platform_t, struct cpuref *cpuref);
  64. static int ps3_smp_start_cpu(platform_t, struct pcpu *cpu);
  65. static void ps3_smp_probe_threads(platform_t);
  66. static struct cpu_group *ps3_smp_topo(platform_t);
  67. #endif
  68. static void ps3_reset(platform_t);
  69. static void ps3_cpu_idle(sbintime_t);
  70. static platform_method_t ps3_methods[] = {
  71. PLATFORMMETHOD(platform_probe, ps3_probe),
  72. PLATFORMMETHOD(platform_attach, ps3_attach),
  73. PLATFORMMETHOD(platform_mem_regions, ps3_mem_regions),
  74. PLATFORMMETHOD(platform_real_maxaddr, ps3_real_maxaddr),
  75. PLATFORMMETHOD(platform_timebase_freq, ps3_timebase_freq),
  76. #ifdef SMP
  77. PLATFORMMETHOD(platform_smp_first_cpu, ps3_smp_first_cpu),
  78. PLATFORMMETHOD(platform_smp_next_cpu, ps3_smp_next_cpu),
  79. PLATFORMMETHOD(platform_smp_get_bsp, ps3_smp_get_bsp),
  80. PLATFORMMETHOD(platform_smp_start_cpu, ps3_smp_start_cpu),
  81. PLATFORMMETHOD(platform_smp_probe_threads, ps3_smp_probe_threads),
  82. PLATFORMMETHOD(platform_smp_topo, ps3_smp_topo),
  83. #endif
  84. PLATFORMMETHOD(platform_reset, ps3_reset),
  85. PLATFORMMETHOD_END
  86. };
  87. static platform_def_t ps3_platform = {
  88. "ps3",
  89. ps3_methods,
  90. 0
  91. };
  92. PLATFORM_DEF(ps3_platform);
  93. static int ps3_boot_pir = 0;
  94. static int
  95. ps3_probe(platform_t plat)
  96. {
  97. phandle_t root;
  98. char compatible[64];
  99. root = OF_finddevice("/");
  100. if (OF_getprop(root, "compatible", compatible, sizeof(compatible)) <= 0)
  101. return (BUS_PROBE_NOWILDCARD);
  102. if (strncmp(compatible, "sony,ps3", sizeof(compatible)) != 0)
  103. return (BUS_PROBE_NOWILDCARD);
  104. return (BUS_PROBE_SPECIFIC);
  105. }
  106. static int
  107. ps3_attach(platform_t plat)
  108. {
  109. pmap_mmu_install("mmu_ps3", BUS_PROBE_SPECIFIC);
  110. cpu_idle_hook = ps3_cpu_idle;
  111. /* Record our PIR at boot for later */
  112. ps3_boot_pir = mfspr(SPR_PIR);
  113. return (0);
  114. }
  115. void
  116. ps3_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
  117. struct mem_region *avail_regions, int *availsz)
  118. {
  119. uint64_t lpar_id, junk;
  120. int i;
  121. /* Prefer device tree information if available */
  122. if (OF_finddevice("/") != -1) {
  123. ofw_mem_regions(phys, physsz, avail_regions, availsz);
  124. } else {
  125. /* Real mode memory region is first segment */
  126. phys[0].mr_start = 0;
  127. phys[0].mr_size = ps3_real_maxaddr(plat);
  128. *physsz = *availsz = 1;
  129. avail_regions[0] = phys[0];
  130. }
  131. /* Now get extended memory region */
  132. lv1_get_logical_partition_id(&lpar_id);
  133. lv1_get_repository_node_value(lpar_id,
  134. lv1_repository_string("bi") >> 32,
  135. lv1_repository_string("rgntotal"), 0, 0,
  136. &phys[*physsz].mr_size, &junk);
  137. for (i = 0; i < *physsz; i++)
  138. phys[*physsz].mr_size -= phys[i].mr_size;
  139. /* Convert to maximum amount we can allocate in 16 MB pages */
  140. phys[*physsz].mr_size -= phys[*physsz].mr_size % (16*1024*1024);
  141. /* Allocate extended memory region */
  142. lv1_allocate_memory(phys[*physsz].mr_size, 24 /* 16 MB pages */,
  143. 0, 0x04 /* any address */, &phys[*physsz].mr_start, &junk);
  144. avail_regions[*availsz] = phys[*physsz];
  145. (*physsz)++;
  146. (*availsz)++;
  147. }
  148. static u_long
  149. ps3_timebase_freq(platform_t plat, struct cpuref *cpuref)
  150. {
  151. uint64_t ticks, node_id, junk;
  152. lv1_get_repository_node_value(PS3_LPAR_ID_PME,
  153. lv1_repository_string("be") >> 32, 0, 0, 0, &node_id, &junk);
  154. lv1_get_repository_node_value(PS3_LPAR_ID_PME,
  155. lv1_repository_string("be") >> 32, node_id,
  156. lv1_repository_string("clock"), 0, &ticks, &junk);
  157. return (ticks);
  158. }
  159. #ifdef SMP
  160. static int
  161. ps3_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
  162. {
  163. cpuref->cr_cpuid = 0;
  164. cpuref->cr_hwref = ps3_boot_pir;
  165. return (0);
  166. }
  167. static int
  168. ps3_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
  169. {
  170. if (cpuref->cr_cpuid >= 1)
  171. return (ENOENT);
  172. cpuref->cr_cpuid++;
  173. cpuref->cr_hwref = !ps3_boot_pir;
  174. return (0);
  175. }
  176. static int
  177. ps3_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
  178. {
  179. cpuref->cr_cpuid = 0;
  180. cpuref->cr_hwref = ps3_boot_pir;
  181. return (0);
  182. }
  183. static int
  184. ps3_smp_start_cpu(platform_t plat, struct pcpu *pc)
  185. {
  186. /* kernel is spinning on 0x40 == -1 right now */
  187. volatile uint32_t *secondary_spin_sem =
  188. (uint32_t *)PHYS_TO_DMAP((uintptr_t)0x40);
  189. int remote_pir = pc->pc_hwref;
  190. int timeout;
  191. ap_pcpu = pc;
  192. /* Try both PIR values, looping a few times: the HV likes moving us */
  193. timeout = 10000;
  194. while (!pc->pc_awake && timeout--) {
  195. *secondary_spin_sem = remote_pir;
  196. powerpc_sync();
  197. DELAY(100);
  198. remote_pir = !remote_pir;
  199. }
  200. return ((pc->pc_awake) ? 0 : EBUSY);
  201. }
  202. static void
  203. ps3_smp_probe_threads(platform_t plat)
  204. {
  205. mp_ncores = 1;
  206. smp_threads_per_core = 2;
  207. }
  208. static struct cpu_group *
  209. ps3_smp_topo(platform_t plat)
  210. {
  211. return (smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_SMT));
  212. }
  213. #endif
  214. static void
  215. ps3_reset(platform_t plat)
  216. {
  217. lv1_panic(1);
  218. }
  219. static vm_offset_t
  220. ps3_real_maxaddr(platform_t plat)
  221. {
  222. uint64_t lpar_id, junk, ppe_id;
  223. static uint64_t rm_maxaddr = 0;
  224. if (rm_maxaddr == 0) {
  225. /* Get real mode memory region */
  226. lv1_get_logical_partition_id(&lpar_id);
  227. lv1_get_logical_ppe_id(&ppe_id);
  228. lv1_get_repository_node_value(lpar_id,
  229. lv1_repository_string("bi") >> 32,
  230. lv1_repository_string("pu"),
  231. ppe_id, lv1_repository_string("rm_size"),
  232. &rm_maxaddr, &junk);
  233. }
  234. return (rm_maxaddr);
  235. }
  236. static void
  237. ps3_cpu_idle(sbintime_t sbt)
  238. {
  239. lv1_pause(0);
  240. }